Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# STEP 1: Load your CSV data
|
| 10 |
+
data = pd.read_csv("/content/data.csv")
|
| 11 |
+
|
| 12 |
+
# STEP 2: Clean the data
|
| 13 |
+
data = data.dropna(subset=["question", "answer"]).reset_index(drop=True)
|
| 14 |
+
data["question"] = data["question"].astype(str)
|
| 15 |
+
data["answer"] = data["answer"].astype(str)
|
| 16 |
+
|
| 17 |
+
# STEP 3: Load sentence embedding model
|
| 18 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 19 |
+
|
| 20 |
+
# STEP 4: Generate embeddings and FAISS index
|
| 21 |
+
embeddings = model.encode(data['question'].tolist())
|
| 22 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 23 |
+
index.add(np.array(embeddings))
|
| 24 |
+
|
| 25 |
+
# ✅ STEP 5: Define the function with TTS
|
| 26 |
+
def answer_with_audio(user_input):
|
| 27 |
+
query_embedding = model.encode([user_input])
|
| 28 |
+
_, I = index.search(np.array(query_embedding), k=1)
|
| 29 |
+
answer = data.iloc[I[0][0]]['answer']
|
| 30 |
+
|
| 31 |
+
# Convert answer to speech using gTTS
|
| 32 |
+
tts = gTTS(answer)
|
| 33 |
+
tts_path = "/tmp/tts_output.mp3"
|
| 34 |
+
tts.save(tts_path)
|
| 35 |
+
|
| 36 |
+
return answer, tts_path
|
| 37 |
+
|
| 38 |
+
# ✅ STEP 6: Create Gradio interface with text + audio output
|
| 39 |
+
gr.Interface(
|
| 40 |
+
fn=answer_with_audio,
|
| 41 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask your question..."),
|
| 42 |
+
outputs=["text", "audio"],
|
| 43 |
+
title="🔊 Cooking FAQ ",
|
| 44 |
+
description="Ask your question and listen to the response.",
|
| 45 |
+
).launch()
|