Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -22,20 +22,15 @@ translator = pipeline("translation", model=TRANSLATOR_MODEL)
|
|
22 |
|
23 |
# ---------------- HELPERS ----------------
|
24 |
def is_translation_request(message: str) -> bool:
|
25 |
-
"""
|
26 |
-
Heuristics: if user explicitly asks to translate OR if message is not English.
|
27 |
-
"""
|
28 |
triggers = ["translate", "traduce", "ترجم", "traduire", "übersetze"]
|
29 |
if any(t in message.lower() for t in triggers):
|
30 |
return True
|
31 |
-
# naive non-English detection (if >40% chars non-ASCII)
|
32 |
non_ascii_ratio = sum(1 for c in message if ord(c) > 127) / max(len(message), 1)
|
33 |
return non_ascii_ratio > 0.4
|
34 |
|
35 |
|
36 |
# ---------------- CHAT FUNCTION ----------------
|
37 |
def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
|
38 |
-
# check if translation
|
39 |
if is_translation_request(message):
|
40 |
try:
|
41 |
translated = translator(message, src_lang="auto", tgt_lang="en")[0]["translation_text"]
|
@@ -60,10 +55,9 @@ def stream_response(message, chat_history, system_message, max_tokens, temperatu
|
|
60 |
messages = [{"role": "system", "content": system_message}] + chat_history
|
61 |
messages.append({"role": "user", "content": message})
|
62 |
|
63 |
-
# Append user first
|
64 |
chat_history.append({"role": "user", "content": message})
|
65 |
response = ""
|
66 |
-
chat_history.append({"role": "assistant", "content": ""})
|
67 |
|
68 |
for msg in client.chat_completion(
|
69 |
messages,
|
@@ -74,10 +68,9 @@ def stream_response(message, chat_history, system_message, max_tokens, temperatu
|
|
74 |
):
|
75 |
token = msg.choices[0].delta.content or ""
|
76 |
response += token
|
77 |
-
chat_history[-1]["content"] = response
|
78 |
yield "", chat_history
|
79 |
|
80 |
-
# finally clear input box once after streaming is done
|
81 |
yield "", chat_history
|
82 |
|
83 |
|
@@ -109,7 +102,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink"))
|
|
109 |
system_prompt = gr.Textbox(label="System Prompt", value=SYSTEM_PROMPT_DEFAULT, lines=3)
|
110 |
response_style = gr.Dropdown(
|
111 |
["No explanation", "Short explanation", "Detailed explanation"],
|
112 |
-
value="
|
113 |
label="Response Style"
|
114 |
)
|
115 |
temperature = gr.Slider(0.1, 1.5, value=TEMP_DEFAULT, step=0.1, label="Temperature")
|
|
|
22 |
|
23 |
# ---------------- HELPERS ----------------
|
24 |
def is_translation_request(message: str) -> bool:
|
|
|
|
|
|
|
25 |
triggers = ["translate", "traduce", "ترجم", "traduire", "übersetze"]
|
26 |
if any(t in message.lower() for t in triggers):
|
27 |
return True
|
|
|
28 |
non_ascii_ratio = sum(1 for c in message if ord(c) > 127) / max(len(message), 1)
|
29 |
return non_ascii_ratio > 0.4
|
30 |
|
31 |
|
32 |
# ---------------- CHAT FUNCTION ----------------
|
33 |
def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
|
|
|
34 |
if is_translation_request(message):
|
35 |
try:
|
36 |
translated = translator(message, src_lang="auto", tgt_lang="en")[0]["translation_text"]
|
|
|
55 |
messages = [{"role": "system", "content": system_message}] + chat_history
|
56 |
messages.append({"role": "user", "content": message})
|
57 |
|
|
|
58 |
chat_history.append({"role": "user", "content": message})
|
59 |
response = ""
|
60 |
+
chat_history.append({"role": "assistant", "content": ""})
|
61 |
|
62 |
for msg in client.chat_completion(
|
63 |
messages,
|
|
|
68 |
):
|
69 |
token = msg.choices[0].delta.content or ""
|
70 |
response += token
|
71 |
+
chat_history[-1]["content"] = response
|
72 |
yield "", chat_history
|
73 |
|
|
|
74 |
yield "", chat_history
|
75 |
|
76 |
|
|
|
102 |
system_prompt = gr.Textbox(label="System Prompt", value=SYSTEM_PROMPT_DEFAULT, lines=3)
|
103 |
response_style = gr.Dropdown(
|
104 |
["No explanation", "Short explanation", "Detailed explanation"],
|
105 |
+
value="No explanation", # ✅ default set here
|
106 |
label="Response Style"
|
107 |
)
|
108 |
temperature = gr.Slider(0.1, 1.5, value=TEMP_DEFAULT, step=0.1, label="Temperature")
|