Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -584,36 +584,39 @@ async def health_check():
|
|
| 584 |
@app.post("/ask")
|
| 585 |
async def chat(query: ChatQuery):
|
| 586 |
try:
|
|
|
|
|
|
|
|
|
|
| 587 |
# Handle Arabic input
|
| 588 |
if query.language_code == 0:
|
| 589 |
# Translate question from Arabic to English
|
| 590 |
english_query = translate_ar_to_en(query.query)
|
| 591 |
if not english_query:
|
| 592 |
raise HTTPException(status_code=500, detail="Failed to translate question from Arabic to English")
|
| 593 |
-
|
| 594 |
-
#
|
| 595 |
-
english_response = get_completion(english_query)
|
| 596 |
-
|
| 597 |
# Translate response back to Arabic
|
| 598 |
arabic_response = translate_en_to_ar(english_response)
|
| 599 |
if not arabic_response:
|
| 600 |
raise HTTPException(status_code=500, detail="Failed to translate response to Arabic")
|
| 601 |
-
|
| 602 |
return {
|
| 603 |
"original_query": query.query,
|
| 604 |
"translated_query": english_query,
|
| 605 |
"response": arabic_response,
|
| 606 |
"response_in_english": english_response
|
| 607 |
}
|
| 608 |
-
|
| 609 |
# Handle English input
|
| 610 |
else:
|
| 611 |
-
response = get_completion(query.query)
|
| 612 |
return {
|
| 613 |
"query": query.query,
|
| 614 |
"response": response
|
| 615 |
}
|
| 616 |
-
|
| 617 |
except HTTPException as e:
|
| 618 |
raise e
|
| 619 |
except Exception as e:
|
|
|
|
| 584 |
@app.post("/ask")
|
| 585 |
async def chat(query: ChatQuery):
|
| 586 |
try:
|
| 587 |
+
# Define constraints
|
| 588 |
+
constraints = "Provide a medically reliable answer in no more than 250 words."
|
| 589 |
+
|
| 590 |
# Handle Arabic input
|
| 591 |
if query.language_code == 0:
|
| 592 |
# Translate question from Arabic to English
|
| 593 |
english_query = translate_ar_to_en(query.query)
|
| 594 |
if not english_query:
|
| 595 |
raise HTTPException(status_code=500, detail="Failed to translate question from Arabic to English")
|
| 596 |
+
|
| 597 |
+
# Modify the prompt with constraints
|
| 598 |
+
english_response = get_completion(f"{english_query} {constraints}")
|
| 599 |
+
|
| 600 |
# Translate response back to Arabic
|
| 601 |
arabic_response = translate_en_to_ar(english_response)
|
| 602 |
if not arabic_response:
|
| 603 |
raise HTTPException(status_code=500, detail="Failed to translate response to Arabic")
|
| 604 |
+
|
| 605 |
return {
|
| 606 |
"original_query": query.query,
|
| 607 |
"translated_query": english_query,
|
| 608 |
"response": arabic_response,
|
| 609 |
"response_in_english": english_response
|
| 610 |
}
|
| 611 |
+
|
| 612 |
# Handle English input
|
| 613 |
else:
|
| 614 |
+
response = get_completion(f"{query.query} {constraints}")
|
| 615 |
return {
|
| 616 |
"query": query.query,
|
| 617 |
"response": response
|
| 618 |
}
|
| 619 |
+
|
| 620 |
except HTTPException as e:
|
| 621 |
raise e
|
| 622 |
except Exception as e:
|