Spaces:
Runtime error
Runtime error
Ubuntu commited on
Commit ·
419f944
1
Parent(s): c7e62cc
Remove temperature and top-p sliders for default behavior
Browse files
app.py
CHANGED
|
@@ -16,8 +16,6 @@ def respond(
|
|
| 16 |
history: list[tuple[str, str]],
|
| 17 |
system_message,
|
| 18 |
max_tokens,
|
| 19 |
-
temperature,
|
| 20 |
-
top_p,
|
| 21 |
translation_choice: str,
|
| 22 |
):
|
| 23 |
"""
|
|
@@ -25,6 +23,7 @@ def respond(
|
|
| 25 |
"""
|
| 26 |
messages = [{"role": "system", "content": system_message}]
|
| 27 |
|
|
|
|
| 28 |
for val in history:
|
| 29 |
if val[0]: # User message
|
| 30 |
messages.append({"role": "user", "content": val[0]})
|
|
@@ -41,34 +40,34 @@ def respond(
|
|
| 41 |
if translation_choice == "Moroccan Arabic to MSA":
|
| 42 |
# Translate Moroccan Arabic (Darija) to Modern Standard Arabic
|
| 43 |
inputs = tokenizer_darija_to_msa(message, return_tensors="pt", padding=True)
|
| 44 |
-
outputs = model_darija_to_msa.generate(inputs["input_ids"], num_beams=5, max_length=
|
| 45 |
response = tokenizer_darija_to_msa.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
|
| 47 |
elif translation_choice == "English to Moroccan Arabic":
|
| 48 |
# Translate English to Moroccan Arabic (Darija)
|
| 49 |
inputs = tokenizer_eng_to_darija(message, return_tensors="pt", padding=True)
|
| 50 |
-
outputs = model_eng_to_darija.generate(inputs["input_ids"], num_beams=5, max_length=
|
| 51 |
response = tokenizer_eng_to_darija.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
|
| 53 |
return response
|
| 54 |
|
| 55 |
|
| 56 |
-
# Gradio interface setup
|
| 57 |
-
demo = gr.
|
| 58 |
-
respond,
|
| 59 |
-
|
| 60 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 61 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 62 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 64 |
gr.Dropdown(
|
| 65 |
label="Choose Translation Direction",
|
| 66 |
choices=["English to Moroccan Arabic", "Moroccan Arabic to MSA"],
|
| 67 |
value="English to Moroccan Arabic"
|
| 68 |
),
|
| 69 |
],
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
|
|
|
| 16 |
history: list[tuple[str, str]],
|
| 17 |
system_message,
|
| 18 |
max_tokens,
|
|
|
|
|
|
|
| 19 |
translation_choice: str,
|
| 20 |
):
|
| 21 |
"""
|
|
|
|
| 23 |
"""
|
| 24 |
messages = [{"role": "system", "content": system_message}]
|
| 25 |
|
| 26 |
+
# Append previous history to maintain context
|
| 27 |
for val in history:
|
| 28 |
if val[0]: # User message
|
| 29 |
messages.append({"role": "user", "content": val[0]})
|
|
|
|
| 40 |
if translation_choice == "Moroccan Arabic to MSA":
|
| 41 |
# Translate Moroccan Arabic (Darija) to Modern Standard Arabic
|
| 42 |
inputs = tokenizer_darija_to_msa(message, return_tensors="pt", padding=True)
|
| 43 |
+
outputs = model_darija_to_msa.generate(inputs["input_ids"], num_beams=5, max_length=max_tokens, early_stopping=True)
|
| 44 |
response = tokenizer_darija_to_msa.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
|
| 46 |
elif translation_choice == "English to Moroccan Arabic":
|
| 47 |
# Translate English to Moroccan Arabic (Darija)
|
| 48 |
inputs = tokenizer_eng_to_darija(message, return_tensors="pt", padding=True)
|
| 49 |
+
outputs = model_eng_to_darija.generate(inputs["input_ids"], num_beams=5, max_length=max_tokens, early_stopping=True)
|
| 50 |
response = tokenizer_eng_to_darija.decode(outputs[0], skip_special_tokens=True)
|
| 51 |
|
| 52 |
return response
|
| 53 |
|
| 54 |
|
| 55 |
+
# Gradio interface setup without temperature and top-p sliders
|
| 56 |
+
demo = gr.Interface(
|
| 57 |
+
fn=respond,
|
| 58 |
+
inputs=[
|
| 59 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 60 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
|
|
|
|
| 61 |
gr.Dropdown(
|
| 62 |
label="Choose Translation Direction",
|
| 63 |
choices=["English to Moroccan Arabic", "Moroccan Arabic to MSA"],
|
| 64 |
value="English to Moroccan Arabic"
|
| 65 |
),
|
| 66 |
],
|
| 67 |
+
outputs="text"
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Launch the interface
|
| 71 |
+
demo.launch()
|
| 72 |
+
|
| 73 |
|