Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import faiss
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# Load data
|
| 10 |
+
data = pd.read_csv("Mental_Health_FAQ.csv")
|
| 11 |
+
|
| 12 |
+
# Rename columns if necessary
|
| 13 |
+
data.rename(columns={
|
| 14 |
+
'Questions': 'question',
|
| 15 |
+
'Answers': 'answer'
|
| 16 |
+
}, inplace=True)
|
| 17 |
+
|
| 18 |
+
# Load model and build embeddings
|
| 19 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 20 |
+
faq_embeddings = np.array(model.encode(data['question'].tolist()))
|
| 21 |
+
|
| 22 |
+
# Create FAISS index
|
| 23 |
+
faq_index = faiss.IndexFlatL2(faq_embeddings.shape[1])
|
| 24 |
+
faq_index.add(faq_embeddings)
|
| 25 |
+
|
| 26 |
+
# Define query function
|
| 27 |
+
def gradio_faq(query):
|
| 28 |
+
query_embedding = model.encode([query])
|
| 29 |
+
D, I = faq_index.search(np.array(query_embedding), 3)
|
| 30 |
+
|
| 31 |
+
results = ""
|
| 32 |
+
for rank, idx in enumerate(I[0]):
|
| 33 |
+
question = data.iloc[idx]['question']
|
| 34 |
+
answer = data.iloc[idx]['answer']
|
| 35 |
+
results += f"🔹 **Q{rank+1}:** {question}\n**A:** {answer}\n\n"
|
| 36 |
+
|
| 37 |
+
return results.strip()
|
| 38 |
+
|
| 39 |
+
# Launch Gradio interface
|
| 40 |
+
gr.Interface(
|
| 41 |
+
fn=gradio_faq,
|
| 42 |
+
inputs=gr.Textbox(label="Ask a Mental Health Question"),
|
| 43 |
+
outputs=gr.Markdown(label="Top Answers"),
|
| 44 |
+
title="🧠 Mental Health FAQ Assistant",
|
| 45 |
+
description="Type your question to get the most relevant answers from the FAQ dataset."
|
| 46 |
+
).launch()
|