Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -278,9 +278,6 @@ def respond(
|
|
| 278 |
if custom_response:
|
| 279 |
yield custom_response
|
| 280 |
return
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
if is_image_request(message):
|
| 285 |
try:
|
| 286 |
image = generate_image(message)
|
|
@@ -290,17 +287,15 @@ def respond(
|
|
| 290 |
return "Sorry, I couldn't generate the image. Please try again."
|
| 291 |
except Exception as e:
|
| 292 |
return f"An error occurred while generating the image: {str(e)}"
|
| 293 |
-
|
| 294 |
# Handle translation with more conservative approach
|
| 295 |
translated_msg, original_lang, was_transliterated = translate_text(message)
|
| 296 |
-
|
| 297 |
# Prepare conversation history - only translate if necessary
|
| 298 |
messages = [{"role": "system", "content": system_message}]
|
| 299 |
for val in history:
|
| 300 |
if val[0]:
|
| 301 |
# Only translate longer messages
|
| 302 |
if len(val[0].split()) > 2:
|
| 303 |
-
trans_user_msg,
|
| 304 |
messages.append({"role": "user", "content": trans_user_msg})
|
| 305 |
else:
|
| 306 |
messages.append({"role": "user", "content": val[0]})
|
|
@@ -308,7 +303,6 @@ def respond(
|
|
| 308 |
messages.append({"role": "assistant", "content": val[1]})
|
| 309 |
|
| 310 |
messages.append({"role": "user", "content": translated_msg})
|
| 311 |
-
|
| 312 |
# Get response from model
|
| 313 |
response = ""
|
| 314 |
for message in text_client.chat_completion(
|
|
@@ -320,7 +314,7 @@ def respond(
|
|
| 320 |
):
|
| 321 |
token = message.choices[0].delta.content
|
| 322 |
response += token
|
| 323 |
-
|
| 324 |
# Only translate back if the original was definitely non-English
|
| 325 |
if original_lang != 'en' and len(message.split()) > 2:
|
| 326 |
try:
|
|
@@ -332,7 +326,10 @@ def respond(
|
|
| 332 |
else:
|
| 333 |
yield response
|
| 334 |
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
| 336 |
demo = gr.ChatInterface(
|
| 337 |
respond,
|
| 338 |
additional_inputs=[
|
|
@@ -361,8 +358,9 @@ demo = gr.ChatInterface(
|
|
| 361 |
step=0.05,
|
| 362 |
label="Top-p (nucleus sampling)"
|
| 363 |
),
|
| 364 |
-
]
|
|
|
|
| 365 |
)
|
| 366 |
|
| 367 |
if __name__ == "__main__":
|
| 368 |
-
demo.launch(share=True)
|
|
|
|
| 278 |
if custom_response:
|
| 279 |
yield custom_response
|
| 280 |
return
|
|
|
|
|
|
|
|
|
|
| 281 |
if is_image_request(message):
|
| 282 |
try:
|
| 283 |
image = generate_image(message)
|
|
|
|
| 287 |
return "Sorry, I couldn't generate the image. Please try again."
|
| 288 |
except Exception as e:
|
| 289 |
return f"An error occurred while generating the image: {str(e)}"
|
|
|
|
| 290 |
# Handle translation with more conservative approach
|
| 291 |
translated_msg, original_lang, was_transliterated = translate_text(message)
|
|
|
|
| 292 |
# Prepare conversation history - only translate if necessary
|
| 293 |
messages = [{"role": "system", "content": system_message}]
|
| 294 |
for val in history:
|
| 295 |
if val[0]:
|
| 296 |
# Only translate longer messages
|
| 297 |
if len(val[0].split()) > 2:
|
| 298 |
+
trans_user_msg, *, * = translate_text(val[0])
|
| 299 |
messages.append({"role": "user", "content": trans_user_msg})
|
| 300 |
else:
|
| 301 |
messages.append({"role": "user", "content": val[0]})
|
|
|
|
| 303 |
messages.append({"role": "assistant", "content": val[1]})
|
| 304 |
|
| 305 |
messages.append({"role": "user", "content": translated_msg})
|
|
|
|
| 306 |
# Get response from model
|
| 307 |
response = ""
|
| 308 |
for message in text_client.chat_completion(
|
|
|
|
| 314 |
):
|
| 315 |
token = message.choices[0].delta.content
|
| 316 |
response += token
|
| 317 |
+
yield response
|
| 318 |
# Only translate back if the original was definitely non-English
|
| 319 |
if original_lang != 'en' and len(message.split()) > 2:
|
| 320 |
try:
|
|
|
|
| 326 |
else:
|
| 327 |
yield response
|
| 328 |
|
| 329 |
+
def display_response(response):
|
| 330 |
+
print(response)
|
| 331 |
+
|
| 332 |
+
# Updated Gradio interface to handle images and display the response
|
| 333 |
demo = gr.ChatInterface(
|
| 334 |
respond,
|
| 335 |
additional_inputs=[
|
|
|
|
| 358 |
step=0.05,
|
| 359 |
label="Top-p (nucleus sampling)"
|
| 360 |
),
|
| 361 |
+
],
|
| 362 |
+
output_component=gr.Output(type="text", label="Response")
|
| 363 |
)
|
| 364 |
|
| 365 |
if __name__ == "__main__":
|
| 366 |
+
demo.launch(share=True)
|