Commit
·
0cba459
1
Parent(s):
0f08005
Refactor language handling in app.py to use a dictionary for system messages, enhancing support for multiple languages and simplifying message retrieval.
Browse files- app/app.py +19 -15
app/app.py
CHANGED
@@ -13,7 +13,20 @@ from gradio.components.chatbot import Option
|
|
13 |
from huggingface_hub import InferenceClient
|
14 |
from pandas import DataFrame
|
15 |
|
16 |
-
LANGUAGES:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
client = InferenceClient(
|
19 |
token=os.getenv("HF_TOKEN"),
|
@@ -34,17 +47,6 @@ def add_user_message(history, message):
|
|
34 |
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
35 |
|
36 |
|
37 |
-
def get_system_message(language: str) -> str:
|
38 |
-
if language == "English":
|
39 |
-
return "You are a helpful assistant that speaks English."
|
40 |
-
elif language == "Spanish":
|
41 |
-
return "Tu eres un asistente útil que habla español."
|
42 |
-
elif language == "Hebrew":
|
43 |
-
return "אתה עוזר טוב שמפגש בעברית."
|
44 |
-
elif language == "Dutch":
|
45 |
-
return "Je bent een handige assistent die Nederlands spreekt."
|
46 |
-
|
47 |
-
|
48 |
def format_system_message(language: str, history: list):
|
49 |
if history:
|
50 |
if history[0]["role"] == "system":
|
@@ -52,7 +54,7 @@ def format_system_message(language: str, history: list):
|
|
52 |
system_message = [
|
53 |
{
|
54 |
"role": "system",
|
55 |
-
"content":
|
56 |
}
|
57 |
]
|
58 |
history = system_message + history
|
@@ -315,7 +317,9 @@ with gr.Blocks(css=css) as demo:
|
|
315 |
|
316 |
Some feedback is automatically submitted allowing you to continue chatting, but you can also submit and reset the conversation by clicking "💾 Submit conversation" (under the chat) or trash the conversation by clicking "🗑️" (upper right corner).
|
317 |
""")
|
318 |
-
language = gr.Dropdown(
|
|
|
|
|
319 |
|
320 |
session_id = gr.Textbox(
|
321 |
interactive=False,
|
@@ -330,7 +334,7 @@ with gr.Blocks(css=css) as demo:
|
|
330 |
value=[
|
331 |
{
|
332 |
"role": "system",
|
333 |
-
"content":
|
334 |
}
|
335 |
],
|
336 |
type="messages",
|
|
|
13 |
from huggingface_hub import InferenceClient
|
14 |
from pandas import DataFrame
|
15 |
|
16 |
+
LANGUAGES: dict[str, str] = {
|
17 |
+
"English": "You are a helpful assistant that speaks English.",
|
18 |
+
"Spanish": "Tu eres un asistente útil que habla español.",
|
19 |
+
"Hebrew": "אתה עוזר טוב שמפגש בעברית.",
|
20 |
+
"Dutch": "Je bent een handige assistent die Nederlands spreekt.",
|
21 |
+
"Italian": "Tu sei un assistente utile che parla italiano.",
|
22 |
+
"French": "Tu es un assistant utile qui parle français.",
|
23 |
+
"German": "Du bist ein hilfreicher Assistent, der Deutsch spricht.",
|
24 |
+
"Portuguese": "Você é um assistente útil que fala português.",
|
25 |
+
"Russian": "Ты полезный помощник, который говорит по-русски.",
|
26 |
+
"Chinese": "你是一个有用的助手,会说中文。",
|
27 |
+
"Japanese": "あなたは役立つ助け役で、日本語を話します。",
|
28 |
+
"Korean": "당신은 유용한 도우미이며 한국어를 말합니다.",
|
29 |
+
}
|
30 |
|
31 |
client = InferenceClient(
|
32 |
token=os.getenv("HF_TOKEN"),
|
|
|
47 |
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
48 |
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def format_system_message(language: str, history: list):
|
51 |
if history:
|
52 |
if history[0]["role"] == "system":
|
|
|
54 |
system_message = [
|
55 |
{
|
56 |
"role": "system",
|
57 |
+
"content": LANGUAGES[language],
|
58 |
}
|
59 |
]
|
60 |
history = system_message + history
|
|
|
317 |
|
318 |
Some feedback is automatically submitted allowing you to continue chatting, but you can also submit and reset the conversation by clicking "💾 Submit conversation" (under the chat) or trash the conversation by clicking "🗑️" (upper right corner).
|
319 |
""")
|
320 |
+
language = gr.Dropdown(
|
321 |
+
choices=list(LANGUAGES.keys()), label="Language", interactive=True
|
322 |
+
)
|
323 |
|
324 |
session_id = gr.Textbox(
|
325 |
interactive=False,
|
|
|
334 |
value=[
|
335 |
{
|
336 |
"role": "system",
|
337 |
+
"content": LANGUAGES[language.value],
|
338 |
}
|
339 |
],
|
340 |
type="messages",
|