HeshamHaroon commited on
Commit
4195e71
1 Parent(s): f7a0c83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -83
app.py CHANGED
@@ -2,16 +2,17 @@ from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  from deep_translator import GoogleTranslator
4
 
 
5
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
6
 
7
- # Function to translate Arabic text to English
8
- def translate_to_english(text):
9
- return GoogleTranslator(source='arabic', target='english').translate(text)
10
-
11
- # Function to translate English text to Arabic
12
  def translate_to_arabic(text):
13
- return GoogleTranslator(source='english', target='arabic').translate(text)
 
 
 
14
 
 
15
  def format_prompt(message, history):
16
  prompt = "<s>"
17
  for user_prompt, bot_response in history:
@@ -20,88 +21,50 @@ def format_prompt(message, history):
20
  prompt += f"[INST] {message} [/INST]"
21
  return prompt
22
 
23
- def generate(
24
- prompt, history, temperature=0.1, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
25
- ):
26
- # Initialize history as an empty list if it's None
27
- if history is None:
28
- history = []
29
-
30
- temperature = float(temperature)
31
- if temperature < 1e-2:
32
- temperature = 1e-2
33
- top_p = float(top_p)
34
-
35
- generate_kwargs = dict(
36
- temperature=temperature,
37
- max_new_tokens=max_new_tokens,
38
- top_p=top_p,
39
- repetition_penalty=repetition_penalty,
40
- do_sample=True,
41
- seed=42,
42
- )
43
-
44
- # Translate the Arabic prompt to English
45
- english_prompt = translate_to_english(prompt)
46
-
47
- formatted_prompt = format_prompt(english_prompt, history)
48
-
49
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
 
50
  output = ""
51
-
52
  for response in stream:
53
- output += response.token.text
54
 
55
  # Translate the English response back to Arabic
56
- arabic_output = translate_to_arabic(output)
57
-
58
- # Update the history state with the latest exchange
59
- history.append((prompt, arabic_output))
60
-
61
- return arabic_output, history
62
-
63
- additional_inputs=[
64
- gr.Slider(
65
- label="Temperature",
66
- value=0.9,
67
- minimum=0.0,
68
- maximum=1.0,
69
- step=0.05,
70
- interactive=True,
71
- info="Higher values produce more diverse outputs",
72
- ),
73
- gr.Slider(
74
- label="Max new tokens",
75
- value=256,
76
- minimum=0,
77
- maximum=1048,
78
- step=64,
79
- interactive=True,
80
- info="The maximum numbers of new tokens",
81
- ),
82
- gr.Slider(
83
- label="Top-p (nucleus sampling)",
84
- value=0.90,
85
- minimum=0.0,
86
- maximum=1,
87
- step=0.05,
88
- interactive=True,
89
- info="Higher values sample more low-probability tokens",
90
- ),
91
- gr.Slider(
92
- label="Repetition penalty",
93
- value=1.2,
94
- minimum=1.0,
95
- maximum=2.0,
96
- step=0.05,
97
- interactive=True,
98
- info="Penalize repeated tokens",
99
- )
100
  ]
101
 
102
- gr.ChatInterface(
 
103
  fn=generate,
104
- chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
105
- additional_inputs=additional_inputs,
106
- title="DorjGPT interface"
107
- ).launch(show_api=True)
 
 
 
 
 
 
 
2
  import gradio as gr
3
  from deep_translator import GoogleTranslator
4
 
5
+ # Initialize the Hugging Face client with your model
6
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
7
 
8
+ # Define the translation functions
 
 
 
 
9
  def translate_to_arabic(text):
10
+ return GoogleTranslator(source='auto', target='ar').translate(text)
11
+
12
+ def translate_to_english(text):
13
+ return GoogleTranslator(source='auto', target='en').translate(text)
14
 
15
+ # Format the prompt for the model
16
  def format_prompt(message, history):
17
  prompt = "<s>"
18
  for user_prompt, bot_response in history:
 
21
  prompt += f"[INST] {message} [/INST]"
22
  return prompt
23
 
24
+ # Generate a response from the model
25
+ def generate(prompt, history=[], temperature=0.1, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
26
+ # Translate the Arabic prompt to English before sending to the model
27
+ prompt_in_english = translate_to_english(prompt)
28
+ formatted_prompt = format_prompt(prompt_in_english, history)
29
+
30
+ generate_kwargs = {
31
+ "temperature": temperature,
32
+ "max_new_tokens": max_new_tokens,
33
+ "top_p": top_p,
34
+ "repetition_penalty": repetition_penalty,
35
+ "do_sample": True,
36
+ "seed": 42, # Seed for reproducibility, remove or change if randomness is preferred
37
+ }
38
+
39
+ # Generate the response
 
 
 
 
 
 
 
 
 
 
40
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
41
+
42
  output = ""
 
43
  for response in stream:
44
+ output += response["token"]["text"]
45
 
46
  # Translate the English response back to Arabic
47
+ response_in_arabic = translate_to_arabic(output)
48
+ return response_in_arabic
49
+
50
+ # Define additional inputs for Gradio interface
51
+ additional_inputs = [
52
+ gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05),
53
+ gr.Slider(label="Max new tokens", value=256, minimum=0, maximum=1048, step=64),
54
+ gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1.0, step=0.05),
55
+ gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  ]
57
 
58
+ # Set up the Gradio interface
59
+ iface = gr.Interface(
60
  fn=generate,
61
+ inputs=[
62
+ gr.Textbox(lines=5, placeholder='Type your Arabic query here...', label='Arabic Query'),
63
+ *additional_inputs
64
+ ],
65
+ outputs='text',
66
+ title="DorjGPT Arabic-English Translation Chatbot",
67
+ )
68
+
69
+ # Launch the Gradio interface
70
+ iface.launch()