Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,419 +1,110 @@
|
|
| 1 |
-
# app.py โ Drop-in version with refined mic, safe Whisper, and UX polish
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
import json
|
| 5 |
-
import tempfile
|
| 6 |
-
import re
|
| 7 |
-
import pandas as pd
|
| 8 |
import streamlit as st
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from langchain.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from streamlit_mic_recorder import mic_recorder
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
from
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
initialize_from_jsonl, check_database_stats
|
| 34 |
-
)
|
| 35 |
-
from pdf_utils import extract_pdf_text
|
| 36 |
-
|
| 37 |
-
# =========================
|
| 38 |
-
# ---------- UI -----------
|
| 39 |
-
# =========================
|
| 40 |
-
|
| 41 |
-
st.set_page_config(page_title="Talk2Data | Multi-Model RAG", layout="wide")
|
| 42 |
-
st.markdown(
|
| 43 |
-
"""
|
| 44 |
-
<style>
|
| 45 |
-
.title {text-align:center; font-size:40px; font-weight:700; color:#2E86C1;}
|
| 46 |
-
.subtle {color:#6c757d; font-size:14px;}
|
| 47 |
-
.result-card {background:#fff; border-radius:14px; padding:16px; box-shadow:0 2px 12px rgba(0,0,0,.06);}
|
| 48 |
-
.pill {display:inline-block; padding:2px 8px; border-radius:999px; background:#EAF2FB; color:#2E86C1; font-weight:600; font-size:12px; margin-right:8px;}
|
| 49 |
-
</style>
|
| 50 |
-
""",
|
| 51 |
-
unsafe_allow_html=True
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
st.markdown('<h1 class="title">Talk2Data โ Multi-Model RAG</h1>', unsafe_allow_html=True)
|
| 55 |
-
st.caption("Ask your data anything โ voice, text, or files. Generate SQL, run queries, and visualize results.")
|
| 56 |
-
|
| 57 |
-
# =========================
|
| 58 |
-
# ----- Session State -----
|
| 59 |
-
# =========================
|
| 60 |
-
if "chat_history" not in st.session_state:
|
| 61 |
-
st.session_state["chat_history"] = [] # [{role, content, sql, df}]
|
| 62 |
-
if "pending_voice_query" not in st.session_state:
|
| 63 |
-
st.session_state["pending_voice_query"] = None
|
| 64 |
-
if "last_result_df" not in st.session_state:
|
| 65 |
-
st.session_state["last_result_df"] = None
|
| 66 |
-
if "last_data_question" not in st.session_state:
|
| 67 |
-
st.session_state["last_data_question"] = None
|
| 68 |
-
|
| 69 |
-
# =========================
|
| 70 |
-
# ----- Core Services -----
|
| 71 |
-
# =========================
|
| 72 |
-
engine = get_engine()
|
| 73 |
-
db = get_db(engine)
|
| 74 |
-
llm = get_llm()
|
| 75 |
-
sql_prompt = get_sql_prompt()
|
| 76 |
-
|
| 77 |
-
# ---------- Vector DB ----------
|
| 78 |
-
PROMPT_JSONL_PATH = "prompts.jsonl"
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
with st.spinner("๐ Initializing persistent vector DB..."):
|
| 82 |
-
chroma_client = get_chroma_client()
|
| 83 |
-
chroma_collection = get_chroma_collection(chroma_client)
|
| 84 |
-
embedding_model = get_embedding_model()
|
| 85 |
-
initialize_from_jsonl(PROMPT_JSONL_PATH)
|
| 86 |
-
|
| 87 |
-
stats = check_database_stats()
|
| 88 |
-
if stats["status"] == "healthy":
|
| 89 |
-
st.success(f"โ
Vector DB ready: {stats['total_items']} prompts loaded")
|
| 90 |
-
else:
|
| 91 |
-
st.warning(f"โ ๏ธ Vector DB status: {stats['status']}")
|
| 92 |
-
except Exception as e:
|
| 93 |
-
st.error(f"โ Vector DB init failed: {e}")
|
| 94 |
-
chroma_client = chroma_collection = embedding_model = None
|
| 95 |
-
|
| 96 |
-
# =========================
|
| 97 |
-
# ------ Sidebar I/O ------
|
| 98 |
-
# =========================
|
| 99 |
-
with st.sidebar:
|
| 100 |
-
st.header("๐ Upload files")
|
| 101 |
-
uploaded_files = st.file_uploader(
|
| 102 |
-
"Upload PDF, CSV, or Excel", type=["pdf", "csv", "xlsx", "xls"], accept_multiple_files=True
|
| 103 |
-
)
|
| 104 |
-
if "uploaded_dfs" not in st.session_state:
|
| 105 |
-
st.session_state["uploaded_dfs"] = {}
|
| 106 |
-
if "uploaded_texts" not in st.session_state:
|
| 107 |
-
st.session_state["uploaded_texts"] = {}
|
| 108 |
-
|
| 109 |
-
for f in uploaded_files or []:
|
| 110 |
-
name = f.name
|
| 111 |
-
if name.endswith(".csv"):
|
| 112 |
-
df = pd.read_csv(f)
|
| 113 |
-
st.session_state["uploaded_dfs"][name] = df
|
| 114 |
-
st.success(f"CSV loaded: {name} (shape: {df.shape})")
|
| 115 |
-
elif name.endswith((".xlsx", ".xls")):
|
| 116 |
-
df = pd.read_excel(f)
|
| 117 |
-
st.session_state["uploaded_dfs"][name] = df
|
| 118 |
-
st.success(f"Excel loaded: {name} (shape: {df.shape})")
|
| 119 |
-
elif name.endswith(".pdf"):
|
| 120 |
-
txt = extract_pdf_text(f)
|
| 121 |
-
st.session_state["uploaded_texts"][name] = txt
|
| 122 |
-
st.success(f"PDF loaded: {name} (chars: {len(txt)})")
|
| 123 |
-
|
| 124 |
-
if st.session_state["uploaded_dfs"]:
|
| 125 |
-
st.markdown("#### DataFrames")
|
| 126 |
-
for k, df in st.session_state["uploaded_dfs"].items():
|
| 127 |
-
st.write(f"**{k}**")
|
| 128 |
-
st.dataframe(df.head())
|
| 129 |
-
|
| 130 |
-
if st.session_state["uploaded_texts"]:
|
| 131 |
-
st.markdown("#### PDF Preview")
|
| 132 |
-
for k, t in st.session_state["uploaded_texts"].items():
|
| 133 |
-
st.write(f"**{k}**: {t[:400]}{'...' if len(t) > 400 else ''}")
|
| 134 |
-
|
| 135 |
-
st.markdown("---")
|
| 136 |
-
debug = st.toggle("๐ฌ Show RAG Debug Info", value=False)
|
| 137 |
-
st.caption("Great for demos โ shows SQL, matches, and chain steps.")
|
| 138 |
-
|
| 139 |
-
# =========================
|
| 140 |
-
# ------- Utilities -------
|
| 141 |
-
# =========================
|
| 142 |
-
|
| 143 |
-
def format_schema_markdown(schema_info: str) -> str:
|
| 144 |
-
tables = re.findall(r'CREATE TABLE \"([^\"]+)\" \((.*?)\)', schema_info, re.DOTALL)
|
| 145 |
-
md = "**Database Tables and Columns:**\n"
|
| 146 |
-
for table, cols in tables:
|
| 147 |
-
col_names = re.findall(r'\"([^\"]+)\"', cols)
|
| 148 |
-
md += f'- **{table}**: ' + ", ".join(f'`{c}`' for c in col_names) + "\\n"
|
| 149 |
-
return md
|
| 150 |
-
|
| 151 |
-
followup_starters = [
|
| 152 |
-
"and ", "also", "from which", "which", "what about", "where", "who", "how many",
|
| 153 |
-
"show me those", "give me those"
|
| 154 |
-
]
|
| 155 |
-
def is_followup_question(q: str) -> bool:
|
| 156 |
-
ql = q.strip().lower()
|
| 157 |
-
return any(ql.startswith(sta) for sta in followup_starters)
|
| 158 |
-
|
| 159 |
-
def create_data_visualization(df: pd.DataFrame, chart_key: str):
|
| 160 |
-
if not VISUALIZATION_AVAILABLE or df.empty:
|
| 161 |
-
return
|
| 162 |
-
st.subheader("๐ Quick Visuals")
|
| 163 |
-
c1, c2, c3 = st.columns(3)
|
| 164 |
-
with c1: b = st.checkbox("Bar", key=f"b_{chart_key}")
|
| 165 |
-
with c2: p = st.checkbox("Pie", key=f"p_{chart_key}")
|
| 166 |
-
with c3: l = st.checkbox("Line", key=f"l_{chart_key}")
|
| 167 |
-
|
| 168 |
-
if not any([b, p, l]): return
|
| 169 |
-
|
| 170 |
-
numeric_cols = df.select_dtypes(include=["number"]).columns.tolist()
|
| 171 |
-
x = st.selectbox("X-axis", df.columns, key=f"x_{chart_key}")
|
| 172 |
-
y = st.selectbox("Y-axis", numeric_cols or df.columns, key=f"y_{chart_key}")
|
| 173 |
-
|
| 174 |
-
if b:
|
| 175 |
-
st.plotly_chart(px.bar(df, x=x, y=y, title=f"{y} by {x}"), use_container_width=True)
|
| 176 |
-
if p:
|
| 177 |
-
g = df.groupby(x)[y].sum().reset_index() if y in df.columns else df
|
| 178 |
-
st.plotly_chart(px.pie(g, values=y, names=x, title=f"{y} distribution by {x}"), use_container_width=True)
|
| 179 |
-
if l:
|
| 180 |
-
st.plotly_chart(px.line(df, x=x, y=y, title=f"{y} over {x}"), use_container_width=True)
|
| 181 |
-
|
| 182 |
-
# =========================
|
| 183 |
-
# --------- ASR ----------
|
| 184 |
-
# =========================
|
| 185 |
-
asr_ready = False
|
| 186 |
-
try:
|
| 187 |
-
from transformers import pipeline
|
| 188 |
-
import torch
|
| 189 |
-
# Small model for Spaces CPU; disable FP16 on CPU
|
| 190 |
-
asr_device = 0 if torch.cuda.is_available() else -1
|
| 191 |
-
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=asr_device)
|
| 192 |
-
asr_ready = True
|
| 193 |
-
except Exception as e:
|
| 194 |
-
st.info("๐๏ธ To enable voice-to-text, add to requirements: transformers torch soundfile")
|
| 195 |
-
|
| 196 |
-
# =========================
|
| 197 |
-
# ------ Query Chain ------
|
| 198 |
-
# =========================
|
| 199 |
-
|
| 200 |
-
def run_query_chain(user_query: str):
|
| 201 |
-
schema_info = db.get_table_info()
|
| 202 |
-
|
| 203 |
-
# meta/schema questions
|
| 204 |
-
meta_keywords = [
|
| 205 |
-
"list of tables", "show tables", "schema details", "database schema", "table names",
|
| 206 |
-
"what tables", "describe schema", "show schema", "database structure", "column", "columns"
|
| 207 |
-
]
|
| 208 |
-
if any(k in user_query.lower() for k in meta_keywords):
|
| 209 |
-
if debug: st.write("[DEBUG] meta/schema request")
|
| 210 |
-
return None, format_schema_markdown(schema_info)
|
| 211 |
-
|
| 212 |
-
# table preview
|
| 213 |
-
tm = re.search(r'(?:give me|show|display|list|fetch|get) (.+?)(?: details)?$', user_query.lower())
|
| 214 |
-
if tm:
|
| 215 |
-
stop = {"me","the","a","an","all","of","my","our","their","this","that"}
|
| 216 |
-
table_candidate = " ".join([w for w in tm.group(1).split() if w not in stop]).strip()
|
| 217 |
-
for suf in [" table"," data"]:
|
| 218 |
-
if table_candidate.endswith(suf):
|
| 219 |
-
table_candidate = table_candidate[:-len(suf)].strip()
|
| 220 |
-
for t in ["Location","Product","Sales"]:
|
| 221 |
-
if table_candidate.lower() == t.lower():
|
| 222 |
-
try:
|
| 223 |
-
df = pd.read_sql(f'SELECT * FROM "{t}" LIMIT 10', engine)
|
| 224 |
-
st.session_state["last_result_df"] = df
|
| 225 |
-
return f'SELECT * FROM "{t}" LIMIT 10', df
|
| 226 |
-
except Exception as e:
|
| 227 |
-
return None, f"SQL Error: {e}"
|
| 228 |
-
|
| 229 |
-
# follow-up handling
|
| 230 |
-
if st.session_state.get("last_data_question") and is_followup_question(user_query):
|
| 231 |
-
ctx = st.session_state["last_data_question"]
|
| 232 |
-
last_df = st.session_state.get("last_result_df")
|
| 233 |
-
if debug: st.write(f"[DEBUG] follow-up โ {ctx}")
|
| 234 |
-
if isinstance(last_df, pd.DataFrame) and "Product Name" in last_df.columns:
|
| 235 |
-
names = last_df["Product Name"].dropna().astype(str).tolist()[:100]
|
| 236 |
-
if names:
|
| 237 |
-
in_clause = ", ".join([f'"{p}"' for p in names])
|
| 238 |
-
sql = f'SELECT * FROM "Product" WHERE "Product Name" IN ({in_clause})'
|
| 239 |
-
try:
|
| 240 |
-
df = pd.read_sql(sql, engine)
|
| 241 |
-
st.session_state["last_result_df"] = df
|
| 242 |
-
return sql, df
|
| 243 |
-
except Exception as e:
|
| 244 |
-
return sql, f"SQL Error: {e}"
|
| 245 |
-
user_query = f"Previous question: {ctx}\nFollow-up: {user_query}"
|
| 246 |
-
|
| 247 |
-
# prompts.jsonl nearest (string similarity)
|
| 248 |
-
best_example, best_score = None, 0.0
|
| 249 |
try:
|
| 250 |
-
|
| 251 |
-
for line in f:
|
| 252 |
-
ex = json.loads(line)
|
| 253 |
-
s = SequenceMatcher(None, user_query.lower(), ex["prompt"].lower()).ratio()
|
| 254 |
-
if s > best_score:
|
| 255 |
-
best_score, best_example = s, ex
|
| 256 |
except Exception:
|
| 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 |
-
return generated_sql, f"SQL Error: {e}"
|
| 298 |
-
|
| 299 |
-
# =========================
|
| 300 |
-
# ------- Main UI --------
|
| 301 |
-
# =========================
|
| 302 |
-
|
| 303 |
-
col_left, col_right = st.columns([0.8, 1.2], gap="large")
|
| 304 |
-
|
| 305 |
-
with col_left:
|
| 306 |
-
st.subheader("๐ค Voice or ๐ฌ Text โ ask your data")
|
| 307 |
-
|
| 308 |
-
# Mic recorder
|
| 309 |
-
voice = mic_recorder(
|
| 310 |
-
start_prompt="Start Recording",
|
| 311 |
-
stop_prompt="Stop Recording",
|
| 312 |
just_once=True,
|
| 313 |
-
|
| 314 |
)
|
| 315 |
-
if audio:
|
| 316 |
-
st.audio(audio['bytes'])
|
| 317 |
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
try:
|
| 325 |
-
result = asr(wav_path)
|
| 326 |
-
user_query = result.get("text", "").strip()
|
| 327 |
-
st.info(f"๐ฃ You said: **{user_query}**")
|
| 328 |
-
except Exception as e:
|
| 329 |
-
st.error(f"ASR error: {e}")
|
| 330 |
-
|
| 331 |
-
text_query = st.chat_input("Type your question (e.g., 'top 5 products by revenue')โฆ")
|
| 332 |
-
if text_query:
|
| 333 |
-
user_query = text_query
|
| 334 |
-
|
| 335 |
-
st.markdown("---")
|
| 336 |
-
if st.button("Show Database Schema"):
|
| 337 |
-
schema_info = db.get_table_info()
|
| 338 |
-
st.session_state["chat_history"].append({
|
| 339 |
-
"role": "agent", "content": format_schema_markdown(schema_info), "sql": None, "df": None
|
| 340 |
-
})
|
| 341 |
-
|
| 342 |
-
if st.button("Show prompts in vector DB"):
|
| 343 |
-
try:
|
| 344 |
-
data = chroma_collection.get()
|
| 345 |
-
st.write("**Prompts in Vector DB:**")
|
| 346 |
-
for i, doc in enumerate(data.get("documents", [])):
|
| 347 |
-
meta = data.get("metadatas", [])[i]
|
| 348 |
-
st.write(f"{i+1}. {doc}")
|
| 349 |
-
st.code(meta.get("sql",""), language="sql")
|
| 350 |
-
except Exception as e:
|
| 351 |
-
st.warning(f"Vector DB access error: {e}")
|
| 352 |
|
|
|
|
|
|
|
| 353 |
if user_query:
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
with st.container(border=True):
|
| 373 |
-
st.code(sql, language="sql")
|
| 374 |
-
if isinstance(df, pd.DataFrame):
|
| 375 |
-
if df.empty:
|
| 376 |
-
st.info("No rows returned.")
|
| 377 |
-
else:
|
| 378 |
-
if VISUALIZATION_AVAILABLE:
|
| 379 |
-
create_data_visualization(df, f"viz_{hash(sql)}")
|
| 380 |
-
st.dataframe(df, use_container_width=True)
|
| 381 |
-
c1, c2 = st.columns(2)
|
| 382 |
-
with c1:
|
| 383 |
-
st.download_button(
|
| 384 |
-
"Download CSV",
|
| 385 |
-
data=df.to_csv(index=False).encode("utf-8"),
|
| 386 |
-
file_name="query_result.csv",
|
| 387 |
-
mime="text/csv",
|
| 388 |
-
key=f"csv_{hash(sql)}"
|
| 389 |
-
)
|
| 390 |
-
with c2:
|
| 391 |
-
view_name = st.text_input("Save as DB view:", key=f"view_{hash(sql)}")
|
| 392 |
-
if st.button("Save View", key=f"btnv_{hash(sql)}") and view_name:
|
| 393 |
-
safe = "".join(ch for ch in view_name if ch.isalnum() or ch == "_")
|
| 394 |
-
q = f'''CREATE VIEW IF NOT EXISTS "{safe}" AS {sql.strip().rstrip(";")};'''
|
| 395 |
-
try:
|
| 396 |
-
engine.execute(q)
|
| 397 |
-
st.success(f"View '{safe}' created.")
|
| 398 |
-
except Exception as ve:
|
| 399 |
-
st.error(f"View error: {ve}")
|
| 400 |
-
elif isinstance(df, str) and df.startswith("SQL Error:"):
|
| 401 |
-
st.error(df)
|
| 402 |
-
else:
|
| 403 |
-
st.markdown(body)
|
| 404 |
-
if isinstance(df, pd.DataFrame):
|
| 405 |
-
if df.empty:
|
| 406 |
-
st.info("No rows returned.")
|
| 407 |
-
else:
|
| 408 |
-
if VISUALIZATION_AVAILABLE:
|
| 409 |
-
create_data_visualization(df, f"viz_{hash(body)}")
|
| 410 |
-
st.dataframe(df, use_container_width=True)
|
| 411 |
-
st.download_button(
|
| 412 |
-
"Download CSV",
|
| 413 |
-
data=df.to_csv(index=False).encode("utf-8"),
|
| 414 |
-
file_name="result.csv",
|
| 415 |
-
mime="text/csv",
|
| 416 |
-
key=f"csv_{hash(body)}"
|
| 417 |
-
)
|
| 418 |
-
if debug and sql:
|
| 419 |
-
st.caption("๐ฌ Debug: SQL above generated via LLM + schema prompt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import sqlalchemy
|
| 4 |
+
from langchain.schema import Document
|
| 5 |
+
from langchain.vectorstores import Chroma
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from PyPDF2 import PdfReader
|
| 8 |
+
import torch
|
| 9 |
+
import tempfile
|
| 10 |
+
import os
|
| 11 |
+
import numpy as np
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import plotly.express as px
|
| 14 |
+
import openpyxl
|
| 15 |
+
import speech_recognition as sr
|
| 16 |
+
from gtts import gTTS
|
| 17 |
+
import base64
|
| 18 |
from streamlit_mic_recorder import mic_recorder
|
| 19 |
|
| 20 |
+
# ------------------
|
| 21 |
+
# CONFIG
|
| 22 |
+
# ------------------
|
| 23 |
+
CHROMA_DIR = "chromadb_local"
|
| 24 |
+
COLLECTION_NAME = "prompts"
|
| 25 |
+
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
|
| 26 |
+
|
| 27 |
+
# ------------------
|
| 28 |
+
# CHROMA DB FUNCTIONS
|
| 29 |
+
# ------------------
|
| 30 |
+
def get_chroma_client():
|
| 31 |
+
from chromadb import Client
|
| 32 |
+
from chromadb.config import Settings
|
| 33 |
+
return Client(Settings(persist_directory=CHROMA_DIR))
|
| 34 |
+
|
| 35 |
+
def get_chroma_collection(client):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
+
return client.get_collection(COLLECTION_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception:
|
| 39 |
+
return client.create_collection(COLLECTION_NAME)
|
| 40 |
+
|
| 41 |
+
def get_embedding_model():
|
| 42 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
+
cache_dir = tempfile.mkdtemp()
|
| 44 |
+
return SentenceTransformer(EMBEDDING_MODEL, device=device, cache_folder=cache_dir)
|
| 45 |
+
|
| 46 |
+
# ------------------
|
| 47 |
+
# SPEECH TO TEXT
|
| 48 |
+
# ------------------
|
| 49 |
+
def speech_to_text(audio_bytes):
|
| 50 |
+
recognizer = sr.Recognizer()
|
| 51 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_wav:
|
| 52 |
+
tmp_wav.write(audio_bytes)
|
| 53 |
+
tmp_wav.flush()
|
| 54 |
+
with sr.AudioFile(tmp_wav.name) as source:
|
| 55 |
+
audio_data = recognizer.record(source)
|
| 56 |
+
try:
|
| 57 |
+
text = recognizer.recognize_google(audio_data)
|
| 58 |
+
return text
|
| 59 |
+
except sr.UnknownValueError:
|
| 60 |
+
return "Sorry, could not understand the audio."
|
| 61 |
+
except sr.RequestError:
|
| 62 |
+
return "Speech Recognition service error."
|
| 63 |
+
|
| 64 |
+
# ------------------
|
| 65 |
+
# MAIN APP
|
| 66 |
+
# ------------------
|
| 67 |
+
st.set_page_config(page_title="Talk2Data - MultiModal RAG", layout="wide")
|
| 68 |
+
st.title("๐๏ธ Talk2Data โ MultiModal RAG")
|
| 69 |
+
|
| 70 |
+
# Mic Recorder
|
| 71 |
+
col1, col2 = st.columns([2, 1])
|
| 72 |
+
with col1:
|
| 73 |
+
user_query = st.text_input("๐ฌ Enter your query:")
|
| 74 |
+
with col2:
|
| 75 |
+
st.write("๐ค Or speak your query")
|
| 76 |
+
audio = mic_recorder(
|
| 77 |
+
start_prompt="๐๏ธ Start Recording",
|
| 78 |
+
stop_prompt="โน Stop Recording",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
just_once=True,
|
| 80 |
+
key="voice"
|
| 81 |
)
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
if audio:
|
| 84 |
+
st.audio(audio["bytes"])
|
| 85 |
+
transcribed_text = speech_to_text(audio["bytes"])
|
| 86 |
+
st.success(f"**Transcribed Query:** {transcribed_text}")
|
| 87 |
+
if not user_query:
|
| 88 |
+
user_query = transcribed_text # If no typed query, use voice
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# RAG Process
|
| 91 |
+
if st.button("Run Query"):
|
| 92 |
if user_query:
|
| 93 |
+
st.info(f"Processing: {user_query}")
|
| 94 |
+
# Here you integrate with your existing SQL + Vector Search pipeline
|
| 95 |
+
# Example:
|
| 96 |
+
client = get_chroma_client()
|
| 97 |
+
collection = get_chroma_collection(client)
|
| 98 |
+
model = get_embedding_model()
|
| 99 |
+
|
| 100 |
+
# Vector embedding
|
| 101 |
+
query_embedding = model.encode([user_query])[0]
|
| 102 |
+
# Dummy search (replace with actual retrieval)
|
| 103 |
+
results = collection.query(
|
| 104 |
+
query_embeddings=[query_embedding],
|
| 105 |
+
n_results=5
|
| 106 |
+
)
|
| 107 |
+
st.write("### Results")
|
| 108 |
+
st.write(results)
|
| 109 |
+
else:
|
| 110 |
+
st.warning("Please type or speak your query first.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|