Ubuntu commited on
Commit
a26be94
·
1 Parent(s): 419f944

Fix input field and remove pre-filled system message

Browse files
Files changed (1) hide show
  1. app.py +19 -29
app.py CHANGED
@@ -18,45 +18,37 @@ def respond(
18
  max_tokens,
19
  translation_choice: str,
20
  ):
21
- """
22
- Responds to the input message by selecting the translation model based on the user's choice.
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]})
30
- if val[1]: # Assistant message
31
- messages.append({"role": "assistant", "content": val[1]})
32
-
33
- # Append the user message
34
- messages.append({"role": "user", "content": message})
35
 
36
  # Initialize the response variable
37
  response = ""
38
 
39
  # Translate based on the user's choice
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",
@@ -69,5 +61,3 @@ demo = gr.Interface(
69
 
70
  # Launch the interface
71
  demo.launch()
72
-
73
-
 
18
  max_tokens,
19
  translation_choice: str,
20
  ):
21
+ # Ensure there's no empty input
22
+ if not message.strip():
23
+ return "Error: Please enter a valid text to translate."
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Initialize the response variable
26
  response = ""
27
 
28
  # Translate based on the user's choice
29
+ try:
30
+ if translation_choice == "Moroccan Arabic to MSA":
31
+ # Translate Moroccan Arabic (Darija) to Modern Standard Arabic
32
+ inputs = tokenizer_darija_to_msa(message, return_tensors="pt", padding=True)
33
+ outputs = model_darija_to_msa.generate(inputs["input_ids"], num_beams=5, max_length=max_tokens, early_stopping=True)
34
+ response = tokenizer_darija_to_msa.decode(outputs[0], skip_special_tokens=True)
35
+
36
+ elif translation_choice == "English to Moroccan Arabic":
37
+ # Translate English to Moroccan Arabic (Darija)
38
+ inputs = tokenizer_eng_to_darija(message, return_tensors="pt", padding=True)
39
+ outputs = model_eng_to_darija.generate(inputs["input_ids"], num_beams=5, max_length=max_tokens, early_stopping=True)
40
+ response = tokenizer_eng_to_darija.decode(outputs[0], skip_special_tokens=True)
41
+ except Exception as e:
42
+ response = f"Error occurred: {str(e)}"
43
 
44
  return response
45
 
46
 
47
+ # Gradio interface setup without pre-filled system message
48
  demo = gr.Interface(
49
  fn=respond,
50
  inputs=[
51
+ gr.Textbox(value="", label="Enter Your Text", placeholder="Type your sentence here..."),
52
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
53
  gr.Dropdown(
54
  label="Choose Translation Direction",
 
61
 
62
  # Launch the interface
63
  demo.launch()