import streamlit as st import pandas as pd import sqlalchemy from langchain.schema import Document from langchain.vectorstores import Chroma from sentence_transformers import SentenceTransformer from PyPDF2 import PdfReader import torch import tempfile import os import numpy as np import matplotlib.pyplot as plt import plotly.express as px import openpyxl import speech_recognition as sr from gtts import gTTS import base64 from streamlit_mic_recorder import mic_recorder # ------------------ # CONFIG # ------------------ CHROMA_DIR = "chromadb_local" COLLECTION_NAME = "prompts" EMBEDDING_MODEL = "all-MiniLM-L6-v2" # ------------------ # CHROMA DB FUNCTIONS # ------------------ def get_chroma_client(): from chromadb import Client from chromadb.config import Settings return Client(Settings(persist_directory=CHROMA_DIR)) def get_chroma_collection(client): try: return client.get_collection(COLLECTION_NAME) except Exception: return client.create_collection(COLLECTION_NAME) def get_embedding_model(): device = "cuda" if torch.cuda.is_available() else "cpu" cache_dir = tempfile.mkdtemp() return SentenceTransformer(EMBEDDING_MODEL, device=device, cache_folder=cache_dir) # ------------------ # SPEECH TO TEXT # ------------------ def speech_to_text(audio_bytes): recognizer = sr.Recognizer() with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_wav: tmp_wav.write(audio_bytes) tmp_wav.flush() with sr.AudioFile(tmp_wav.name) as source: audio_data = recognizer.record(source) try: text = recognizer.recognize_google(audio_data) return text except sr.UnknownValueError: return "Sorry, could not understand the audio." except sr.RequestError: return "Speech Recognition service error." # ------------------ # MAIN APP # ------------------ st.set_page_config(page_title="Talk2Data - MultiModal RAG", layout="wide") st.title("🎙️ Talk2Data – MultiModal RAG") # Mic Recorder col1, col2 = st.columns([2, 1]) with col1: user_query = st.text_input("💬 Enter your query:") with col2: st.write("🎤 Or speak your query") audio = mic_recorder( start_prompt="🎙️ Start Recording", stop_prompt="⏹ Stop Recording", just_once=True, key="voice" ) if audio: st.audio(audio["bytes"]) transcribed_text = speech_to_text(audio["bytes"]) st.success(f"**Transcribed Query:** {transcribed_text}") if not user_query: user_query = transcribed_text # If no typed query, use voice # RAG Process if st.button("Run Query"): if user_query: st.info(f"Processing: {user_query}") # Here you integrate with your existing SQL + Vector Search pipeline # Example: client = get_chroma_client() collection = get_chroma_collection(client) model = get_embedding_model() # Vector embedding query_embedding = model.encode([user_query])[0] # Dummy search (replace with actual retrieval) results = collection.query( query_embeddings=[query_embedding], n_results=5 ) st.write("### Results") st.write(results) else: st.warning("Please type or speak your query first.")