Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import pandas as pd
|
|
| 9 |
|
| 10 |
from huggingface_hub import Repository, upload_file
|
| 11 |
from text_generation import Client
|
|
|
|
| 12 |
from share_btn import (community_icon_html, loading_icon_html, share_btn_css,
|
| 13 |
share_js)
|
| 14 |
|
|
@@ -96,14 +97,14 @@ def get_inference_prompt(messages, model_name):
|
|
| 96 |
prompt = "<|system|>\n<|endoftext|>\n"
|
| 97 |
for message in messages :
|
| 98 |
if message["role"] == "user" :
|
| 99 |
-
prompt += f"<|user|>\n{message['content']}<|endoftext|>\n<|assistant
|
| 100 |
else : # message["role"] == "assistant"
|
| 101 |
prompt += f"\n{message['content']}<|endoftext|>\n"
|
| 102 |
elif model_name == "starChat-alpha" :
|
| 103 |
prompt = "<|system|>\n<|end|>\n"
|
| 104 |
for message in messages :
|
| 105 |
if message["role"] == "user" :
|
| 106 |
-
prompt += f"<|user|>\n{message['content']}<|end|>\n<|assistant
|
| 107 |
else : # message["role"] == "assistant"
|
| 108 |
prompt += f"\n{message['content']}<|end|>\n"
|
| 109 |
else : # starCoder-gradio
|
|
@@ -180,40 +181,43 @@ def generate(
|
|
| 180 |
stop_sequences=["<|end|>", "Question:"],
|
| 181 |
)
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
examples = [
|
| 219 |
"Use the gradio library to create a calculator. It should take into account the 4 basic operations. A user should be able to enter 2 numbers, choose an operation and get the corresponding result.",
|
|
|
|
| 9 |
|
| 10 |
from huggingface_hub import Repository, upload_file
|
| 11 |
from text_generation import Client
|
| 12 |
+
from text_generation.errors import UnknownError
|
| 13 |
from share_btn import (community_icon_html, loading_icon_html, share_btn_css,
|
| 14 |
share_js)
|
| 15 |
|
|
|
|
| 97 |
prompt = "<|system|>\n<|endoftext|>\n"
|
| 98 |
for message in messages :
|
| 99 |
if message["role"] == "user" :
|
| 100 |
+
prompt += f"<|user|>\n{message['content']}<|endoftext|>\n<|assistant|>\n"
|
| 101 |
else : # message["role"] == "assistant"
|
| 102 |
prompt += f"\n{message['content']}<|endoftext|>\n"
|
| 103 |
elif model_name == "starChat-alpha" :
|
| 104 |
prompt = "<|system|>\n<|end|>\n"
|
| 105 |
for message in messages :
|
| 106 |
if message["role"] == "user" :
|
| 107 |
+
prompt += f"<|user|>\n{message['content']}<|end|>\n<|assistant|>\n"
|
| 108 |
else : # message["role"] == "assistant"
|
| 109 |
prompt += f"\n{message['content']}<|end|>\n"
|
| 110 |
else : # starCoder-gradio
|
|
|
|
| 181 |
stop_sequences=["<|end|>", "Question:"],
|
| 182 |
)
|
| 183 |
|
| 184 |
+
try :
|
| 185 |
+
stream = client.generate_stream(
|
| 186 |
+
prompt,
|
| 187 |
+
**generate_kwargs,
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
output = ""
|
| 191 |
+
for idx, response in enumerate(stream):
|
| 192 |
+
if response.token.special:
|
| 193 |
+
continue
|
| 194 |
+
output += response.token.text
|
| 195 |
+
if idx == 0:
|
| 196 |
+
history.append(" " + output)
|
| 197 |
+
else:
|
| 198 |
+
history[-1] = output
|
| 199 |
+
|
| 200 |
+
chat = [
|
| 201 |
+
(wrap_html_code(history[i].strip()), wrap_html_code(history[i + 1].strip()))
|
| 202 |
+
for i in range(0, len(history) - 1, 2)
|
| 203 |
+
]
|
| 204 |
+
|
| 205 |
+
# chat = [(history[i].strip(), history[i + 1].strip()) for i in range(0, len(history) - 1, 2)]
|
| 206 |
+
|
| 207 |
+
yield chat, history, user_message, ""
|
| 208 |
+
|
| 209 |
+
if HF_TOKEN and do_save:
|
| 210 |
+
try:
|
| 211 |
+
now = datetime.datetime.now()
|
| 212 |
+
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 213 |
+
print(f"[{current_time}] Pushing prompt and completion to the Hub")
|
| 214 |
+
save_inputs_and_outputs(now, prompt, output, generate_kwargs, model_name)
|
| 215 |
+
except Exception as e:
|
| 216 |
+
print(e)
|
| 217 |
+
|
| 218 |
+
return chat, history, user_message, ""
|
| 219 |
+
except UnknownError :
|
| 220 |
+
return "The model is currently loading. Please wait a few seconds and retry.", None, None, ""
|
| 221 |
|
| 222 |
examples = [
|
| 223 |
"Use the gradio library to create a calculator. It should take into account the 4 basic operations. A user should be able to enter 2 numbers, choose an operation and get the corresponding result.",
|