File size: 24,873 Bytes
4187281
 
 
 
 
fcbfe7d
 
4187281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ca04ed
4187281
 
 
 
2ca04ed
4187281
2ca04ed
4187281
2ca04ed
 
 
4187281
2ca04ed
 
4187281
 
 
 
 
 
 
 
2ca04ed
4187281
 
2ca04ed
4187281
 
 
2ca04ed
4187281
 
2ca04ed
 
4187281
 
2ca04ed
4187281
 
 
2ca04ed
4187281
 
 
 
 
 
2ca04ed
4187281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcbfe7d
4187281
fcbfe7d
 
4187281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ca04ed
 
fcbfe7d
a4913cf
4187281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ca04ed
 
4187281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ca04ed
4187281
fcbfe7d
2ca04ed
fcbfe7d
 
 
 
 
a4913cf
4187281
 
2ca04ed
4187281
2ca04ed
4187281
 
 
2ca04ed
4187281
2ca04ed
4187281
 
 
 
 
 
 
 
 
fcbfe7d
 
 
 
4187281
fcbfe7d
4187281
2ca04ed
4187281
 
 
fcbfe7d
 
 
 
 
 
4187281
 
 
 
 
fcbfe7d
 
 
 
 
 
4187281
fcbfe7d
4187281
fcbfe7d
 
 
 
 
 
 
4187281
 
fcbfe7d
 
 
4187281
 
 
fcbfe7d
 
4187281
 
fcbfe7d
4187281
 
fcbfe7d
 
 
 
4187281
 
2ca04ed
 
 
 
4187281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ca04ed
 
 
4187281
 
 
fcbfe7d
 
 
 
4187281
fcbfe7d
4187281
 
 
 
 
2ca04ed
4187281
2ca04ed
4187281
 
fcbfe7d
4187281
fcbfe7d
 
 
 
4187281
fcbfe7d
4187281
 
 
 
2ca04ed
 
4187281
 
 
 
2ca04ed
4187281
2ca04ed
4187281
2ca04ed
 
 
4187281
2ca04ed
 
4187281
 
 
 
 
 
 
 
2ca04ed
4187281
 
2ca04ed
4187281
 
 
2ca04ed
4187281
 
2ca04ed
 
4187281
 
2ca04ed
4187281
 
 
2ca04ed
4187281
 
 
 
 
 
2ca04ed
 
 
4187281
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import os
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/transformers"
os.environ["HF_HOME"] = "/app/.cache/huggingface"

import uvicorn


from fastapi import FastAPI, File, UploadFile
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import openai
from dotenv import load_dotenv
from sentence_transformers import SentenceTransformer
import math
from collections import Counter
import json
import pandas as pd
import asyncio
import numpy as np
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
import openai as _openai_mod
import requests

import time
from fastapi import UploadFile, File
from starlette.responses import StreamingResponse
from pydub import AudioSegment
from openai import OpenAI
load_dotenv()

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
openai.api_key = os.getenv("OPENAI_API_KEY")

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/", response_class=HTMLResponse)
async def serve_html():
    with open("templates/index.html", "r", encoding="utf-8") as f:
        html_content = f.read()
    return HTMLResponse(content=html_content)


chat_messages = [{"role": "system", "content": '''
        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
        You assist customers specifically with knee replacement surgery queries and you are the assistant of Dr.Sandeep, a highly experienced knee replacement surgeon.

        Rules for your responses:

        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.

        2. **General conversation**: Engage in greetings and casual conversation. If the user mentions their name, greet them personally using their name

        3. **Technical/medical queries**:
        - If the question is relevant to knee replacement surgery and the answer is in the context or chat history, provide the answer.
        - If the question is relevant but not present in the context, respond: "దయచేసి డాక్టర్ సందీప్ లేదా రిసెప్షన్ ను సంప్రదించండి."

        - Translate medical and technical terms into simple, **understandable words in Telugu** wherever possible.
        - 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.

          Example:
          “knee replacement” → మోకాలు రీప్లేస్‌మెంట్
          “hip replacement” → హిప్ రీప్లేస్‌మెంట్
          “surgery” → సర్జరీ
          “physiotherapy” → ఫిజియోథెరపీ
          Ensure the language sounds simple, natural, and conversational for Telugu-speaking patients.

        4. **Irrelevant queries**:
        - If the question is completely unrelated to knee replacement surgery, politely decline in Telugu: "నేను కేవలం మోకాలు రీప్లేస్‌మెంట్ సర్జరీ సంబంధిత ప్రశ్నలకు సహాయం చేస్తాను."

        5. **Readable voice output**:
        - Break sentences at natural punctuation: , . ? ! : ;
        - Do not use #, **, or other markdown symbols.
          Telugu Output Guidelines:
            All numbers, decimals, and points MUST be fully spelled out in Telugu words.
            Example: 2.5 lakh → రెండు లక్షల యాభై వేల రూపాయలు

        6. **Concise and human-like**:
        - Keep answers short, conversational, and natural
        - Maximum 40 words / ~20 seconds of speech.

        7. **Tone and style**:
        - Helpful, friendly, approachable, and human-like.
        - Maintain professionalism while being conversational.

        8. **About Dr.Sandeep**:
        - Over 5 years of experience in orthopedic and joint replacement surgery.
        - Specializes in total and partial knee replacement procedures.
        - Known for a patient-friendly approach, focusing on pre-surgery preparation, post-surgery rehabilitation, and pain management.
        - Actively keeps up-to-date with the latest techniques and technologies in knee replacement surgery.
        - Highly approachable and prefers that patients are well-informed about their treatment options and recovery process.

        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.
'''}]

class BM25:
    def __init__(self, corpus, k1=1.2, b=0.75):
        self.corpus = [doc.split() if isinstance(doc, str) else doc for doc in corpus]
        self.k1 = k1
        self.b = b
        self.N = len(self.corpus)
        self.avgdl = sum(len(doc) for doc in self.corpus) / self.N
        self.doc_freqs = self._compute_doc_frequencies()
        self.idf = self._compute_idf()

    def _compute_doc_frequencies(self):
        """Count how many documents contain each term"""
        df = {}
        for doc in self.corpus:
            unique_terms = set(doc)
            for term in unique_terms:
                df[term] = df.get(term, 0) + 1
        return df

    def _compute_idf(self):
        """Compute the IDF for each term in the corpus"""
        idf = {}
        for term, df in self.doc_freqs.items():
            idf[term] = math.log((self.N - df + 0.5) / (df + 0.5) + 1)
        return idf

    def score(self, query, document):
        """Compute the BM25 score for one document and one query"""
        query_terms = query.split() if isinstance(query, str) else query
        doc_terms = document.split() if isinstance(document, str) else document
        score = 0.0
        freqs = Counter(doc_terms)
        doc_len = len(doc_terms)

        for term in query_terms:
            if term not in freqs:
                continue
            f = freqs[term]
            idf = self.idf.get(term, 0)
            denom = f + self.k1 * (1 - self.b + self.b * doc_len / self.avgdl)
            score += idf * (f * (self.k1 + 1)) / denom
        return score

    def rank(self, query):
        """Rank all documents for a given query"""
        return [(i, self.score(query, doc)) for i, doc in enumerate(self.corpus)]


def sigmoid_scaled(x, midpoint=3.0):
    """
    Sigmoid function with shifting.
    `midpoint` controls where the output is 0.5.
    """
    return 1 / (1 + math.exp(-(x - midpoint)))

def cosine_similarity(a: np.ndarray, b: np.ndarray) -> float:

    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

async def compute_similarity(query: str, query_embedding: np.ndarray, chunk_text: str, chunk_embedding: np.ndarray, sem_weight: float,syn_weight:float,bm25) -> float:

    semantic_score = cosine_similarity(query_embedding, chunk_embedding)

    # syntactic_score = fuzz.ratio(query, chunk_text) / 100.0
    syntactic_score = bm25.score(query,chunk_text)
    final_syntactic_score = sigmoid_scaled(syntactic_score)

    combined_score = sem_weight * semantic_score + syn_weight * final_syntactic_score

    return combined_score

async def retrieve_top_k_hybrid(query, k, sem_weight,syn_weight,bm25):
    emb_strt = time.time()
    query_embedding = model.encode(query)
    emb_end = time.time()
    print("\n\nTime for Query Embedding", emb_end-emb_strt)

    tasks = [

        compute_similarity(query, query_embedding, row["Chunks"], row["Embeddings"] , sem_weight,syn_weight,bm25)

        for _, row in df_expanded.iterrows()

    ]

    similarities = await asyncio.gather(*tasks)

    df_expanded["similarity"] = similarities

    top_results = df_expanded.sort_values(by="similarity", ascending=False).head(k)

    # print("the retrieved chunks are")
    # print(top_results["telugu_chunk"].to_list()[0])
    print("\n\nRetrieval Time", time.time() - emb_end)
    return top_results["telugu_chunk"].to_list()


os.makedirs("/tmp/transformers_cache", exist_ok=True)

model = SentenceTransformer("abhinand/MedEmbed-large-v0.1")
df_expanded = pd.read_excel("Database.xlsx")  # Replace with your filename
df_expanded["Embeddings"] = df_expanded["Embeddings"].map(lambda x: json.loads(x))
corpus = df_expanded['Chunks'].to_list()
bm25 = BM25(corpus)


# --- gTTS helper: stream raw audio file in small chunks ---
# def tts_chunk_stream(text_chunk: str, lang: str = "en"):
#     if not text_chunk.strip():
#         return []

#     tts = gTTS(text=text_chunk, lang=lang)
#     temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
#     tts.save(temp_file.name)

#     def audio_stream():
#         try:
#             with open(temp_file.name, "rb") as f:
#                 chunk = f.read(1024)
#                 while chunk:
#                     yield chunk
#                     chunk = f.read(1024)
#         finally:
#             try:
#                 os.remove(temp_file.name)
#             except Exception:
#                 pass

#     return audio_stream()


def tts_chunk_stream(text_chunk: str, lang: str = "en"):
    """
    REST-based OpenAI TTS fallback for older openai SDKs (e.g. 0.28).
    Returns a generator yielding MP3 byte chunks (1024 bytes).
    """
    if not text_chunk or not text_chunk.strip():
        return []

    # Map short lang -> locale (extend if needed)
    language_map = {
        "en": "en-US",
        "en-US": "en-US",
        "en-GB": "en-GB",
        "hi": "hi-IN",
    }
    language_code = language_map.get(lang, "en-GB")

    # TTS model & voice choice
    model = "gpt-4o-mini-tts"  # or "tts-1"
    voice = "alloy"           # alloy, verse, shimmer, echo, coral
    fmt = "mp3"

    # Resolve API key (prefer openai.api_key if available)
    api_key = None
    try:
        # if you set openai.api_key earlier in your code, prefer it
        api_key = getattr(_openai_mod, "api_key", None)
    except Exception:
        api_key = None

    if not api_key:
        api_key = os.getenv("OPENAI_API_KEY")

    if not api_key:
        print("OpenAI API key not found. Set openai.api_key or env var OPENAI_API_KEY.")
        return []

    url = "https://api.openai.com/v1/audio/speech"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }

    payload = {
        "model": model,
        "voice": voice,
        "input": text_chunk,
        "format": fmt,
        "temperature" : 0
        # "instructions" : "speak in cheerfull and positive tone"
        # optional: "language": language_code   # include if needed by API variation
    }

    try:
        # Use stream=True so we can yield bytes progressively.
        resp = requests.post(url, headers=headers, json=payload, stream=True, timeout=60)
    except Exception as e:
        print("OpenAI TTS request failed:", e)
        return []

    if resp.status_code != 200:
        # Try to show helpful error message
        try:
            err = resp.json()
        except Exception:
            err = resp.text
        print(f"OpenAI TTS REST call failed {resp.status_code}: {err}")
        try:
            resp.close()
        except Exception:
            pass
        return []

    # At this point resp.iter_content yields raw mp3 bytes
    def audio_stream():
        try:
            for chunk in resp.iter_content(chunk_size=1024):
                if chunk:
                    yield chunk
        finally:
            try:
                resp.close()
            except Exception:
                pass
    return audio_stream()


async def get_rag_response(user_message_english: str, user_message_telugu: str):
    global chat_messages
    start_time = time.time()
    Chunks = await retrieve_top_k_hybrid(user_message_english,15, 0.9, 0.1,bm25)
    end_time = time.time()
    # print(f"Retrieval start time : {start_time}")
    # print(f"Retrieval end time : {end_time}")
    # print(f"Retrieval duration is : {end_time - start_time}")

    context = "======================================================================================================\n".join(map(str,Chunks))
    chat_messages.append({"role": "user", "content": f'''     
          Context : {context}
          User Query: {user_message_telugu}'''})
    # print("chat_messages",chat_messages)
    return [chat_messages[0]]+chat_messages[-7:]


# --- GPT + TTS async generator with smaller buffer like second code ---
async def gpt_tts_stream(prompt: str,telugu_text: str):
    global chat_messages
    chat_messages = await get_rag_response(prompt,telugu_text)
    # print(chat_messages,"chat_messages after getting RAG response")
    # response = openai.ChatCompletion.create(
    #     model="gpt-4o",
    #     messages= chat_messages,
    #     stream=True
    # )
    bot_response = ""
    buffer = ""
    buffer_size = 30
    count1 = 0
    count2 = 0
    count3 = 0
    count4 = 0
    # ✅ Must use the `with` block for streaming
    start_time = time.time()
    with client.chat.completions.stream(
        model="gpt-4o",
        messages=chat_messages,
        ) as stream:
        for event in stream:
            if count1 == 0:
                end_time = time.time()
                # print(f"gpt call start time : {start_time}")
                # print(f"gpt response start time : {end_time}")
                print(f"gpt duration for first token : {end_time - start_time}")
            count1 += 1
            if event.type == "content.delta":
                delta = event.delta
                bot_response = bot_response + delta
                buffer += delta
                if len(buffer) >= buffer_size and buffer.endswith((".", "!", ",", "?", "\n", ";", ":")):
                    if count2 == 0:
                        count2 += 1
                        end_time = time.time()
                        # print(f"gpt response first buffer start time : {end_time}")
                        print(f"gpt duration for first buffer : {end_time - start_time}")
                    print(buffer)
                    # audio_chunks = tts_chunk_stream(buffer)
                    start_time = time.time()
                    for audio_chunk in tts_chunk_stream(buffer):
                        if count3 == 0:
                            count3+=1
                            end_time = time.time()
                            # print(f"tts start time : {start_time}")
                            # print(f"tts response first buffer start time : {end_time}")
                            print(f"tts duration for first buffer : {end_time - start_time}")
                        # print("chunk",buffer)
                        yield audio_chunk
                        buffer = ""
                    # audio_chunk = tts_chunk_stream(buffer)
                    # yield audio_chunk
                    # count+=1
            elif event.type == "content.done":
            # 🧾 model finished — flush whatever is left
                if buffer.strip():
                    start_time = time.time()
                    # print(f"the final response time : {start_time}")
                    print(buffer.strip())
                    for audio_chunk in tts_chunk_stream(buffer):

                    # print("chunk",buffer)
                        yield audio_chunk
                        # buffer = ""
                    # audio_chunk = tts_chunk_stream(buffer)
                    start_time = time.time()
                    # print(f"the final audio time : {start_time}")

        bot_response = bot_response.strip()
        # print("the final bot response :")
        # print(bot_response)
        # print("full repsones is")
        # print(fll_response)
        chat_messages.append({"role": "assistant", "content": bot_response})

# def convert_to_mono16_wav_bytes(audio_bytes: bytes) -> tuple[bytes, int]:
#     print("i am inside the mono16 conversion")
#     """Convert any uploaded audio (mp3/webm/wav) to mono 16-bit WAV bytes in memory."""
#     audio = AudioSegment.from_file(io.BytesIO(audio_bytes))

#     # Convert to mono
#     if audio.channels != 1:
#         audio = audio.set_channels(1)

#     # Convert to 16-bit PCM
#     if audio.sample_width != 2:
#         audio = audio.set_sample_width(2)

#     # Standardize sample rate to 16 kHz (required by Google STT)
#     if audio.frame_rate != 16000:
#         audio = audio.set_frame_rate(16000)

#     # Export as in-memory WAV bytes
#     wav_buffer = io.BytesIO()
#     audio.export(wav_buffer, format="wav")
#     wav_bytes = wav_buffer.getvalue()
#     print("mono 16 conversion done successfully")
#     return wav_bytes, 16000


# ------------------------------------------------------------------
# 2️⃣ Telugu STT (Speech-to-Text)
# ------------------------------------------------------------------
# def transcribe_telugu_audio(audio_bytes: bytes) -> tuple[str, float]:
#     print("i am inside the stt (telugu to telugu)")
#     wav_bytes, sample_rate = convert_to_mono16_wav_bytes(audio_bytes)
#     print("mono 16 conversion done successfully and fetched")
#     client = speech.SpeechClient()
#     print("clinet called successfully")
#     audio = speech.RecognitionAudio(content=wav_bytes)
#     print("audio created successfully")
#     config = speech.RecognitionConfig(
#         encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
#         sample_rate_hertz=sample_rate,
#         language_code="te-IN",
#         enable_automatic_punctuation=True,
#     )

#     print("\n🔊 Transcribing Telugu audio...")
#     start_time = time.time()
#     response = client.recognize(config=config, audio=audio)

#     telugu_text = " ".join(
#         [result.alternatives[0].transcript for result in response.results]
#     )
#     stt_time = time.time() - start_time
#     print(f"✅ Telugu STT completed in {stt_time:.2f} seconds")

#     return telugu_text.strip(), stt_time


# ------------------------------------------------------------------
# 3️⃣ Telugu → English Translation
# ------------------------------------------------------------------
# def translate_to_english(te_text: str) -> tuple[str, float]:
#     translate_client = translate.Client()
#     print("\n🌐 Translating to English...")
#     start_time = time.time()

#     result = translate_client.translate(te_text, target_language="en")
#     english_text = result["translatedText"]

#     translation_time = time.time() - start_time
#     print(f"✅ Translation completed in {translation_time:.2f} seconds")
# manoj
#     return english_text, translation_time


@app.post("/chat_stream")
async def chat_stream(file: UploadFile = File(...)):
    start_time = time.time()
    audio_bytes = await file.read()

    transcription = client.audio.transcriptions.create(
            model="gpt-4o-transcribe",  # or "gpt-4o-mini-transcribe"
            file=(file.filename, audio_bytes),  # important: (filename, bytes)
            language="te",
            prompt="Medical terms related to knee replacement surgery"
        )

    telugu_text = transcription.text
    end_time = time.time()
    # print(f"stt start time :{start_time}")
    # print(f"stt end time : {end_time}")
    print(f"transcription total time : {end_time-start_time}")
    print(f"the text is : {telugu_text}")

    start_time = time.time()
    translation = client.responses.create(
        model="gpt-4o-mini",
        temperature = 0,
        top_p = 0,
        input=f''' your task is to Translate the following Telugu user query into English:
        {telugu_text}
        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.''')

    english_text = translation.output[0].content[0].text
    end_time = time.time()

    # print(f"translation start time :{start_time}")
    # print(f"translation end time : {end_time}")
    print(f"translation total time : {end_time-start_time}")
    print(f"the english text is : {english_text}")

    return StreamingResponse(gpt_tts_stream(english_text,telugu_text), media_type="audio/mpeg")

@app.post("/reset_chat")
async def reset_chat():
    global chat_messages
    chat_messages = [{"role": "system", "content": '''
        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
        You assist customers specifically with knee replacement surgery queries and you are the assistant of Dr.Sandeep, a highly experienced knee replacement surgeon.

        Rules for your responses:

        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.

        2. **General conversation**: Engage in greetings and casual conversation. If the user mentions their name, greet them personally using their name

        3. **Technical/medical queries**:
        - If the question is relevant to knee replacement surgery and the answer is in the context or chat history, provide the answer.
        - If the question is relevant but not present in the context, respond: "దయచేసి డాక్టర్ సందీప్ లేదా రిసెప్షన్ ను సంప్రదించండి."

        - Translate medical and technical terms into simple, **understandable words in Telugu** wherever possible.
        - 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.

          Example:
          “knee replacement” → మోకాలు రీప్లేస్‌మెంట్
          “hip replacement” → హిప్ రీప్లేస్‌మెంట్
          “surgery” → సర్జరీ
          “physiotherapy” → ఫిజియోథెరపీ
          Ensure the language sounds simple, natural, and conversational for Telugu-speaking patients.

        4. **Irrelevant queries**:
        - If the question is completely unrelated to knee replacement surgery, politely decline in Telugu: "నేను కేవలం మోకాలు రీప్లేస్‌మెంట్ సర్జరీ సంబంధిత ప్రశ్నలకు సహాయం చేస్తాను."

        5. **Readable voice output**:
        - Break sentences at natural punctuation: , . ? ! : ;
        - Do not use #, **, or other markdown symbols.
          Telugu Output Guidelines:
            All numbers, decimals, and points MUST be fully spelled out in Telugu words.
            Example: 2.5 lakh → రెండు లక్షల యాభై వేల రూపాయలు

        6. **Concise and human-like**:
        - Keep answers short, conversational, and natural
        - Maximum 40 words / ~20 seconds of speech.

        7. **Tone and style**:
        - Helpful, friendly, approachable, and human-like.
        - Maintain professionalism while being conversational.

        8. **About Dr.Sandeep**:
        - Over 5 years of experience in orthopedic and joint replacement surgery.
        - Specializes in total and partial knee replacement procedures.
        - Known for a patient-friendly approach, focusing on pre-surgery preparation, post-surgery rehabilitation, and pain management.
        - Actively keeps up-to-date with the latest techniques and technologies in knee replacement surgery.
        - Highly approachable and prefers that patients are well-informed about their treatment options and recovery process.

        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.
    '''}]

    return {"message": "Chat history reset successfully."}