Download app.py from varun1128/FAQ_bot: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/spaces/varun1128/FAQ_bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/varun1128/FAQ_bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/varun1128/FAQ_bot/resolve/main/app.py
1.29 kB
| # app.py | |
| import pandas as pd | |
| import numpy as np | |
| import faiss | |
| from sentence_transformers import SentenceTransformer | |
| import gradio as gr | |
| # Load data | |
| data = pd.read_csv("Mental_Health_FAQ.csv") | |
| # Rename columns if necessary | |
| data.rename(columns={ | |
| 'Questions': 'question', | |
| 'Answers': 'answer' | |
| }, inplace=True) | |
| # Load model and build embeddings | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| faq_embeddings = np.array(model.encode(data['question'].tolist())) | |
| # Create FAISS index | |
| faq_index = faiss.IndexFlatL2(faq_embeddings.shape[1]) | |
| faq_index.add(faq_embeddings) | |
| # Define query function | |
| def gradio_faq(query): | |
| query_embedding = model.encode([query]) | |
| D, I = faq_index.search(np.array(query_embedding), 3) | |
| results = "" | |
| for rank, idx in enumerate(I[0]): | |
| question = data.iloc[idx]['question'] | |
| answer = data.iloc[idx]['answer'] | |
| results += f"🔹 **Q{rank+1}:** {question}\n**A:** {answer}\n\n" | |
| return results.strip() | |
| # Launch Gradio interface | |
| gr.Interface( | |
| fn=gradio_faq, | |
| inputs=gr.Textbox(label="Ask a Mental Health Question"), | |
| outputs=gr.Markdown(label="Top Answers"), | |
| title="🧠 Mental Health FAQ Assistant", | |
| description="Type your question to get the most relevant answers from the FAQ dataset." | |
| ).launch() | |