AnnaMathews commited on
Commit
6d609ea
·
verified ·
1 Parent(s): 4bccc04

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 transformer model
10
+ model = SentenceTransformer('all-MiniLM-L6-v2')
11
+
12
+ # Load and prepare the FAQ data
13
+ def load_static_csv():
14
+ df = pd.read_csv("faq.csv")
15
+ df.columns = ['question', 'answer']
16
+ return df
17
+
18
+ # Load data and build FAISS index
19
+ data = load_static_csv()
20
+ question_embeddings = model.encode(data['question'].tolist())
21
+ faq_index = faiss.IndexFlatL2(question_embeddings.shape[1])
22
+ faq_index.add(np.array(question_embeddings))
23
+
24
+ # Function to return answer text and audio file
25
+ def ask_question(query, k=1):
26
+ query_embedding = model.encode([query])
27
+ D, I = faq_index.search(np.array(query_embedding), k=k)
28
+ results = ""
29
+ for idx in I[0]:
30
+ a = data.iloc[idx]['answer']
31
+ results += f"{a}\n\n"
32
+ results = results.strip()
33
+
34
+ # Convert text to speech
35
+ tts = gTTS(text=results, lang='en')
36
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
37
+ tts.save(temp_file.name)
38
+
39
+ return results, temp_file.name
40
+ custom_css = """
41
+ body, html, .gradio-container {
42
+ background-color: #FFDDEE !important; /* baby pink */
43
+ color: black !important;
44
+ font-family: 'Segoe UI', sans-serif;
45
+ }
46
+ /* Title */
47
+ h1 {
48
+ color: black !important;
49
+ text-align: center;
50
+ font-weight: 700;
51
+ margin-top: 20px;
52
+ }
53
+ /* Subheading text */
54
+ .gr-markdown > p {
55
+ color: black !important;
56
+ text-align: center;
57
+ font-size: 16px;
58
+ font-weight: 500;
59
+ }
60
+ /* Labels and inputs */
61
+ label {
62
+ color: black !important;
63
+ font-weight: bold;
64
+ }
65
+ textarea, input[type="text"] {
66
+ border-radius: 10px !important;
67
+ padding: 10px;
68
+ border: 1px solid #aaa;
69
+ background-color: white;
70
+ color: black;
71
+ }
72
+ /* Button */
73
+ button {
74
+ background-color: white !important;
75
+ color: black !important;
76
+ font-weight: bold;
77
+ border-radius: 10px;
78
+ padding: 10px 20px;
79
+ border: 2px solid black;
80
+ margin-top: 10px;
81
+ cursor: pointer;
82
+ }
83
+ /* Answer box */
84
+ #answer-box {
85
+ background-color: white;
86
+ color: black !important;
87
+ border-radius: 12px;
88
+ padding: 16px;
89
+ font-size: 16px;
90
+ border: 1px solid #ccc;
91
+ box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
92
+ margin-top: 10px;
93
+ }
94
+ #answer-box p {
95
+ color: black !important;
96
+ }
97
+ /* AUDIO PLAYER FIXES */
98
+ audio {
99
+ width: 100% !important;
100
+ }
101
+ .gr-audio {
102
+ background-color: white;
103
+ border-radius: 10px;
104
+ padding: 16px;
105
+ border: 1px solid #bbb;
106
+ margin-top: 10px;
107
+ }
108
+ /* Target speed and audio buttons better */
109
+ .gr-audio .speed-button,
110
+ .gr-audio .audio-button {
111
+ border: 2px solid black;
112
+ color: black !important;
113
+ background-color: white !important;
114
+ border-radius: 8px;
115
+ padding: 4px 10px;
116
+ font-weight: bold;
117
+ min-width: 48px;
118
+ text-align: center;
119
+ display: inline-block;
120
+ }
121
+ .gr-audio .speed-button span {
122
+ color: black !important;
123
+ font-weight: bold;
124
+ display: inline-block;
125
+ width: 100%;
126
+ text-align: center;
127
+ }
128
+ /* Make sure play/pause button is visible */
129
+ .gr-audio .play-button svg,
130
+ .gr-audio .pause-button svg {
131
+ fill: black !important;
132
+ height: 24px;
133
+ width: 24px;
134
+ }
135
+ """
136
+
137
+
138
+ #Gradio UI
139
+ with gr.Blocks(css=custom_css) as demo:
140
+ gr.Markdown(
141
+ "<h1>🧘 MentalWell Q&A</h1>"
142
+ "<p>Ask any mental health related question based on our FAQ knowledge base.</p>"
143
+ )
144
+
145
+ with gr.Column():
146
+ query_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here…", lines=1)
147
+ ask_button = gr.Button("Get Answer")
148
+
149
+ with gr.Column():
150
+ output_text = gr.Markdown(elem_id="answer-box")
151
+ output_audio = gr.Audio(label="Listen", type="filepath")
152
+
153
+ ask_button.click(fn=ask_question, inputs=query_input, outputs=[output_text, output_audio])
154
+ query_input.submit(fn=ask_question, inputs=query_input, outputs=[output_text, output_audio])
155
+
156
+ demo.launch()