Spaces:
Sleeping
Sleeping
Download app_sound.py from prithvi55/talk2data: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/spaces/prithvi55/talk2data/resolve/main/app_sound.py
- Command line
-
hf download hf://spaces/prithvi55/talk2data/app_sound.py
-
curl -L -o app_sound.py https://huggingface.co/spaces/prithvi55/talk2data/resolve/main/app_sound.py
3.38 kB
| 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.") | |