prithvi55 commited on
Commit
b67007b
ยท
verified ยท
1 Parent(s): 1bdc8f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -409
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
- from difflib import SequenceMatcher
10
- from langchain.chains import LLMChain
11
- from langchain.prompts import PromptTemplate
12
-
13
- # Voice recorder
14
- # from st_mic_recorder import mic_recorder
 
 
 
 
 
 
 
 
 
 
15
  from streamlit_mic_recorder import mic_recorder
16
 
17
- # โœ… Fix OpenMP library conflict (common with PyTorch/NumPy on HF Spaces)
18
- os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
19
-
20
- # --- Visualization imports ---
21
- try:
22
- import plotly.express as px
23
- VISUALIZATION_AVAILABLE = True
24
- except Exception:
25
- st.warning("๐Ÿ“Š Visualization libraries not available. Install: pip install plotly")
26
- VISUALIZATION_AVAILABLE = False
27
-
28
- # --- Modular Imports (your local utils) ---
29
- from db_utils import get_engine, get_db
30
- from llm_utils import get_llm, get_sql_prompt
31
- from chroma_utils import (
32
- get_chroma_client, get_chroma_collection, get_embedding_model,
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
- with open(PROMPT_JSONL_PATH, "r", encoding="utf-8") as f:
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
- pass
258
-
259
- if best_example and best_score > 0.85:
260
- try:
261
- df = pd.read_sql(best_example["sql"], engine)
262
- st.session_state["last_result_df"] = df
263
- return best_example["sql"], df
264
- except Exception as e:
265
- return best_example["sql"], f"SQL Error: {e}"
266
-
267
- # generate SQL via LLM
268
- sql_generation_chain = LLMChain(llm=llm, prompt=sql_prompt)
269
- generated_sql = sql_generation_chain.invoke({"question": user_query, "schema": schema_info})
270
- if isinstance(generated_sql, dict):
271
- for k in ["sql","text","output","result"]:
272
- if k in generated_sql and isinstance(generated_sql[k], str):
273
- generated_sql = generated_sql[k]; break
274
- else:
275
- generated_sql = str(generated_sql)
276
-
277
- # extract first SELECT
278
- if not str(generated_sql).strip().lower().startswith("select"):
279
- for line in str(generated_sql).splitlines():
280
- if line.strip().lower().startswith("select"):
281
- generated_sql = line.strip(); break
282
-
283
- if not str(generated_sql).strip().lower().startswith("select"):
284
- return None, "No SQL query could be generated. Please rephrase."
285
-
286
- # run SQL
287
- try:
288
- df = pd.read_sql(generated_sql, engine)
289
- st.session_state["last_data_question"] = (
290
- user_query if not is_followup_question(user_query) else st.session_state.get("last_data_question", user_query)
291
- )
292
- st.session_state["last_result_df"] = df
293
- with open(PROMPT_JSONL_PATH, "a", encoding="utf-8") as f:
294
- f.write(json.dumps({"prompt": user_query, "sql": generated_sql}) + "\n")
295
- return generated_sql, df
296
- except Exception as e:
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
- use_container_width=True
314
  )
315
- if audio:
316
- st.audio(audio['bytes'])
317
 
318
- user_query = None
319
- if voice and asr_ready:
320
- st.success("โœ… Audio captured โ€” transcribing...")
321
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
322
- tmp.write(voice["bytes"])
323
- wav_path = tmp.name
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
- sql, df = run_query_chain(user_query)
355
- st.session_state["chat_history"].append({"role":"user","content":user_query,"sql":None,"df":None})
356
- st.session_state["chat_history"].append({"role":"agent","content":"Here is the generated SQL and results.","sql":sql,"df":df})
357
-
358
- with col_right:
359
- st.subheader("๐Ÿ“ฃ Results & Visuals")
360
- for msg in st.session_state["chat_history"][-10:]:
361
- if msg["role"] == "user":
362
- with st.chat_message("user"):
363
- st.markdown(msg.get("content",""))
364
- else:
365
- with st.chat_message("assistant"):
366
- sql = msg.get("sql")
367
- df = msg.get("df")
368
- body = msg.get("content","")
369
-
370
- if sql and isinstance(sql, str) and sql.strip().lower().startswith("select"):
371
- st.markdown(body)
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.")