Daemontatox commited on
Commit
1ab4db2
·
verified ·
1 Parent(s): 8c74e22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -64
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import re
3
  import cohere
@@ -6,7 +8,6 @@ from typing import Iterator, List, Tuple
6
 
7
  # Configuration Constants
8
  DEFAULT_SYSTEM_PROMPT = """
9
-
10
  أنت مترجم ثنائي اللغة متخصص في الترجمة بين العربية والإنجليزية. هدفك هو تقديم ترجمات دقيقة، ملائمة للسياق، ومتسقة من الناحية الأسلوبية، مع الالتزام بالإرشادات التالية:
11
  أسلوب الكتابة:
12
  1. الدقة النحوية: احرص دائمًا على أن تكون الترجمة صحيحة نحويًا.
@@ -39,17 +40,13 @@ DEFAULT_SYSTEM_PROMPT = """
39
  حافظ على تنسيق البيانات المهمة (مثل التواريخ، والقياسات، والاستشهادات القانونية).
40
  عند الشك:
41
  قدم الأولوية للوضوح، والتناسق، والملاءمة مع احتياجات الجمهور المستهدف. قم دائمًا بموازنة التعليمات الخاصة بالمشروع مع هذه الإرشادات، مع إعطاء الأولوية لمتطلبات العميل عند وجود أي تعارض.
42
-
43
-
44
-
45
- """ # Full prompt maintained as original
46
 
47
  TITLE = "<h1><center>Mawared T Assistant</center></h1>"
48
  PLACEHOLDER = "Ask me anything! I'll think through it step by step."
49
 
50
  CSS = """
51
-
52
- duplicate-button {
53
  margin: auto !important;
54
  color: white !important;
55
  background: black !important;
@@ -84,43 +81,56 @@ h3 {
84
  height: 500px !important;
85
  overflow-y: auto !important;
86
  }
87
-
88
  """
89
 
90
- # Precompile regex patterns for better performance
91
- TAG_PATTERNS = [
92
- (re.compile(r'<Thinking>'), '\n<Thinking>\n'),
93
- (re.compile(r'</Thinking>'), '\n</Thinking>\n'),
94
- (re.compile(r'<Critique>'), '\n<Critique>\n'),
95
- (re.compile(r'</Critique>'), '\n</Critique>\n'),
96
- (re.compile(r'<Revising>'), '\n<Revising>\n'),
97
- (re.compile(r'</Revising>'), '\n</Revising>\n'),
98
- (re.compile(r'<Final>'), '\n<Final>\n'),
99
- (re.compile(r'</Final>'), '\n</Final>\n')
100
- ]
101
-
102
  def format_text(text: str) -> str:
103
  """Format text with proper spacing and tag highlighting"""
 
 
 
 
 
 
 
 
 
 
 
104
  formatted = text
105
- for pattern, replacement in TAG_PATTERNS:
106
- formatted = pattern.sub(replacement, formatted)
107
- return '\n'.join(line for line in formatted.split('\n') if line.strip())
 
 
 
108
 
109
  def format_chat_history(history: List[List[str]]) -> str:
110
  """Format chat history for display"""
111
- return "\n\n".join(
112
- f"User: {user_msg}\nAssistant: {assistant_msg}"
113
- for user_msg, assistant_msg in history if user_msg
114
- )
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  def convert_history_to_cohere_format(history: List[List[str]]) -> List[dict]:
117
- """Convert chat history to Cohere's format with proper roles"""
118
  cohere_history = []
119
  for user_msg, assistant_msg in history:
120
  if user_msg:
121
- cohere_history.append({"role": "USER", "message": user_msg})
122
  if assistant_msg:
123
- cohere_history.append({"role": "CHATBOT", "message": assistant_msg})
124
  return cohere_history
125
 
126
  def chat_response(
@@ -135,42 +145,45 @@ def chat_response(
135
  penalty: float = 1.2,
136
  api_key: str = os.getenv("COHERE_API_KEY")
137
  ) -> Iterator[Tuple[List[List[str]], str]]:
138
- """Generate chat responses using Cohere API with error handling"""
139
- if not api_key:
140
- error_message = "Error: COHERE_API_KEY environment variable not set."
141
- history.append([message, error_message])
142
- yield history, format_chat_history(history)
143
- return
144
-
145
  co = cohere.Client(api_key=api_key)
 
 
146
  chat_history = convert_history_to_cohere_format(history)
 
 
147
  buffer = ""
148
- history.append([message, ""])
149
 
 
150
  try:
 
151
  stream = co.chat_stream(
152
  model='c4ai-aya-expanse-32b',
153
  message=message,
154
  temperature=temperature,
155
- max_tokens=max_new_tokens,
156
- p=top_p,
157
- k=top_k,
158
- frequency_penalty=penalty,
159
  chat_history=chat_history,
160
  prompt_truncation='AUTO',
161
  preamble=system_prompt
162
  )
163
-
164
  for event in stream:
165
  if event.event_type == "text-generation":
166
  buffer += event.text
167
- history[-1][1] = format_text(buffer)
168
- yield history, format_chat_history(history)
169
-
 
 
170
  except Exception as e:
171
  error_message = f"Error: {str(e)}"
172
  history[-1][1] = error_message
173
- yield history, format_chat_history(history)
 
 
 
 
 
174
 
175
  def main():
176
  """Main function to set up and launch the Gradio interface"""
@@ -187,19 +200,18 @@ def main():
187
  chat_display = gr.TextArea(
188
  value="",
189
  label="Chat History",
190
- interactive=False,
191
  elem_classes=["chat-area"],
192
  )
193
 
194
  message = gr.TextArea(
195
  placeholder=PLACEHOLDER,
196
  label="Your message",
197
- lines=3,
198
- elem_id="message_input"
199
  )
200
 
201
  with gr.Row():
202
- submit = gr.Button("Send", elem_id="submit_btn")
203
  clear = gr.Button("Clear")
204
 
205
  with gr.Accordion("⚙️ Advanced Settings", open=False):
@@ -243,32 +255,62 @@ def main():
243
  value=1.2,
244
  label="Repetition Penalty",
245
  )
246
-
247
- # Event handlers
248
- submit.click(
 
 
 
 
 
 
 
 
249
  chat_response,
250
- inputs=[message, chat_history, chat_display, system_prompt,
251
- temperature, max_tokens, top_p, top_k, penalty],
 
 
 
 
 
 
 
 
 
252
  outputs=[chat_history, chat_display],
253
  show_progress=True,
254
- ).then(lambda: "", outputs=message)
255
-
 
256
  message.submit(
257
  chat_response,
258
- inputs=[message, chat_history, chat_display, system_prompt,
259
- temperature, max_tokens, top_p, top_k, penalty],
 
 
 
 
 
 
 
 
 
260
  outputs=[chat_history, chat_display],
261
  show_progress=True,
262
- ).then(lambda: "", outputs=message)
263
-
264
  clear.click(
265
  lambda: ([], ""),
266
  outputs=[chat_history, chat_display],
267
  show_progress=True,
268
  )
269
-
270
- # Add JavaScript for Enter key handling
 
 
271
 
 
272
 
273
  if __name__ == "__main__":
274
  demo = main()
 
1
+ modify the code to allow the send button to work with enter and make other necessary optimizations.
2
+
3
  import os
4
  import re
5
  import cohere
 
8
 
9
  # Configuration Constants
10
  DEFAULT_SYSTEM_PROMPT = """
 
11
  أنت مترجم ثنائي اللغة متخصص في الترجمة بين العربية والإنجليزية. هدفك هو تقديم ترجمات دقيقة، ملائمة للسياق، ومتسقة من الناحية الأسلوبية، مع الالتزام بالإرشادات التالية:
12
  أسلوب الكتابة:
13
  1. الدقة النحوية: احرص دائمًا على أن تكون الترجمة صحيحة نحويًا.
 
40
  حافظ على تنسيق البيانات المهمة (مثل التواريخ، والقياسات، والاستشهادات القانونية).
41
  عند الشك:
42
  قدم الأولوية للوضوح، والتناسق، والملاءمة مع احتياجات الجمهور المستهدف. قم دائمًا بموازنة التعليمات الخاصة بالمشروع مع هذه الإرشادات، مع إعطاء الأولوية لمتطلبات العميل عند وجود أي تعارض.
43
+ """ # (keeping the full prompt as in original)
 
 
 
44
 
45
  TITLE = "<h1><center>Mawared T Assistant</center></h1>"
46
  PLACEHOLDER = "Ask me anything! I'll think through it step by step."
47
 
48
  CSS = """
49
+ .duplicate-button {
 
50
  margin: auto !important;
51
  color: white !important;
52
  background: black !important;
 
81
  height: 500px !important;
82
  overflow-y: auto !important;
83
  }
 
84
  """
85
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  def format_text(text: str) -> str:
87
  """Format text with proper spacing and tag highlighting"""
88
+ tag_patterns = [
89
+ (r'<Thinking>', '\n<Thinking>\n'),
90
+ (r'</Thinking>', '\n</Thinking>\n'),
91
+ (r'<Critique>', '\n<Critique>\n'),
92
+ (r'</Critique>', '\n</Critique>\n'),
93
+ (r'<Revising>', '\n<Revising>\n'),
94
+ (r'</Revising>', '\n</Revising>\n'),
95
+ (r'<Final>', '\n<Final>\n'),
96
+ (r'</Final>', '\n</Final>\n')
97
+ ]
98
+
99
  formatted = text
100
+ for pattern, replacement in tag_patterns:
101
+ formatted = re.sub(pattern, replacement, formatted)
102
+
103
+ formatted = '\n'.join(line for line in formatted.split('\n') if line.strip())
104
+
105
+ return formatted
106
 
107
  def format_chat_history(history: List[List[str]]) -> str:
108
  """Format chat history for display"""
109
+ formatted = []
110
+ for user_msg, assistant_msg in history:
111
+ formatted.append(f"User: {user_msg}")
112
+ if assistant_msg:
113
+ formatted.append(f"Assistant: {assistant_msg}")
114
+ return "\n\n".join(formatted)
115
+
116
+ def create_examples() -> List[str]:
117
+ """Create example queries for the UI"""
118
+ return [
119
+ "Explain the concept of artificial intelligence.",
120
+ "How does photosynthesis work?",
121
+ "What are the main causes of climate change?",
122
+ "Describe the process of protein synthesis.",
123
+ "What are the key features of a democratic government?",
124
+ ]
125
 
126
  def convert_history_to_cohere_format(history: List[List[str]]) -> List[dict]:
127
+ """Convert chat history to Cohere's format"""
128
  cohere_history = []
129
  for user_msg, assistant_msg in history:
130
  if user_msg:
131
+ cohere_history.append({"role": "User", "message": user_msg})
132
  if assistant_msg:
133
+ cohere_history.append({"role": "Chatbot", "message": assistant_msg})
134
  return cohere_history
135
 
136
  def chat_response(
 
145
  penalty: float = 1.2,
146
  api_key: str = os.getenv("COHERE_API_KEY")
147
  ) -> Iterator[Tuple[List[List[str]], str]]:
148
+ """Generate chat responses using Cohere API"""
 
 
 
 
 
 
149
  co = cohere.Client(api_key=api_key)
150
+
151
+ # Convert history to Cohere format
152
  chat_history = convert_history_to_cohere_format(history)
153
+
154
+ # Initialize buffer for streaming
155
  buffer = ""
156
+ history = history + [[message, ""]]
157
 
158
+ # Process stream
159
  try:
160
+ # Initialize stream
161
  stream = co.chat_stream(
162
  model='c4ai-aya-expanse-32b',
163
  message=message,
164
  temperature=temperature,
 
 
 
 
165
  chat_history=chat_history,
166
  prompt_truncation='AUTO',
167
  preamble=system_prompt
168
  )
169
+
170
  for event in stream:
171
  if event.event_type == "text-generation":
172
  buffer += event.text
173
+ formatted_buffer = format_text(buffer)
174
+ history[-1][1] = formatted_buffer
175
+ chat_display = format_chat_history(history)
176
+ yield history, chat_display
177
+
178
  except Exception as e:
179
  error_message = f"Error: {str(e)}"
180
  history[-1][1] = error_message
181
+ chat_display = format_chat_history(history)
182
+ yield history, chat_display
183
+
184
+ def process_example(example: str) -> tuple:
185
+ """Process example query and return empty history and updated display"""
186
+ return [], f"User: {example}\n\n"
187
 
188
  def main():
189
  """Main function to set up and launch the Gradio interface"""
 
200
  chat_display = gr.TextArea(
201
  value="",
202
  label="Chat History",
203
+ interactive=AttributeError,
204
  elem_classes=["chat-area"],
205
  )
206
 
207
  message = gr.TextArea(
208
  placeholder=PLACEHOLDER,
209
  label="Your message",
210
+ lines=5
 
211
  )
212
 
213
  with gr.Row():
214
+ submit = gr.Button("Send")
215
  clear = gr.Button("Clear")
216
 
217
  with gr.Accordion("⚙️ Advanced Settings", open=False):
 
255
  value=1.2,
256
  label="Repetition Penalty",
257
  )
258
+
259
+ examples = gr.Examples(
260
+ examples=create_examples(),
261
+ inputs=[message],
262
+ outputs=[chat_history, chat_display],
263
+ fn=process_example,
264
+ cache_examples=False,
265
+ )
266
+
267
+ # Set up event handlers
268
+ submit_click = submit.click(
269
  chat_response,
270
+ inputs=[
271
+ message,
272
+ chat_history,
273
+ chat_display,
274
+ system_prompt,
275
+ temperature,
276
+ max_tokens,
277
+ top_p,
278
+ top_k,
279
+ penalty,
280
+ ],
281
  outputs=[chat_history, chat_display],
282
  show_progress=True,
283
+ )
284
+
285
+ # Bind Enter key to submit
286
  message.submit(
287
  chat_response,
288
+ inputs=[
289
+ message,
290
+ chat_history,
291
+ chat_display,
292
+ system_prompt,
293
+ temperature,
294
+ max_tokens,
295
+ top_p,
296
+ top_k,
297
+ penalty,
298
+ ],
299
  outputs=[chat_history, chat_display],
300
  show_progress=True,
301
+ )
302
+
303
  clear.click(
304
  lambda: ([], ""),
305
  outputs=[chat_history, chat_display],
306
  show_progress=True,
307
  )
308
+
309
+ # Clear input after submission
310
+ submit_click.then(lambda: "", outputs=message)
311
+ message.submit(lambda: "", outputs=message)
312
 
313
+ return demo
314
 
315
  if __name__ == "__main__":
316
  demo = main()