Beibars003 commited on
Commit
2fe2b52
·
verified ·
1 Parent(s): 1f5eded

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +199 -0
app.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ import re
4
+ from tqdm import tqdm
5
+ from openai import OpenAI
6
+
7
+ instructs = {'eng': 'English',
8
+ 'kaz': 'Kazakh',
9
+ 'rus': 'Russian',
10
+ 'tur': 'Turkish',
11
+ 'uzn': 'Uzbek',
12
+ 'zho_simpl': 'Chinese (Simplified)'}
13
+
14
+ openai_api_key = "EMPTY"
15
+ openai_api_base = "http://localhost:7050/v1"
16
+ model_path = "gemma_translator"
17
+ client = OpenAI(
18
+ api_key=openai_api_key,
19
+ base_url=openai_api_base,
20
+ )
21
+
22
+
23
+ def build_prompt_alpaca(instruction: str, input_text: str = "") -> str:
24
+ return (
25
+ "<bos>Below is an instruction that describes a task, paired with an input that provides further context. "
26
+ "Write a response that appropriately completes the request.\n\n"
27
+ f"### Instruction:\n{instruction}\n\n### Input:\n{input_text}\n\n### Response:\n"
28
+ )
29
+
30
+
31
+ def get_prediction(instruction, input_text, temperature=0.0, top_p=0.9, max_tokens=1024,
32
+ presence_penalty=0.0, frequency_penalty=0.3, repetition_penalty=1.0,
33
+ top_k=64, min_p=0.0, retry_count=3, sleep_time=1):
34
+ for attempt in range(retry_count):
35
+ try:
36
+ prompt = build_prompt_alpaca(instruction, input_text)
37
+ response = client.completions.create(
38
+ model=model_path,
39
+ prompt=prompt,
40
+ temperature=temperature,
41
+ top_p=top_p,
42
+ max_tokens=max_tokens,
43
+ frequency_penalty=frequency_penalty,
44
+ stream=True
45
+ )
46
+ return response
47
+
48
+ except Exception as e:
49
+ print(f"Error on attempt {attempt+1}: {str(e)}")
50
+ if attempt < retry_count - 1:
51
+ print(f"Retrying in {sleep_time} seconds...")
52
+ time.sleep(sleep_time)
53
+ sleep_time *= 2 # Exponential backoff
54
+ else:
55
+ print("Max retries reached. Returning empty string.")
56
+ return ""
57
+
58
+
59
+ def process_streaming_response(response):
60
+ """Process streaming response and return complete text"""
61
+ if not response:
62
+ return "Error: No response received"
63
+
64
+ buffer = ""
65
+ is_first_chunk = True
66
+ complete_text = ""
67
+
68
+ try:
69
+ for chunk in response:
70
+ if hasattr(chunk, 'choices') and chunk.choices and chunk.choices[0].text:
71
+ text_content = chunk.choices[0].text
72
+
73
+ if is_first_chunk:
74
+ text_content = text_content.lstrip()
75
+ if text_content:
76
+ text_content = " " + text_content
77
+ is_first_chunk = False
78
+
79
+ buffer += text_content
80
+ complete_text += text_content
81
+
82
+ return complete_text.strip()
83
+ except Exception as e:
84
+ return f"Error processing response: {str(e)}"
85
+
86
+
87
+ def generate_translation(text, target_lang, temperature, top_p, max_tokens,
88
+ presence_penalty, frequency_penalty, repetition_penalty,
89
+ top_k, min_p, use_v0_prompt=False):
90
+ """Updated function that accepts all parameters from Gradio"""
91
+ if not text.strip():
92
+ return "Please enter some text to translate."
93
+
94
+ text = text.strip()
95
+
96
+ # Build instruction based on target language
97
+ if use_v0_prompt:
98
+ instruction = f"Translate the following text into {instructs[target_lang]}."
99
+ else:
100
+ instruction = f"Translate to {instructs[target_lang]}"
101
+
102
+ try:
103
+ # Use the get_prediction function for inference
104
+ response = get_prediction(
105
+ instruction=instruction,
106
+ input_text=text,
107
+ temperature=temperature,
108
+ top_p=top_p,
109
+ max_tokens=int(max_tokens),
110
+ presence_penalty=presence_penalty,
111
+ frequency_penalty=frequency_penalty,
112
+ repetition_penalty=repetition_penalty,
113
+ top_k=int(top_k),
114
+ min_p=min_p,
115
+ retry_count=3,
116
+ sleep_time=1
117
+ )
118
+
119
+ # Process the streaming response
120
+ return process_streaming_response(response)
121
+
122
+ except Exception as e:
123
+ return f"Error: {str(e)}"
124
+
125
+
126
+ def set_example_text(example_text):
127
+ """Helper function to set example text"""
128
+ return example_text
129
+
130
+
131
+ # Gradio UI
132
+ with gr.Blocks() as demo:
133
+ gr.Markdown("## 🌐 Multilingual Translation App")
134
+
135
+ with gr.Row():
136
+ input_text = gr.Textbox(
137
+ label="Enter your text",
138
+ placeholder="Type here and press Enter or click Translate",
139
+ lines=3
140
+ )
141
+
142
+ with gr.Row():
143
+ lang_dropdown = gr.Dropdown(
144
+ choices=list(instructs.keys()),
145
+ value="kaz",
146
+ label="Translate to"
147
+ )
148
+
149
+ with gr.Accordion("Advanced Parameters (Optional)", open=False):
150
+ temperature_slider = gr.Slider(0.0, 1.0, value=0.0, step=0.01, label="Temperature")
151
+ top_p_slider = gr.Slider(0.0, 1.0, value=0.9, step=0.01, label="Top-p")
152
+ max_tokens_slider = gr.Slider(64, 2048, value=1024, step=1, label="Max Tokens")
153
+ presence_penalty_slider = gr.Slider(-2.0, 2.0, value=0.0, step=0.01, label="Presence Penalty", info="Usually left at 0.0")
154
+ frequency_penalty_slider = gr.Slider(-2.0, 2.0, value=0.3, step=0.01, label="Frequency Penalty")
155
+ repetition_penalty_slider = gr.Slider(0.5, 2.0, value=1.0, step=0.01, label="Repetition Penalty", info="Default is 1.0")
156
+ top_k_slider = gr.Slider(1, 100, value=64, step=1, label="Top-k", info="Default 64")
157
+ min_p_slider = gr.Slider(0.0, 1.0, value=0.0, step=0.01, label="Min-p", info="Usually 0.0")
158
+ use_v0_prompt = gr.Checkbox(label="Use v0 Prompt Format", value=False)
159
+
160
+ with gr.Row():
161
+ submit_btn = gr.Button("Translate", variant="primary")
162
+
163
+ output_text = gr.Textbox(label="Translation Result", lines=4)
164
+
165
+ # Define inputs in the correct order to match the function parameters
166
+ inputs = [
167
+ input_text, lang_dropdown,
168
+ temperature_slider, top_p_slider, max_tokens_slider,
169
+ presence_penalty_slider, frequency_penalty_slider, repetition_penalty_slider,
170
+ top_k_slider, min_p_slider, use_v0_prompt
171
+ ]
172
+
173
+ # Connect the function to the button and text input
174
+ submit_btn.click(fn=generate_translation, inputs=inputs, outputs=output_text)
175
+ input_text.submit(fn=generate_translation, inputs=inputs, outputs=output_text)
176
+
177
+ # Example inputs
178
+ gr.Markdown("### 🔍 Examples:")
179
+ with gr.Row():
180
+ examples = [
181
+ "Hello! How can I help you?",
182
+ "Hello! how can I help you?",
183
+ "2 + 2 is?",
184
+ "Your appointment is on 5th July at 3 PM.",
185
+ "The total cost is 1250 KZT.",
186
+ "She was born in 1995."
187
+ ]
188
+
189
+ for example_text in examples:
190
+ example_btn = gr.Button(example_text, size="sm")
191
+ example_btn.click(
192
+ fn=lambda x=example_text: x,
193
+ inputs=[],
194
+ outputs=[input_text]
195
+ )
196
+
197
+
198
+ if __name__ == "__main__":
199
+ demo.launch(server_name="0.0.0.0", server_port=5482, share=True)