HeshamHaroon commited on
Commit
5f1a7e4
1 Parent(s): 5f44fd5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+ from deep_translator import GoogleTranslator
4
+
5
+ # Initialize the Hugging Face Inference Client with the specific model
6
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
7
+
8
+ # Function to translate Arabic text to English
9
+ def translate_to_english(text):
10
+ return GoogleTranslator(source='arabic', target='english').translate(text)
11
+
12
+ # Function to translate English text to Arabic
13
+ def translate_to_arabic(text):
14
+ return GoogleTranslator(source='english', target='arabic').translate(text)
15
+
16
+ # Function to format the prompt with conversation history
17
+ def format_prompt(message, history):
18
+ prompt = "<s>"
19
+ for user_prompt, bot_response in history:
20
+ prompt += f"[INST] {user_prompt} [/INST]"
21
+ prompt += f" {bot_response}</s> "
22
+ prompt += f"[INST] {message} [/INST]"
23
+ return prompt
24
+
25
+ # The main function to generate responses
26
+ def generate(prompt, history, temperature=0.1, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
27
+ temperature = float(temperature)
28
+ if temperature < 1e-2:
29
+ temperature = 1e-2
30
+ top_p = float(top_p)
31
+
32
+ generate_kwargs = dict(
33
+ temperature=temperature,
34
+ max_new_tokens=max_new_tokens,
35
+ top_p=top_p,
36
+ repetition_penalty=repetition_penalty,
37
+ do_sample=True,
38
+ seed=42,
39
+ )
40
+
41
+ # Translate the Arabic prompt to English
42
+ english_prompt = translate_to_english(prompt)
43
+
44
+ formatted_prompt = format_prompt(english_prompt, history)
45
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
46
+ output = ""
47
+
48
+ for response in stream:
49
+ output += response.token.text
50
+ # Translate the English response back to Arabic before yielding it
51
+ arabic_output = translate_to_arabic(output)
52
+ yield arabic_output
53
+ return arabic_output
54
+
55
+ # Additional input widgets for controlling the generation parameters
56
+ additional_inputs = [
57
+ gr.Slider(
58
+ label="Temperature",
59
+ value=0.9,
60
+ minimum=0.0,
61
+ maximum=1.0,
62
+ step=0.05,
63
+ interactive=True,
64
+ info="Higher values produce more diverse outputs",
65
+ ),
66
+ gr.Slider(
67
+ label="Max new tokens",
68
+ value=256,
69
+ minimum=0,
70
+ maximum=1048,
71
+ step=64,
72
+ interactive=True,
73
+ info="The maximum numbers of new tokens",
74
+ ),
75
+ gr.Slider(
76
+ label="Top-p (nucleus sampling)",
77
+ value=0.90,
78
+ minimum=0.0,
79
+ maximum=1.0,
80
+ step=0.05,
81
+ interactive=True,
82
+ info="Higher values sample more low-probability tokens",
83
+ ),
84
+ gr.Slider(
85
+ label="Repetition penalty",
86
+ value=1.2,
87
+ minimum=1.0,
88
+ maximum=2.0,
89
+ step=0.05,
90
+ interactive=True,
91
+ info="Penalize repeated tokens",
92
+ )
93
+ ]
94
+
95
+ # Creating and launching the Gradio interface
96
+ gr.Interface(
97
+ fn=generate,
98
+ inputs=[
99
+ gr.inputs.Textbox(lines=2, label="Your Prompt in Arabic"),
100
+ gr.inputs.State(label="Conversation History"),
101
+ *additional_inputs
102
+ ],
103
+ outputs=gr.outputs.Textbox(label="Generated Response in Arabic"),
104
+ title="Try Arabic Misteral",
105
+ description="Interact with an advanced AI model in Arabic. Adjust the settings below to tailor the responses. Your prompts will be translated to English, processed by the AI, and the response will be translated back to Arabic."
106
+ ).launch(show_api=True)