Update main.py
Browse filesfollow up questions removed. other prompt changes
main.py
CHANGED
|
@@ -2,8 +2,14 @@ import os
|
|
| 2 |
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/transformers"
|
| 3 |
os.environ["HF_HOME"] = "/app/.cache/huggingface"
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
from pydub import AudioSegment
|
|
|
|
| 6 |
import uvicorn
|
|
|
|
|
|
|
| 7 |
from fastapi import FastAPI, File, UploadFile
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -17,15 +23,19 @@ import json
|
|
| 17 |
import pandas as pd
|
| 18 |
import asyncio
|
| 19 |
import numpy as np
|
|
|
|
| 20 |
from fastapi.staticfiles import StaticFiles
|
| 21 |
from fastapi.responses import HTMLResponse
|
| 22 |
import openai as _openai_mod
|
| 23 |
import requests
|
| 24 |
|
| 25 |
import time
|
|
|
|
| 26 |
from fastapi import UploadFile, File
|
| 27 |
from starlette.responses import StreamingResponse
|
| 28 |
from pydub import AudioSegment
|
|
|
|
|
|
|
| 29 |
from openai import OpenAI
|
| 30 |
load_dotenv()
|
| 31 |
|
|
@@ -52,24 +62,21 @@ async def serve_html():
|
|
| 52 |
|
| 53 |
|
| 54 |
chat_messages = [{"role": "system", "content": '''
|
| 55 |
-
You are Kammi, a friendly, medical assistant specializing in orthopedic surgery, human-like voice assistant built by Facile AI Solutions
|
| 56 |
You assist customers specifically with knee replacement surgery queries and you are the assistant of Dr.Sandeep, a highly experienced knee replacement surgeon.
|
| 57 |
|
| 58 |
Rules for your responses:
|
| 59 |
|
| 60 |
-
1.
|
| 61 |
-
|
| 62 |
-
2. *General conversation*: Engage in greetings and casual conversation. If the user mentions their name, greet them personally using their name, in Telugu.
|
| 63 |
|
| 64 |
-
|
| 65 |
-
Answer in *Telugu* based on the context or chat history.
|
| 66 |
|
| 67 |
-
|
| 68 |
-
- If the question is
|
|
|
|
| 69 |
|
| 70 |
-
- Translate medical and technical terms into simple, **understandable
|
| 71 |
-
- The output must be in Telugu script, but common English medical or technical terms (like knee, hip, surgery, replacement, physiotherapy, arthritis, etc.)
|
| 72 |
-
should be written phonetically in Telugu, not translated literally.
|
| 73 |
|
| 74 |
Example:
|
| 75 |
“knee replacement” → మోకాలు రీప్లేస్మెంట్
|
|
@@ -78,41 +85,32 @@ chat_messages = [{"role": "system", "content": '''
|
|
| 78 |
“physiotherapy” → ఫిజియోథెరపీ
|
| 79 |
Ensure the language sounds simple, natural, and conversational for Telugu-speaking patients.
|
| 80 |
|
| 81 |
-
4.
|
| 82 |
- If the question is completely unrelated to knee replacement surgery, politely decline in Telugu: "నేను కేవలం మోకాలు రీప్లేస్మెంట్ సర్జరీ సంబంధిత ప్రశ్నలకు సహాయం చేస్తాను."
|
| 83 |
|
| 84 |
-
5.
|
| 85 |
-
- After answering the user’s question, suggest a follow-up question from the context that you can answer, in *Telugu*
|
| 86 |
-
- Make the follow-up natural and conversational. The follow up question must be relevant to the current question or response in *Telugu*
|
| 87 |
-
- Do not include redundant statements like “మీరు మరింత సమాచారం కావాలనుకుంటే…”, "మీరు మరేదైనా అడగాలనుకుంటున్నారా" in the follow up questions
|
| 88 |
-
- If the user responds with confirmation like “yes”, “okay” give the answer for the previous follow-up question from the context.
|
| 89 |
-
- If the user ends the conversation, do not ask or suggest any follow-up question.
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
6. *Readable voice output for gTTS*:
|
| 93 |
- Break sentences at natural punctuation: , . ? ! : ;
|
| 94 |
- Do not use #, **, or other markdown symbols.
|
| 95 |
Telugu Output Guidelines:
|
| 96 |
-
All numbers, decimals, and points
|
| 97 |
Example: 2.5 lakh → రెండు లక్షల యాభై వేల రూపాయలు
|
| 98 |
|
| 99 |
-
|
| 100 |
-
- Keep answers short, conversational, and natural
|
| 101 |
- Maximum 40 words / ~20 seconds of speech.
|
| 102 |
|
| 103 |
-
|
| 104 |
- Helpful, friendly, approachable, and human-like.
|
| 105 |
- Maintain professionalism while being conversational.
|
| 106 |
|
| 107 |
-
|
| 108 |
-
- ALL the below points must be in *Telugu*
|
| 109 |
- Over 5 years of experience in orthopedic and joint replacement surgery.
|
| 110 |
- Specializes in total and partial knee replacement procedures.
|
| 111 |
- Known for a patient-friendly approach, focusing on pre-surgery preparation, post-surgery rehabilitation, and pain management.
|
| 112 |
- Actively keeps up-to-date with the latest techniques and technologies in knee replacement surgery.
|
| 113 |
- Highly approachable and prefers that patients are well-informed about their treatment options and recovery process.
|
| 114 |
|
| 115 |
-
Always provide readable, streaming-friendly sentences in
|
| 116 |
'''}]
|
| 117 |
|
| 118 |
class BM25:
|
|
@@ -204,7 +202,9 @@ async def retrieve_top_k_hybrid(query, k, sem_weight,syn_weight,bm25):
|
|
| 204 |
|
| 205 |
top_results = df_expanded.sort_values(by="similarity", ascending=False).head(k)
|
| 206 |
|
| 207 |
-
#
|
|
|
|
|
|
|
| 208 |
return top_results["telugu_chunk"].to_list()
|
| 209 |
|
| 210 |
|
|
@@ -289,7 +289,8 @@ def tts_chunk_stream(text_chunk: str, lang: str = "en"):
|
|
| 289 |
"model": model,
|
| 290 |
"voice": voice,
|
| 291 |
"input": text_chunk,
|
| 292 |
-
"format": fmt
|
|
|
|
| 293 |
# "instructions" : "speak in cheerfull and positive tone"
|
| 294 |
# optional: "language": language_code # include if needed by API variation
|
| 295 |
}
|
|
@@ -328,25 +329,23 @@ def tts_chunk_stream(text_chunk: str, lang: str = "en"):
|
|
| 328 |
return audio_stream()
|
| 329 |
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
async def get_rag_response(user_message: str):
|
| 334 |
global chat_messages
|
| 335 |
-
Chunks = await retrieve_top_k_hybrid(
|
| 336 |
context = "======================================================================================================\n".join(map(str,Chunks))
|
| 337 |
chat_messages.append({"role": "user", "content": f'''
|
| 338 |
Context : {context}
|
| 339 |
-
User Query: {
|
| 340 |
# print("chat_messages",chat_messages)
|
| 341 |
-
return chat_messages
|
| 342 |
|
| 343 |
|
| 344 |
# --- GPT + TTS async generator with smaller buffer like second code ---
|
| 345 |
-
async def gpt_tts_stream(prompt: str):
|
| 346 |
# start_time = time.time()
|
| 347 |
# print("started gpt_tts_stream",prompt)
|
| 348 |
global chat_messages
|
| 349 |
-
chat_messages = await get_rag_response(prompt)
|
| 350 |
# print(chat_messages,"chat_messages after getting RAG response")
|
| 351 |
|
| 352 |
# response = openai.ChatCompletion.create(
|
|
@@ -361,7 +360,7 @@ async def gpt_tts_stream(prompt: str):
|
|
| 361 |
|
| 362 |
# ✅ Must use the `with` block for streaming
|
| 363 |
with client.chat.completions.stream(
|
| 364 |
-
model="gpt-4o
|
| 365 |
messages=chat_messages,
|
| 366 |
) as stream:
|
| 367 |
|
|
@@ -371,8 +370,8 @@ async def gpt_tts_stream(prompt: str):
|
|
| 371 |
bot_response = bot_response + delta
|
| 372 |
buffer += delta
|
| 373 |
if len(buffer) >= buffer_size and buffer.endswith((".", "!", ",", "?", "\n", ";", ":")):
|
| 374 |
-
print("the buffer is ")
|
| 375 |
-
print(buffer)
|
| 376 |
# audio_chunks = tts_chunk_stream(buffer)
|
| 377 |
for audio_chunk in tts_chunk_stream(buffer):
|
| 378 |
# print("chunk",buffer)
|
|
@@ -380,9 +379,10 @@ async def gpt_tts_stream(prompt: str):
|
|
| 380 |
buffer = ""
|
| 381 |
|
| 382 |
elif event.type == "content.done":
|
|
|
|
| 383 |
# 🧾 model finished — flush whatever is left
|
| 384 |
if buffer.strip():
|
| 385 |
-
print("the left over message")
|
| 386 |
print(buffer.strip())
|
| 387 |
for audio_chunk in tts_chunk_stream(buffer):
|
| 388 |
# print("chunk",buffer)
|
|
@@ -391,6 +391,10 @@ async def gpt_tts_stream(prompt: str):
|
|
| 391 |
|
| 392 |
|
| 393 |
bot_response = bot_response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
chat_messages.append({"role": "assistant", "content": bot_response})
|
| 395 |
|
| 396 |
|
|
@@ -477,7 +481,9 @@ async def chat_stream(file: UploadFile = File(...)):
|
|
| 477 |
|
| 478 |
transcription = client.audio.transcriptions.create(
|
| 479 |
model="gpt-4o-transcribe", # or "gpt-4o-mini-transcribe"
|
| 480 |
-
file=(file.filename, audio_bytes) # important: (filename, bytes)
|
|
|
|
|
|
|
| 481 |
)
|
| 482 |
|
| 483 |
telugu_text = transcription.text
|
|
@@ -488,42 +494,37 @@ async def chat_stream(file: UploadFile = File(...)):
|
|
| 488 |
model="gpt-4o-mini",
|
| 489 |
temperature = 0,
|
| 490 |
top_p = 0,
|
| 491 |
-
input=f'''Translate the following Telugu
|
| 492 |
{telugu_text}
|
| 493 |
-
Give only the english translation,
|
| 494 |
|
| 495 |
english_text = translation.output[0].content[0].text
|
| 496 |
print(f"translation time {time.time() - start_time}")
|
| 497 |
print(f"the english text is {english_text}")
|
| 498 |
|
| 499 |
-
return StreamingResponse(gpt_tts_stream(english_text), media_type="audio/mpeg")
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
@app.post("/reset_chat")
|
| 504 |
async def reset_chat():
|
| 505 |
global chat_messages
|
| 506 |
-
chat_messages = [{
|
| 507 |
-
|
| 508 |
-
"content": '''
|
| 509 |
-
You are Kammi, a friendly, medical assistant specializing in orthopedic surgery, human-like voice assistant built by Facile AI Solutions
|
| 510 |
You assist customers specifically with knee replacement surgery queries and you are the assistant of Dr.Sandeep, a highly experienced knee replacement surgeon.
|
| 511 |
|
| 512 |
Rules for your responses:
|
| 513 |
|
| 514 |
-
1.
|
| 515 |
-
|
| 516 |
-
2. *General conversation*: Engage in greetings and casual conversation. If the user mentions their name, greet them personally using their name, in Telugu.
|
| 517 |
|
| 518 |
-
|
| 519 |
-
Answer in *Telugu* based on the context or chat history.
|
| 520 |
|
| 521 |
-
|
| 522 |
-
- If the question is
|
|
|
|
| 523 |
|
| 524 |
-
- Translate medical and technical terms into simple, **understandable
|
| 525 |
-
- The output must be in Telugu script, but common English medical or technical terms (like knee, hip, surgery, replacement, physiotherapy, arthritis, etc.)
|
| 526 |
-
should be written phonetically in Telugu, not translated literally.
|
| 527 |
|
| 528 |
Example:
|
| 529 |
“knee replacement” → మోకాలు రీప్లేస్మెంట్
|
|
@@ -532,41 +533,32 @@ async def reset_chat():
|
|
| 532 |
“physiotherapy” → ఫిజియోథెరపీ
|
| 533 |
Ensure the language sounds simple, natural, and conversational for Telugu-speaking patients.
|
| 534 |
|
| 535 |
-
4.
|
| 536 |
- If the question is completely unrelated to knee replacement surgery, politely decline in Telugu: "నేను కేవలం మోకాలు రీప్లేస్మెంట్ సర్జరీ సంబంధిత ప్రశ్నలకు సహాయం చేస్తాను."
|
| 537 |
|
| 538 |
-
5.
|
| 539 |
-
- After answering the user’s question, suggest a follow-up question from the context that you can answer, in *Telugu*
|
| 540 |
-
- Make the follow-up natural and conversational. The follow up question must be relevant to the current question or response in *Telugu*
|
| 541 |
-
- Do not include redundant statements like “మీరు మరింత సమాచారం కావాలనుకుంటే…”, "మీరు మరేదైనా అడగాలనుకుంటున్నారా" in the follow up questions
|
| 542 |
-
- If the user responds with confirmation like “yes”, “okay” give the answer for the previous follow-up question from the context.
|
| 543 |
-
- If the user ends the conversation, do not ask or suggest any follow-up question.
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
6. *Readable voice output for gTTS*:
|
| 547 |
- Break sentences at natural punctuation: , . ? ! : ;
|
| 548 |
- Do not use #, **, or other markdown symbols.
|
| 549 |
Telugu Output Guidelines:
|
| 550 |
-
All numbers, decimals, and points
|
| 551 |
Example: 2.5 lakh → రెండు లక్షల యాభై వేల రూపాయలు
|
| 552 |
|
| 553 |
-
|
| 554 |
-
- Keep answers short, conversational, and natural
|
| 555 |
- Maximum 40 words / ~20 seconds of speech.
|
| 556 |
|
| 557 |
-
|
| 558 |
- Helpful, friendly, approachable, and human-like.
|
| 559 |
- Maintain professionalism while being conversational.
|
| 560 |
|
| 561 |
-
|
| 562 |
-
- ALL the below points must be in *Telugu*
|
| 563 |
- Over 5 years of experience in orthopedic and joint replacement surgery.
|
| 564 |
- Specializes in total and partial knee replacement procedures.
|
| 565 |
- Known for a patient-friendly approach, focusing on pre-surgery preparation, post-surgery rehabilitation, and pain management.
|
| 566 |
- Actively keeps up-to-date with the latest techniques and technologies in knee replacement surgery.
|
| 567 |
- Highly approachable and prefers that patients are well-informed about their treatment options and recovery process.
|
| 568 |
|
| 569 |
-
Always provide readable, streaming-friendly sentences in
|
| 570 |
-
'''
|
| 571 |
-
|
| 572 |
return {"message": "Chat history reset successfully."}
|
|
|
|
| 2 |
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/transformers"
|
| 3 |
os.environ["HF_HOME"] = "/app/.cache/huggingface"
|
| 4 |
|
| 5 |
+
|
| 6 |
+
from google.cloud import speech_v1p1beta1 as speech
|
| 7 |
+
from google.cloud import translate_v2 as translate
|
| 8 |
from pydub import AudioSegment
|
| 9 |
+
import wave
|
| 10 |
import uvicorn
|
| 11 |
+
|
| 12 |
+
|
| 13 |
from fastapi import FastAPI, File, UploadFile
|
| 14 |
from fastapi.responses import StreamingResponse
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 23 |
import pandas as pd
|
| 24 |
import asyncio
|
| 25 |
import numpy as np
|
| 26 |
+
from deepgram import Deepgram
|
| 27 |
from fastapi.staticfiles import StaticFiles
|
| 28 |
from fastapi.responses import HTMLResponse
|
| 29 |
import openai as _openai_mod
|
| 30 |
import requests
|
| 31 |
|
| 32 |
import time
|
| 33 |
+
import wave
|
| 34 |
from fastapi import UploadFile, File
|
| 35 |
from starlette.responses import StreamingResponse
|
| 36 |
from pydub import AudioSegment
|
| 37 |
+
from google.cloud import speech_v1p1beta1 as speech
|
| 38 |
+
from google.cloud import translate_v2 as translate
|
| 39 |
from openai import OpenAI
|
| 40 |
load_dotenv()
|
| 41 |
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
chat_messages = [{"role": "system", "content": '''
|
| 65 |
+
Your task is to answer the user queries in **telugu language**(I mean telugu characters). You are Kammi, a friendly, medical assistant specializing in orthopedic surgery, human-like voice assistant built by Facile AI Solutions
|
| 66 |
You assist customers specifically with knee replacement surgery queries and you are the assistant of Dr.Sandeep, a highly experienced knee replacement surgeon.
|
| 67 |
|
| 68 |
Rules for your responses:
|
| 69 |
|
| 70 |
+
1. **Context-driven answers only**: Answer strictly based on the provided context and previous conversation history. Do not use external knowledge. Respond in **Telugu** language. The user only understands telugu not English.
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
2. **General conversation**: Engage in greetings and casual conversation. If the user mentions their name, greet them personally using their name
|
|
|
|
| 73 |
|
| 74 |
+
3. **Technical/medical queries**:
|
| 75 |
+
- If the question is relevant to knee replacement surgery and the answer is in the context or chat history, provide the answer.
|
| 76 |
+
- If the question is relevant but not present in the context, respond: "దయచేసి డాక్టర్ సందీప్ లేదా రిసెప్షన్ ను సంప్రదించండి."
|
| 77 |
|
| 78 |
+
- Translate medical and technical terms into simple, **understandable words in Telugu** wherever possible.
|
| 79 |
+
- The output must be in Telugu script, but common English medical or technical terms (like knee, hip, surgery, replacement, physiotherapy, arthritis, etc.) should be transliterated in Telugu.
|
|
|
|
| 80 |
|
| 81 |
Example:
|
| 82 |
“knee replacement” → మోకాలు రీప్లేస్మెంట్
|
|
|
|
| 85 |
“physiotherapy” → ఫిజియోథెరపీ
|
| 86 |
Ensure the language sounds simple, natural, and conversational for Telugu-speaking patients.
|
| 87 |
|
| 88 |
+
4. **Irrelevant queries**:
|
| 89 |
- If the question is completely unrelated to knee replacement surgery, politely decline in Telugu: "నేను కేవలం మోకాలు రీప్లేస్మెంట్ సర్జరీ సంబంధిత ప్రశ్నలకు సహాయం చేస్తాను."
|
| 90 |
|
| 91 |
+
5. **Readable voice output**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
- Break sentences at natural punctuation: , . ? ! : ;
|
| 93 |
- Do not use #, **, or other markdown symbols.
|
| 94 |
Telugu Output Guidelines:
|
| 95 |
+
All numbers, decimals, and points MUST be fully spelled out in Telugu words.
|
| 96 |
Example: 2.5 lakh → రెండు లక్షల యాభై వేల రూపాయలు
|
| 97 |
|
| 98 |
+
6. **Concise and human-like**:
|
| 99 |
+
- Keep answers short, conversational, and natural
|
| 100 |
- Maximum 40 words / ~20 seconds of speech.
|
| 101 |
|
| 102 |
+
7. **Tone and style**:
|
| 103 |
- Helpful, friendly, approachable, and human-like.
|
| 104 |
- Maintain professionalism while being conversational.
|
| 105 |
|
| 106 |
+
8. **About Dr.Sandeep**:
|
|
|
|
| 107 |
- Over 5 years of experience in orthopedic and joint replacement surgery.
|
| 108 |
- Specializes in total and partial knee replacement procedures.
|
| 109 |
- Known for a patient-friendly approach, focusing on pre-surgery preparation, post-surgery rehabilitation, and pain management.
|
| 110 |
- Actively keeps up-to-date with the latest techniques and technologies in knee replacement surgery.
|
| 111 |
- Highly approachable and prefers that patients are well-informed about their treatment options and recovery process.
|
| 112 |
|
| 113 |
+
Always provide readable, streaming-friendly sentences in **Telugu** language so that output is read smoothly. Drive conversation forward while staying strictly on knee replacement surgery topics, and suggest follow-up questions for which you have context-based answers.
|
| 114 |
'''}]
|
| 115 |
|
| 116 |
class BM25:
|
|
|
|
| 202 |
|
| 203 |
top_results = df_expanded.sort_values(by="similarity", ascending=False).head(k)
|
| 204 |
|
| 205 |
+
# print("the retrieved chunks are")
|
| 206 |
+
# print(top_results["telugu_chunk"].to_list()[0])
|
| 207 |
+
|
| 208 |
return top_results["telugu_chunk"].to_list()
|
| 209 |
|
| 210 |
|
|
|
|
| 289 |
"model": model,
|
| 290 |
"voice": voice,
|
| 291 |
"input": text_chunk,
|
| 292 |
+
"format": fmt,
|
| 293 |
+
"temperature" : 0
|
| 294 |
# "instructions" : "speak in cheerfull and positive tone"
|
| 295 |
# optional: "language": language_code # include if needed by API variation
|
| 296 |
}
|
|
|
|
| 329 |
return audio_stream()
|
| 330 |
|
| 331 |
|
| 332 |
+
async def get_rag_response(user_message_english: str, user_message_telugu: str):
|
|
|
|
|
|
|
| 333 |
global chat_messages
|
| 334 |
+
Chunks = await retrieve_top_k_hybrid(user_message_english,15, 0.9, 0.1,bm25)
|
| 335 |
context = "======================================================================================================\n".join(map(str,Chunks))
|
| 336 |
chat_messages.append({"role": "user", "content": f'''
|
| 337 |
Context : {context}
|
| 338 |
+
User Query: {user_message_telugu}'''})
|
| 339 |
# print("chat_messages",chat_messages)
|
| 340 |
+
return [chat_messages[0]]+chat_messages[-7:]
|
| 341 |
|
| 342 |
|
| 343 |
# --- GPT + TTS async generator with smaller buffer like second code ---
|
| 344 |
+
async def gpt_tts_stream(prompt: str,telugu_text: str):
|
| 345 |
# start_time = time.time()
|
| 346 |
# print("started gpt_tts_stream",prompt)
|
| 347 |
global chat_messages
|
| 348 |
+
chat_messages = await get_rag_response(prompt,telugu_text)
|
| 349 |
# print(chat_messages,"chat_messages after getting RAG response")
|
| 350 |
|
| 351 |
# response = openai.ChatCompletion.create(
|
|
|
|
| 360 |
|
| 361 |
# ✅ Must use the `with` block for streaming
|
| 362 |
with client.chat.completions.stream(
|
| 363 |
+
model="gpt-4o",
|
| 364 |
messages=chat_messages,
|
| 365 |
) as stream:
|
| 366 |
|
|
|
|
| 370 |
bot_response = bot_response + delta
|
| 371 |
buffer += delta
|
| 372 |
if len(buffer) >= buffer_size and buffer.endswith((".", "!", ",", "?", "\n", ";", ":")):
|
| 373 |
+
# print("the buffer is ")
|
| 374 |
+
# print(buffer)
|
| 375 |
# audio_chunks = tts_chunk_stream(buffer)
|
| 376 |
for audio_chunk in tts_chunk_stream(buffer):
|
| 377 |
# print("chunk",buffer)
|
|
|
|
| 379 |
buffer = ""
|
| 380 |
|
| 381 |
elif event.type == "content.done":
|
| 382 |
+
fll_response = event.content
|
| 383 |
# 🧾 model finished — flush whatever is left
|
| 384 |
if buffer.strip():
|
| 385 |
+
# print("the left over message")
|
| 386 |
print(buffer.strip())
|
| 387 |
for audio_chunk in tts_chunk_stream(buffer):
|
| 388 |
# print("chunk",buffer)
|
|
|
|
| 391 |
|
| 392 |
|
| 393 |
bot_response = bot_response.strip()
|
| 394 |
+
# print("the final bot response :")
|
| 395 |
+
# print(bot_response)
|
| 396 |
+
# print("full repsones is")
|
| 397 |
+
# print(fll_response)
|
| 398 |
chat_messages.append({"role": "assistant", "content": bot_response})
|
| 399 |
|
| 400 |
|
|
|
|
| 481 |
|
| 482 |
transcription = client.audio.transcriptions.create(
|
| 483 |
model="gpt-4o-transcribe", # or "gpt-4o-mini-transcribe"
|
| 484 |
+
file=(file.filename, audio_bytes), # important: (filename, bytes)
|
| 485 |
+
language="te",
|
| 486 |
+
prompt="Medical terms related to knee replacement surgery"
|
| 487 |
)
|
| 488 |
|
| 489 |
telugu_text = transcription.text
|
|
|
|
| 494 |
model="gpt-4o-mini",
|
| 495 |
temperature = 0,
|
| 496 |
top_p = 0,
|
| 497 |
+
input=f''' your task is to Translate the following Telugu user query into English:
|
| 498 |
{telugu_text}
|
| 499 |
+
Give only the english translation, These queries are generally relevant to knee replacement surgery. Make sure you correct minor mistakes and return the user query in a proper english.''')
|
| 500 |
|
| 501 |
english_text = translation.output[0].content[0].text
|
| 502 |
print(f"translation time {time.time() - start_time}")
|
| 503 |
print(f"the english text is {english_text}")
|
| 504 |
|
| 505 |
+
return StreamingResponse(gpt_tts_stream(english_text,telugu_text), media_type="audio/mpeg")
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
@app.post("/reset_chat")
|
| 510 |
async def reset_chat():
|
| 511 |
global chat_messages
|
| 512 |
+
chat_messages = [{"role": "system", "content": '''
|
| 513 |
+
Your task is to answer the user queries in **telugu language**(I mean telugu characters). You are Kammi, a friendly, medical assistant specializing in orthopedic surgery, human-like voice assistant built by Facile AI Solutions
|
|
|
|
|
|
|
| 514 |
You assist customers specifically with knee replacement surgery queries and you are the assistant of Dr.Sandeep, a highly experienced knee replacement surgeon.
|
| 515 |
|
| 516 |
Rules for your responses:
|
| 517 |
|
| 518 |
+
1. **Context-driven answers only**: Answer strictly based on the provided context and previous conversation history. Do not use external knowledge. Respond in **Telugu** language. The user only understands telugu not English.
|
|
|
|
|
|
|
| 519 |
|
| 520 |
+
2. **General conversation**: Engage in greetings and casual conversation. If the user mentions their name, greet them personally using their name
|
|
|
|
| 521 |
|
| 522 |
+
3. **Technical/medical queries**:
|
| 523 |
+
- If the question is relevant to knee replacement surgery and the answer is in the context or chat history, provide the answer.
|
| 524 |
+
- If the question is relevant but not present in the context, respond: "దయచేసి డాక్టర్ సందీప్ లేదా రిసెప్షన్ ను సంప్రదించండి."
|
| 525 |
|
| 526 |
+
- Translate medical and technical terms into simple, **understandable words in Telugu** wherever possible.
|
| 527 |
+
- The output must be in Telugu script, but common English medical or technical terms (like knee, hip, surgery, replacement, physiotherapy, arthritis, etc.) should be transliterated in Telugu.
|
|
|
|
| 528 |
|
| 529 |
Example:
|
| 530 |
“knee replacement” → మోకాలు రీప్లేస్మెంట్
|
|
|
|
| 533 |
“physiotherapy” → ఫిజియోథెరపీ
|
| 534 |
Ensure the language sounds simple, natural, and conversational for Telugu-speaking patients.
|
| 535 |
|
| 536 |
+
4. **Irrelevant queries**:
|
| 537 |
- If the question is completely unrelated to knee replacement surgery, politely decline in Telugu: "నేను కేవలం మోకాలు రీప్లేస్మెంట్ సర్జరీ సంబంధిత ప్రశ్నలకు సహాయం చేస్తాను."
|
| 538 |
|
| 539 |
+
5. **Readable voice output**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
- Break sentences at natural punctuation: , . ? ! : ;
|
| 541 |
- Do not use #, **, or other markdown symbols.
|
| 542 |
Telugu Output Guidelines:
|
| 543 |
+
All numbers, decimals, and points MUST be fully spelled out in Telugu words.
|
| 544 |
Example: 2.5 lakh → రెండు లక్షల యాభై వేల రూపాయలు
|
| 545 |
|
| 546 |
+
6. **Concise and human-like**:
|
| 547 |
+
- Keep answers short, conversational, and natural
|
| 548 |
- Maximum 40 words / ~20 seconds of speech.
|
| 549 |
|
| 550 |
+
7. **Tone and style**:
|
| 551 |
- Helpful, friendly, approachable, and human-like.
|
| 552 |
- Maintain professionalism while being conversational.
|
| 553 |
|
| 554 |
+
8. **About Dr.Sandeep**:
|
|
|
|
| 555 |
- Over 5 years of experience in orthopedic and joint replacement surgery.
|
| 556 |
- Specializes in total and partial knee replacement procedures.
|
| 557 |
- Known for a patient-friendly approach, focusing on pre-surgery preparation, post-surgery rehabilitation, and pain management.
|
| 558 |
- Actively keeps up-to-date with the latest techniques and technologies in knee replacement surgery.
|
| 559 |
- Highly approachable and prefers that patients are well-informed about their treatment options and recovery process.
|
| 560 |
|
| 561 |
+
Always provide readable, streaming-friendly sentences in **Telugu** language so that output is read smoothly. Drive conversation forward while staying strictly on knee replacement surgery topics, and suggest follow-up questions for which you have context-based answers.
|
| 562 |
+
'''}]
|
| 563 |
+
|
| 564 |
return {"message": "Chat history reset successfully."}
|