Spaces:
Runtime error
Runtime error
Jayabalambika
commited on
Commit
•
521155d
1
Parent(s):
7e1327a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
|
6 |
+
hf_token = os.getenv('HF_TOKEN')
|
7 |
+
api_url = os.getenv('API_URL')
|
8 |
+
headers = {
|
9 |
+
'Content-Type': 'application/json',
|
10 |
+
}
|
11 |
+
|
12 |
+
system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
|
13 |
+
title = "Llama2 70B Chatbot"
|
14 |
+
description = """LLama2 demo"""
|
15 |
+
css = """.toast-wrap { display: none !important } """
|
16 |
+
examples=[
|
17 |
+
'Explain the phenomenon of Quantum tunneling?',
|
18 |
+
'Explain few sentences about exoplanets',
|
19 |
+
'What is Sea surface temperature? how is it measured?',
|
20 |
+
]
|
21 |
+
|
22 |
+
|
23 |
+
def predict(message, chatbot):
|
24 |
+
|
25 |
+
input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
|
26 |
+
for interaction in chatbot:
|
27 |
+
input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "
|
28 |
+
|
29 |
+
input_prompt = input_prompt + str(message) + " [/INST] "
|
30 |
+
|
31 |
+
data = {
|
32 |
+
"inputs": input_prompt,
|
33 |
+
"parameters": {"max_new_tokens":256}
|
34 |
+
}
|
35 |
+
|
36 |
+
response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token), stream=True)
|
37 |
+
|
38 |
+
partial_message = ""
|
39 |
+
for line in response.iter_lines():
|
40 |
+
if line: # filter out keep-alive new lines
|
41 |
+
# Decode from bytes to string
|
42 |
+
decoded_line = line.decode('utf-8')
|
43 |
+
|
44 |
+
# Remove 'data:' prefix
|
45 |
+
if decoded_line.startswith('data:'):
|
46 |
+
json_line = decoded_line[5:] # Exclude the first 5 characters ('data:')
|
47 |
+
else:
|
48 |
+
gr.Warning(f"This line does not start with 'data:': {decoded_line}")
|
49 |
+
continue
|
50 |
+
|
51 |
+
# Load as JSON
|
52 |
+
try:
|
53 |
+
json_obj = json.loads(json_line)
|
54 |
+
if 'token' in json_obj:
|
55 |
+
partial_message = partial_message + json_obj['token']['text']
|
56 |
+
yield partial_message
|
57 |
+
elif 'error' in json_obj:
|
58 |
+
yield json_obj['error'] + '. Please refresh and try again with an appropriate smaller input prompt.'
|
59 |
+
else:
|
60 |
+
gr.Warning(f"The key 'token' does not exist in this JSON object: {json_obj}")
|
61 |
+
|
62 |
+
except json.JSONDecodeError:
|
63 |
+
gr.Warning(f"This line is not valid JSON: {json_line}")
|
64 |
+
continue
|
65 |
+
except KeyError as e:
|
66 |
+
gr.Warning(f"KeyError: {e} occurred for JSON object: {json_obj}")
|
67 |
+
continue
|
68 |
+
|
69 |
+
gr.ChatInterface(predict, title=title, description=description, css=css, examples=examples, cache_examples=True).queue(concurrency_count=75).launch()
|