Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +19 -57
app/main.py
CHANGED
|
@@ -2,7 +2,8 @@
|
|
| 2 |
from fastapi import FastAPI, UploadFile, File, Request, Form
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
-
from app.backend import extract_text_from_pdf, build_faiss_index, generate_answer
|
|
|
|
| 6 |
import os
|
| 7 |
from markdown import markdown as md
|
| 8 |
import re
|
|
@@ -119,76 +120,37 @@ async def voice_endpoint(
|
|
| 119 |
query: str = Form(...),
|
| 120 |
user_data_text: str = Form(""),
|
| 121 |
session_id: str = Form(...),
|
| 122 |
-
kb_type: str = Form("bank") # default
|
| 123 |
):
|
| 124 |
start_time = time.time()
|
| 125 |
chat_history = chat_sessions.get(session_id, [])
|
| 126 |
combined_user_data = user_data_text.strip()
|
| 127 |
|
| 128 |
-
# Choose correct FAISS index & chunks
|
| 129 |
if kb_type.lower() == "nse":
|
| 130 |
index, chunks = nse_index, nse_chunks
|
| 131 |
else:
|
| 132 |
index, chunks = bank_index, bank_chunks
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
if chat_history and len(chat_history) > 6:
|
| 144 |
-
summary_block = summarize_chat_history(chat_history)
|
| 145 |
-
|
| 146 |
-
# Build a voice-optimized prompt
|
| 147 |
-
prompt = f"""
|
| 148 |
-
You are FinBot, a calm and professional banking voice assistant.
|
| 149 |
-
You are talking to the user through a voice interface.
|
| 150 |
-
Your task is to answer clearly and naturally, using short sentences (2–4 lines).
|
| 151 |
-
Do NOT use emojis, markdown, equations, or special characters.
|
| 152 |
-
|
| 153 |
-
Use the following context for your answer:
|
| 154 |
-
<context>
|
| 155 |
-
{context}
|
| 156 |
-
</context>
|
| 157 |
-
|
| 158 |
-
Conversation summary:
|
| 159 |
-
{summary_block}
|
| 160 |
-
|
| 161 |
-
User said: "{query}"
|
| 162 |
-
|
| 163 |
-
If the question is not financial or unrelated, respond in a friendly, natural tone.
|
| 164 |
-
Example style:
|
| 165 |
-
- "Your balance is around 20,000 rupees."
|
| 166 |
-
- "I’ve transferred that successfully."
|
| 167 |
-
- "Could you please repeat that?"
|
| 168 |
-
|
| 169 |
-
Keep tone polite, confident, and simple.
|
| 170 |
-
Respond only with plain conversational text.
|
| 171 |
-
"""
|
| 172 |
-
|
| 173 |
-
try:
|
| 174 |
-
client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=openrouter_keys[-1])
|
| 175 |
-
completion = client.chat.completions.create(
|
| 176 |
-
model=MODEL_NAME,
|
| 177 |
-
messages=[{"role": "user", "content": prompt}],
|
| 178 |
-
extra_headers={"X-Title": "FinBotVoice"}
|
| 179 |
-
)
|
| 180 |
-
answer = completion.choices[0].message.content.strip()
|
| 181 |
-
except Exception as e:
|
| 182 |
-
print("❌ Voice answer generation error:", e)
|
| 183 |
-
answer = "Sorry, I couldn’t process that right now."
|
| 184 |
-
|
| 185 |
-
# --- Update chat history ---
|
| 186 |
chat_history.append({"role": "user", "message": query})
|
| 187 |
chat_history.append({"role": "bot", "message": answer})
|
| 188 |
chat_sessions[session_id] = chat_history
|
| 189 |
|
| 190 |
-
# Log
|
|
|
|
|
|
|
|
|
|
| 191 |
time_taken = round(time.time() - start_time, 2)
|
| 192 |
-
print(f"[VOICE
|
| 193 |
|
|
|
|
| 194 |
return JSONResponse(content={"response": answer})
|
|
|
|
| 2 |
from fastapi import FastAPI, UploadFile, File, Request, Form
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
+
from app.backend import extract_text_from_pdf, build_faiss_index, generate_answer, generate_voice_response
|
| 6 |
+
|
| 7 |
import os
|
| 8 |
from markdown import markdown as md
|
| 9 |
import re
|
|
|
|
| 120 |
query: str = Form(...),
|
| 121 |
user_data_text: str = Form(""),
|
| 122 |
session_id: str = Form(...),
|
| 123 |
+
kb_type: str = Form("bank") # ✅ default knowledge base
|
| 124 |
):
|
| 125 |
start_time = time.time()
|
| 126 |
chat_history = chat_sessions.get(session_id, [])
|
| 127 |
combined_user_data = user_data_text.strip()
|
| 128 |
|
| 129 |
+
# ✅ Choose the correct FAISS index & chunks
|
| 130 |
if kb_type.lower() == "nse":
|
| 131 |
index, chunks = nse_index, nse_chunks
|
| 132 |
else:
|
| 133 |
index, chunks = bank_index, bank_chunks
|
| 134 |
|
| 135 |
+
# ✅ Generate natural voice-friendly answer
|
| 136 |
+
answer = generate_voice_response(
|
| 137 |
+
query=query,
|
| 138 |
+
index=index,
|
| 139 |
+
chunks=chunks,
|
| 140 |
+
chat_history=chat_history
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# ✅ Update chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
chat_history.append({"role": "user", "message": query})
|
| 145 |
chat_history.append({"role": "bot", "message": answer})
|
| 146 |
chat_sessions[session_id] = chat_history
|
| 147 |
|
| 148 |
+
# ✅ Log for monitoring
|
| 149 |
+
input_context = "\n".join([c["message"] for c in chat_history]) + combined_user_data
|
| 150 |
+
input_tokens = len(input_context.split())
|
| 151 |
+
output_tokens = len(answer.split())
|
| 152 |
time_taken = round(time.time() - start_time, 2)
|
| 153 |
+
print(f"[VOICE LOG] Session: {session_id} | KB: {kb_type} | Input Tokens: {input_tokens} | Output Tokens: {output_tokens} | Time: {time_taken}s")
|
| 154 |
|
| 155 |
+
# ✅ Return plain text (voice assistant will TTS it)
|
| 156 |
return JSONResponse(content={"response": answer})
|