Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files
app.py
CHANGED
@@ -1,63 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
demo = gr.ChatInterface(
|
46 |
-
|
47 |
-
additional_inputs=[
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
-
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
+
from utils import format_as_translator
|
4 |
+
import requests
|
5 |
+
|
6 |
+
def chatbot_demo(message, history, languages):
|
7 |
+
|
8 |
+
#We don't concatenate history data to avoid noisy data, and avoid the input message becomes too long
|
9 |
+
input_message = format_as_translator(message, languages)
|
10 |
+
|
11 |
+
#Add another assistant delimiter at begining to make sure the output text doesn't contain 'assistant/n/n'
|
12 |
+
json_obj = {
|
13 |
+
"inputs": input_message + '<|eot_id|><|start_header_id|>translator<|end_header_id|>\n\n',
|
14 |
+
"parameters": {
|
15 |
+
"best_of": 1,
|
16 |
+
"decoder_input_details": False,
|
17 |
+
"details": True,
|
18 |
+
"do_sample": True,
|
19 |
+
"frequency_penalty": 0.1,
|
20 |
+
"grammar": None,
|
21 |
+
"max_new_tokens": 500,
|
22 |
+
"repetition_penalty": 1.03,
|
23 |
+
"return_full_text": False,
|
24 |
+
"seed": None,
|
25 |
+
"stop": [
|
26 |
+
"photographer"
|
27 |
+
],
|
28 |
+
"temperature": 0.5,
|
29 |
+
"top_k": 1,
|
30 |
+
"top_n_tokens": 5,
|
31 |
+
"top_p": 0.95,
|
32 |
+
"truncate": None,
|
33 |
+
"typical_p": 0.95,
|
34 |
+
"watermark": True
|
35 |
+
}
|
36 |
+
}
|
37 |
+
|
38 |
+
response = requests.post('https://uf9t072wj5ki2ho4.eu-west-1.aws.endpoints.huggingface.cloud/generate', json=json_obj)
|
39 |
+
data = response.json()
|
40 |
+
llama_out = data['generated_text']
|
41 |
+
for i in range(len(llama_out)):
|
42 |
+
time.sleep(0.05)
|
43 |
+
yield llama_out[: i + 1]
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
demo = gr.ChatInterface(
|
46 |
+
fn=chatbot_demo,
|
47 |
+
additional_inputs=[gr.Textbox("German", label="Target Language", info='SOURCE LANGUAGE SHOULD BE IN ENG!')],
|
48 |
+
chatbot=gr.Chatbot(height=500),
|
49 |
+
textbox=gr.Textbox(placeholder="Just ask Llama3 to translate your sentences into any language you want!", container=False, scale=15),
|
50 |
+
cache_examples=False,
|
51 |
+
title="Llama 3 8B Instruct - Prompted Translator",
|
52 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
+
demo.launch()
|
utils.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
|
4 |
+
def format_as_translator(message: str, languages: str = 'German') -> str:
|
5 |
+
"""
|
6 |
+
Given a message and a history of previous messages, returns a string that formats the conversation as a chat.
|
7 |
+
Uses the format expected by Meta Llama 3 Instruct.
|
8 |
+
|
9 |
+
:param message: A string containing the user's most recent message
|
10 |
+
:param history: A list of lists of previous messages, where each sublist is a conversation turn:
|
11 |
+
[[user_message1, assistant_reply1], [user_message2, assistant_reply2], ...]
|
12 |
+
"""
|
13 |
+
fewshot_training_data = [['Translate this into German: How are you today?', 'Wie geht es dir heute?'],
|
14 |
+
['Translate this into German: I love reading books.', 'Ich liebe es, Bücher zu lesen.'],
|
15 |
+
['Translate this into German: Can you help me with this?', 'Kannst du mir damit helfen?'],
|
16 |
+
['Translate this into German: It\'s a beautiful day outside.', 'Es ist ein schöner Tag draußen.'],
|
17 |
+
['Translate this into German: What time is dinner?', 'Um wie viel Uhr ist das Abendessen?'],
|
18 |
+
['Translate this into Chinese: What is your name?', '你叫什么名字? (Nǐ jiào shénme míngzì?)'],
|
19 |
+
['Translate this into Chinese: I am learning to speak Chinese.', '我在学习说中文。 (Wǒ zài xuéxí shuō zhōngwén.)'],
|
20 |
+
['Translate this into Chinese: Where is the nearest subway station?', '最近的地铁站在哪里? (Zuìjìn de dìtiě zhàn zài nǎlǐ?)'],
|
21 |
+
['Translate this into Chinese: I would like a cup of coffee, please.', '请给我来一杯咖啡。 (Qǐng gěi wǒ lái yī bēi kāfēi.)'],
|
22 |
+
['Translate this into Chinese: How much does this cost?', '这个多少钱? (Zhège duōshǎo qián?)'],
|
23 |
+
['Translate this into Arabic: Where can I find a good restaurant?', 'أين يمكنني أن أجد مطعمًا جيدًا؟ (Ayna yumkinunī an ajida maṭʿaman jayyidan?)'],
|
24 |
+
['Translate this into Arabic: I need to book a flight.', 'أحتاج إلى حجز رحلة طيران. (Aḥtāj ilā ḥajz riḥlat ṭayrān.)'],
|
25 |
+
['Translate this into Arabic: What is the weather like tomorrow?', 'كيف سيكون الطقس غدًا؟ (Kayfa sayakūn al-ṭaqs ghadan?)'],
|
26 |
+
['Translate this into Arabic: I am visiting for the first time.', 'أنا أزور للمرة الأولى. (Anā azūr lilmarrati al\'ūlā.)'],
|
27 |
+
['Translate this into Arabic: Can you speak slower, please?', 'هل يمكنك التحدث ببطء، من فضلك؟ (Hal yumkinuk al-taḥadduth bibuṭ\', min faḍlik?)']]
|
28 |
+
|
29 |
+
fewshot_lis = ['<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n' +
|
30 |
+
'<|eot_id|><|start_header_id|>translator<|end_header_id|>\n\n'.join(sub_fewshotlis) +
|
31 |
+
'<|eot_id|>' for sub_fewshotlis in fewshot_training_data]
|
32 |
+
|
33 |
+
#To avoid error response from model mix into our few shot data, we don't concatenate the history data here
|
34 |
+
output_message = ''.join(fewshot_lis) + '<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n' + f'Translate this into {languages}: ' + message + '<|eot_id|>'
|
35 |
+
|
36 |
+
return output_message
|
37 |
+
|