File size: 14,498 Bytes
d08081f f65990c d08081f f65990c d08081f f65990c d08081f f65990c d08081f 1a15b05 d08081f 1a15b05 587afb9 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f 1a15b05 e141e7c 1a15b05 e141e7c 1a15b05 e141e7c 1a15b05 e141e7c 1a15b05 e141e7c 1a15b05 e141e7c 1a15b05 e141e7c 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f f65990c d08081f f65990c d08081f f65990c d08081f f65990c d08081f 1a15b05 f6f656f f65990c d08081f f6f656f f65990c 1a15b05 f65990c d08081f e141e7c 1a15b05 d08081f 1a15b05 b46da1b d08081f 1a15b05 b46da1b 1a15b05 d08081f f65990c 1a15b05 d08081f f65990c d08081f f65990c 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f b46da1b f65990c d08081f 1a15b05 d08081f f65990c d08081f 1a15b05 53354e8 1a15b05 e141e7c 1a15b05 e141e7c d08081f 1a15b05 d08081f 1a15b05 53354e8 d08081f 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f 1a15b05 2f2f8a0 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f 1a15b05 e85255d d08081f 1a15b05 d08081f f65990c d08081f 1a15b05 d08081f f65990c 1a15b05 d08081f f65990c d08081f 1a15b05 d08081f f65990c 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f f65990c 1a15b05 d08081f 1a15b05 d08081f 1a15b05 d08081f f65990c d08081f 1a15b05 d08081f 1a15b05 d08081f f65990c d08081f 1a15b05 d08081f f65990c 1a15b05 f65990c b46da1b d08081f 1a15b05 f65990c 1a15b05 b46da1b f65990c 1a15b05 b46da1b f65990c b46da1b f65990c 1a15b05 f65990c b85911f 1a15b05 f65990c 1a15b05 f65990c 1a15b05 f65990c 1a15b05 f65990c d08081f 1a15b05 f65990c 1a15b05 f65990c e141e7c f65990c 1a15b05 f65990c 1a15b05 f65990c 1a15b05 f65990c |
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 |
import os
import io
import json
import re
import tempfile
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request, status, Depends, Header, HTTPException
from fastapi.concurrency import run_in_threadpool
from pydantic import BaseModel
from dotenv import load_dotenv
from openai import OpenAI
from elevenlabs.client import ElevenLabs
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_postgres.vectorstores import PGVector
from sqlalchemy import create_engine
# --- GRADIO ---
import gradio as gr
# --------------------------------------------------------------------------- #
# CONFIGURATION
# --------------------------------------------------------------------------- #
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
logging.getLogger("tensorflow").setLevel(logging.ERROR)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
load_dotenv()
NEON_DATABASE_URL = os.getenv("NEON_DATABASE_URL")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
SHARED_SECRET = os.getenv("SHARED_SECRET")
COLLECTION_NAME = "real_estate_embeddings"
EMBEDDING_MODEL = "hkunlp/instructor-large"
# *** HARD-CODED VOICE ID (as requested) ***
ELEVENLABS_VOICE_ID = "LHJy3mhZWsvhUjy0zUM1" # <-- your voice
PLANNER_MODEL = "gpt-4o-mini"
ANSWERER_MODEL = "gpt-4o"
TABLE_DESCRIPTIONS = """
- "ongoing_projects_source": Details about projects currently under construction.
- "upcoming_projects_source": Information on future planned projects.
- "completed_projects_source": Facts about projects that are already finished.
- "historical_sales_source": Specific sales records, including price, date, and property ID.
- "past_customers_source": Information about previous customers.
- "feedback_source": Customer feedback and ratings for projects.
"""
# --------------------------------------------------------------------------- #
# CLIENTS
# --------------------------------------------------------------------------- #
embeddings = None
vector_store = None
client_openai = OpenAI(api_key=OPENAI_API_KEY)
client_elevenlabs = None
# ---- ElevenLabs init with detailed logging ---------------------------------
try:
key_preview = (
f"{ELEVENLABS_API_KEY[:5]}...{ELEVENLABS_API_KEY[-4:]}"
if ELEVENLABS_API_KEY and len(ELEVENLABS_API_KEY) > 9
else "None"
)
logging.info(f"Initializing ElevenLabs client with key: {key_preview}")
if not ELEVENLABS_API_KEY:
raise ValueError("ELEVENLABS_API_KEY is missing or empty.")
client_elevenlabs = ElevenLabs(api_key=ELEVENLABS_API_KEY)
logging.info(f"ElevenLabs client created β type: {type(client_elevenlabs)}")
# Verify we can list voices (optional, but proves the key works)
voices = client_elevenlabs.voices.get_all()
logging.info(f"Fetched {len(voices.voices)} voices from ElevenLabs.")
except Exception as e:
logging.error(f"ElevenLabs init failed: {e}", exc_info=True)
client_elevenlabs = None
# ---- Log SDK version -------------------------------------------------------
try:
import elevenlabs
logging.info(f"elevenlabs SDK version: {elevenlabs.__version__}")
except Exception:
logging.error("Could not import elevenlabs package.")
# --------------------------------------------------------------------------- #
# FASTAPI APP
# --------------------------------------------------------------------------- #
@asynccontextmanager
async def lifespan(app: FastAPI):
global embeddings, vector_store
logging.info(f"Loading embedding model: {EMBEDDING_MODEL}")
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
logging.info(f"Connecting to vector store: {COLLECTION_NAME}")
engine = create_engine(NEON_DATABASE_URL, pool_pre_ping=True)
vector_store = PGVector(
connection=engine,
collection_name=COLLECTION_NAME,
embeddings=embeddings,
)
logging.info("Vector store ready.")
yield
logging.info("Shutting down.")
app = FastAPI(lifespan=lifespan)
# --------------------------------------------------------------------------- #
# PROMPTS
# --------------------------------------------------------------------------- #
QUERY_FORMULATION_PROMPT = """
You are a query analysis agent. Transform the user's query into a precise search query and determine the correct table to filter by.
**Available Tables:**
{table_descriptions}
**User's Query:** "{user_query}"
**Task:**
1. Rephrase into a clear, keyword-focused English search query.
2. If status keywords (ongoing, completed, upcoming, etc.) are present, pick the matching table.
3. If no status keyword, set filter_table to null.
4. Return JSON: {{"search_query": "...", "filter_table": "table_name or null"}}
""".strip()
ANSWER_SYSTEM_PROMPT = """
You are an expert AI assistant for a premier real estate developer.
## CORE KNOWLEDGE
- Cities: Pune, Mumbai, Bengaluru, Delhi, Chennai, Hyderabad, Goa, Gurgaon, Kolkata.
- Properties: Luxury apartments, villas, commercial.
- Budget: 45 lakhs to 5 crores.
## RULES
1. Match user language (Hinglish β Hinglish, English β English).
2. Use CONTEXT if available, else use core knowledge.
3. Only answer real estate questions.
""".strip()
# --------------------------------------------------------------------------- #
# AUDIO & LLM HELPERS
# --------------------------------------------------------------------------- #
def transcribe_audio(audio_path: str, audio_bytes: bytes) -> str:
for attempt in range(3):
try:
audio_file = io.BytesIO(audio_bytes)
filename = os.path.basename(audio_path)
logging.info(f"Transcribing {filename} ({len(audio_bytes)} bytes)")
transcript = client_openai.audio.transcriptions.create(
model="whisper-1",
file=(filename, audio_file),
)
text = transcript.text.strip()
# Hinglish transliteration
if re.search(r"[\u0900-\u097F]", text):
resp = client_openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": f"Transliterate to Roman (Hinglish): {text}"}
],
temperature=0.0,
)
text = resp.choices[0].message.content.strip()
logging.info(f"Transcribed: {text}")
return text
except Exception as e:
logging.error(f"Transcription error (attempt {attempt + 1}): {e}", exc_info=True)
if attempt == 2:
return ""
return ""
def generate_elevenlabs_sync(text: str) -> bytes:
"""
Uses the hard-coded voice ID and the correct SDK method.
NOTE: `model` parameter is REMOVED in SDK v2.17.0+
"""
if client_elevenlabs is None:
logging.error("ElevenLabs client not initialized β skipping TTS.")
return b""
for attempt in range(3):
try:
logging.info("Calling ElevenLabs text_to_speech.convert...")
stream = client_elevenlabs.text_to_speech.convert(
voice_id=ELEVENLABS_VOICE_ID,
text=text,
output_format="mp3_44100_128",
# model="eleven_multilingual_v2" β REMOVED
)
audio_bytes = b""
for chunk in stream:
if chunk:
audio_bytes += chunk
logging.info(f"TTS returned {len(audio_bytes)} bytes.")
return audio_bytes
except Exception as e:
logging.error(
f"ElevenLabs TTS error (attempt {attempt + 1}): {e}", exc_info=True
)
if attempt == 2:
return b""
return b""
async def formulate_search_plan(user_query: str) -> dict:
logging.info(f"Formulating search plan for: {user_query}")
for attempt in range(3):
try:
formatted = QUERY_FORMULATION_PROMPT.format(
table_descriptions=TABLE_DESCRIPTIONS, user_query=user_query
)
resp = await run_in_threadpool(
client_openai.chat.completions.create,
model=PLANNER_MODEL,
messages=[{"role": "user", "content": formatted}],
response_format={"type": "json_object"},
temperature=0.0,
)
raw = resp.choices[0].message.content
logging.info(f"Planner raw response: {raw}")
plan = json.loads(raw)
logging.info(f"Parsed plan: {plan}")
return plan
except Exception as e:
logging.error(f"Planner error (attempt {attempt + 1}): {e}", exc_info=True)
if attempt == 2:
return {"search_query": user_query, "filter_table": None}
return {"search_query": user_query, "filter_table": None}
async def get_agent_response(user_text: str) -> str:
for attempt in range(3):
try:
plan = await formulate_search_plan(user_text)
search_q = plan.get("search_query", user_text)
filter_tbl = plan.get("filter_table")
search_filter = {"source_table": filter_tbl} if filter_tbl else {}
docs = await run_in_threadpool(
vector_store.similarity_search,
search_q,
k=3,
filter=search_filter,
)
if not docs:
docs = await run_in_threadpool(vector_store.similarity_search, search_q, k=3)
context = "\n\n".join(d.page_content for d in docs)
resp = await run_in_threadpool(
client_openai.chat.completions.create,
model=ANSWERER_MODEL,
messages=[
{"role": "system", "content": ANSWER_SYSTEM_PROMPT},
{"role": "system", "content": f"CONTEXT:\n{context}"},
{"role": "user", "content": f"Question: {user_text}"},
],
)
return resp.choices[0].message.content.strip()
except Exception as e:
logging.error(f"RAG error (attempt {attempt + 1}): {e}", exc_info=True)
if attempt == 2:
return "Sorry, I couldn't respond. Please try again."
return "Sorry, I couldn't respond."
# --------------------------------------------------------------------------- #
# AUTH ENDPOINT
# --------------------------------------------------------------------------- #
class TextQuery(BaseModel):
query: str
async def verify_token(x_auth_token: str = Header(...)):
if not SHARED_SECRET or x_auth_token != SHARED_SECRET:
logging.warning("Auth failed for /test-text-query")
raise HTTPException(status_code=401, detail="Invalid token")
logging.info("Auth passed")
@app.post("/test-text-query", dependencies=[Depends(verify_token)])
async def test_text_query_endpoint(query: TextQuery):
logging.info(f"Text query: {query.query}")
response = await get_agent_response(query.query)
return {"response": response}
# --------------------------------------------------------------------------- #
# GRADIO PIPELINE
# --------------------------------------------------------------------------- #
async def process_audio(audio_path):
if not audio_path or not os.path.exists(audio_path):
return None, "No valid audio file received."
try:
# ---- 1. READ RAW BYTES ------------------------------------------------
with open(audio_path, "rb") as f:
audio_bytes = f.read()
if not audio_bytes:
return None, "Empty audio file."
# ---- 2. TRANSCRIBE ----------------------------------------------------
user_text = await run_in_threadpool(transcribe_audio, audio_path, audio_bytes)
if not user_text:
return None, "Couldn't understand audio. Try again."
logging.info(f"User: {user_text}")
# ---- 3. GET AI RESPONSE -----------------------------------------------
agent_response = await get_agent_response(user_text)
if not agent_response:
return None, "No response generated."
logging.info(f"AI: {agent_response[:100]}...")
logging.info(f"FULL AI Response sent to ElevenLabs: >>>{agent_response}<<<")
# ---- 4. TEXT-TO-SPEECH ------------------------------------------------
ai_audio_bytes = await run_in_threadpool(generate_elevenlabs_sync, agent_response)
if not ai_audio_bytes:
logging.error("TTS failed β returning text only.")
return (
None,
f"**You:** {user_text}\n\n**AI:** {agent_response}\n\n_(Audio generation failed)_",
)
# Save to a temporary file for Gradio
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
f.write(ai_audio_bytes)
out_path = f.name
logging.info(f"Saved TTS audio to {out_path}")
return out_path, f"**You:** {user_text}\n\n**AI:** {agent_response}"
except Exception as e:
logging.error(f"Audio processing error: {e}", exc_info=True)
return None, f"Error: {str(e)}"
# --------------------------------------------------------------------------- #
# GRADIO UI
# --------------------------------------------------------------------------- #
with gr.Blocks(title="Real Estate AI") as demo:
gr.Markdown("# Real Estate Voice Assistant")
gr.Markdown("Ask about projects in Pune, Mumbai, Bengaluru, etc.")
with gr.Row():
inp = gr.Audio(sources=["microphone"], type="filepath", label="Speak")
out_audio = gr.Audio(label="AI Response", type="filepath")
out_text = gr.Textbox(label="Conversation", lines=8)
inp.change(process_audio, inputs=inp, outputs=[out_audio, out_text])
# No examples β they caused FileNotFound errors when clicking text.
# --------------------------------------------------------------------------- #
# MOUNT GRADIO
# --------------------------------------------------------------------------- #
app = gr.mount_gradio_app(app, demo, path="/") |