Update app.py
Browse files
app.py
CHANGED
|
@@ -104,15 +104,19 @@ def generate_answer(query: str) -> str:
|
|
| 104 |
f"Question: {query}\n\n"
|
| 105 |
f"Answer:"
|
| 106 |
)
|
| 107 |
-
# Use max_new_tokens to generate additional tokens beyond the prompt.
|
| 108 |
response = generator(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# ====================================================
|
| 112 |
# 2. Speech-to-Text and Text-to-Speech Functions
|
| 113 |
# ====================================================
|
| 114 |
|
| 115 |
-
# Force Whisper to load on CPU explicitly
|
| 116 |
stt_model = whisper.load_model("base", device="cpu")
|
| 117 |
|
| 118 |
def speech_to_text(audio_array: np.ndarray, sample_rate: int = 16000) -> str:
|
|
@@ -193,7 +197,6 @@ class RAGVoiceHandler(AsyncStreamHandler):
|
|
| 193 |
# 4. Voice Streaming Setup & FastAPI Endpoints
|
| 194 |
# ====================================================
|
| 195 |
|
| 196 |
-
# Supply a dummy (but valid) RTC configuration with a TURN server entry
|
| 197 |
rtc_config = {
|
| 198 |
"iceServers": [
|
| 199 |
{"urls": "stun:stun.l.google.com:19302"},
|
|
@@ -229,7 +232,6 @@ async def input_hook(body: InputData):
|
|
| 229 |
async def webrtc_offer(offer: dict):
|
| 230 |
return await stream.handle_offer(offer)
|
| 231 |
|
| 232 |
-
# Added /chat endpoint for text-based queries (fallback)
|
| 233 |
@app.post("/chat")
|
| 234 |
async def chat_endpoint(payload: dict):
|
| 235 |
question = payload.get("question", "")
|
|
|
|
| 104 |
f"Question: {query}\n\n"
|
| 105 |
f"Answer:"
|
| 106 |
)
|
|
|
|
| 107 |
response = generator(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)
|
| 108 |
+
generated_text = response[0]["generated_text"]
|
| 109 |
+
# Return only the text after the "Answer:" delimiter
|
| 110 |
+
if "Answer:" in generated_text:
|
| 111 |
+
answer = generated_text.split("Answer:", 1)[1].strip()
|
| 112 |
+
else:
|
| 113 |
+
answer = generated_text.strip()
|
| 114 |
+
return answer
|
| 115 |
|
| 116 |
# ====================================================
|
| 117 |
# 2. Speech-to-Text and Text-to-Speech Functions
|
| 118 |
# ====================================================
|
| 119 |
|
|
|
|
| 120 |
stt_model = whisper.load_model("base", device="cpu")
|
| 121 |
|
| 122 |
def speech_to_text(audio_array: np.ndarray, sample_rate: int = 16000) -> str:
|
|
|
|
| 197 |
# 4. Voice Streaming Setup & FastAPI Endpoints
|
| 198 |
# ====================================================
|
| 199 |
|
|
|
|
| 200 |
rtc_config = {
|
| 201 |
"iceServers": [
|
| 202 |
{"urls": "stun:stun.l.google.com:19302"},
|
|
|
|
| 232 |
async def webrtc_offer(offer: dict):
|
| 233 |
return await stream.handle_offer(offer)
|
| 234 |
|
|
|
|
| 235 |
@app.post("/chat")
|
| 236 |
async def chat_endpoint(payload: dict):
|
| 237 |
question = payload.get("question", "")
|