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