sreelakshmimukkizhi commited on
Commit
c4bce3c
·
verified ·
1 Parent(s): 84909de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py CHANGED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import faiss
4
+ import gradio as gr
5
+ from sentence_transformers import SentenceTransformer
6
+ from gtts import gTTS
7
+ import tempfile
8
+
9
+ # Load CSV
10
+ df = pd.read_csv("Mental_Health_FAQ.csv")
11
+ df = df.rename(columns={'Questions': 'question', 'Answers': 'answer'})
12
+
13
+ # Load embedding model
14
+ model = SentenceTransformer('all-MiniLM-L6-v2')
15
+
16
+ # Build FAISS index
17
+ question_embeddings = model.encode(df['question'].tolist())
18
+ faq_index = faiss.IndexFlatL2(question_embeddings.shape[1])
19
+ faq_index.add(np.array(question_embeddings))
20
+
21
+
22
+ # Function to handle user input
23
+ def ask_question(query, k=1):
24
+ if not query.strip():
25
+ return "❌ Please enter a valid question.", None
26
+
27
+ query_embedding = model.encode([query])
28
+ D, I = faq_index.search(np.array(query_embedding), k=k)
29
+
30
+ results = ""
31
+ for idx in I[0]:
32
+ a = df.iloc[idx]['answer']
33
+ results += f"{a}\n\n"
34
+ results = results.strip()
35
+
36
+ # Convert answer to speech
37
+ tts = gTTS(text=results, lang='en')
38
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
39
+ tts.save(temp_file.name)
40
+
41
+ return results, temp_file.name
42
+
43
+
44
+ # Custom CSS for Gradio
45
+ custom_css = """
46
+ body, html, .gradio-container {
47
+ background-color: #ffffff !important;
48
+ color: #000000 !important;
49
+ font-family: 'Segoe UI', sans-serif;
50
+ padding: 20px;
51
+ }
52
+ h1 {
53
+ text-align: center;
54
+ color: #222222 !important;
55
+ font-weight: 800;
56
+ font-size: 2.4rem;
57
+ }
58
+ .gr-markdown > p {
59
+ text-align: center;
60
+ font-size: 1rem;
61
+ font-weight: 500;
62
+ color: #444444;
63
+ }
64
+ label {
65
+ color: #000000 !important;
66
+ font-weight: bold;
67
+ font-size: 0.95rem;
68
+ }
69
+ textarea, input[type="text"] {
70
+ border-radius: 12px !important;
71
+ padding: 14px;
72
+ font-size: 1rem;
73
+ border: 1px solid #ccc;
74
+ background-color: #ffffff;
75
+ color: #000000;
76
+ box-shadow: 0 1px 3px rgba(0,0,0,0.1);
77
+ }
78
+ button {
79
+ background: #000000 !important;
80
+ color: #ffffff !important;
81
+ font-weight: bold;
82
+ border-radius: 10px;
83
+ padding: 12px 24px;
84
+ border: none;
85
+ margin-top: 10px;
86
+ cursor: pointer;
87
+ transition: all 0.3s ease;
88
+ }
89
+ button:hover {
90
+ background: #333333 !important;
91
+ }
92
+ #answer-box {
93
+ background-color: #f9f9f9;
94
+ border-radius: 12px;
95
+ padding: 18px;
96
+ font-size: 1rem;
97
+ color: #111111 !important;
98
+ border: 1px solid #ddd;
99
+ box-shadow: 0 2px 6px rgba(0,0,0,0.05);
100
+ margin-top: 20px;
101
+ }
102
+ audio {
103
+ width: 100% !important;
104
+ margin-top: 10px;
105
+ }
106
+ .gr-audio {
107
+ background-color: #f9f9f9;
108
+ border-radius: 10px;
109
+ padding: 14px;
110
+ border: 1px solid #ccc;
111
+ box-shadow: 0 2px 4px rgba(0,0,0,0.05);
112
+ margin-top: 10px;
113
+ }
114
+ """
115
+
116
+ # Gradio UI
117
+ with gr.Blocks(css=custom_css) as demo:
118
+ gr.Markdown(
119
+ "<h1>🧠 Welcome to MentalWell Assistant</h1>"
120
+ "<p>Type any question about mental health and get a quick, accurate answer — and even listen to it aloud!</p>"
121
+ )
122
+
123
+ with gr.Column():
124
+ query_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here…", lines=1)
125
+ ask_button = gr.Button("Get Answer")
126
+
127
+ with gr.Column():
128
+ output_text = gr.Markdown(elem_id="answer-box")
129
+ output_audio = gr.Audio(label="🔊 Listen to Answer", type="filepath")
130
+
131
+ ask_button.click(fn=ask_question, inputs=query_input, outputs=[output_text, output_audio])
132
+ query_input.submit(fn=ask_question, inputs=query_input, outputs=[output_text, output_audio])
133
+
134
+ demo.launch(share=True)