Rishitha3 commited on
Commit
0bc80fc
Β·
verified Β·
1 Parent(s): 784e59a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -9
app.py CHANGED
@@ -9,16 +9,17 @@ from transformers import pipeline
9
  # =============================
10
  # 1. Hugging Face Authentication
11
  # =============================
12
- HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set: export HF_TOKEN="your_token_here"
13
  if HF_TOKEN is None:
14
  raise ValueError("⚠️ Please set your HF_TOKEN as an environment variable.")
15
 
16
  # =============================
17
- # 2. Load embedding + QA model
18
  # =============================
 
19
  embedding_model = SentenceTransformer(
20
  "sentence-transformers/all-MiniLM-L6-v2",
21
- use_auth_token=HF_TOKEN
22
  )
23
  qa_model = pipeline(
24
  "text-generation",
@@ -27,6 +28,20 @@ qa_model = pipeline(
27
  device_map="auto"
28
  )
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # =============================
31
  # 3. Helper: extract text from files
32
  # =============================
@@ -95,13 +110,31 @@ def answer_query(query):
95
  return response
96
 
97
  # =============================
98
- # 8. Gradio UI (Visually Appealing)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  # =============================
100
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="cyan")) as demo:
101
  gr.Markdown("""
102
- # πŸ“š HyDE RAG Chatbot
103
- Talk with your documents using **Hypothetical Document Embeddings (HyDE)**.
104
- Upload a PDF/DOCX/TXT and start asking questions!
105
  """)
106
 
107
  with gr.Row():
@@ -111,12 +144,18 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="cyan"))
111
  status = gr.Textbox(label="Status", interactive=False)
112
 
113
  with gr.Column(scale=2):
114
- query = gr.Textbox(label="❓ Ask a Question", placeholder="Type your question here...")
115
  ask_btn = gr.Button("πŸš€ Get Answer", variant="primary")
116
  answer = gr.Textbox(label="πŸ’‘ Answer", lines=6)
117
 
 
 
 
 
 
 
118
  upload_btn.click(upload_file, inputs=file_input, outputs=status)
119
  ask_btn.click(answer_query, inputs=query, outputs=answer)
 
120
 
121
  demo.launch()
122
-
 
9
  # =============================
10
  # 1. Hugging Face Authentication
11
  # =============================
12
+ HF_TOKEN = os.getenv("HF_TOKEN") # export HF_TOKEN="your_token_here"
13
  if HF_TOKEN is None:
14
  raise ValueError("⚠️ Please set your HF_TOKEN as an environment variable.")
15
 
16
  # =============================
17
+ # 2. Load Models
18
  # =============================
19
+ # Embedding + QA
20
  embedding_model = SentenceTransformer(
21
  "sentence-transformers/all-MiniLM-L6-v2",
22
+ use_auth_token=HF_TOKEN
23
  )
24
  qa_model = pipeline(
25
  "text-generation",
 
28
  device_map="auto"
29
  )
30
 
31
+ # Speech-to-Text (Whisper small, lightweight)
32
+ stt_model = pipeline(
33
+ "automatic-speech-recognition",
34
+ model="openai/whisper-small",
35
+ token=HF_TOKEN
36
+ )
37
+
38
+ # Text-to-Speech (VITS)
39
+ tts_model = pipeline(
40
+ "text-to-speech",
41
+ model="espnet/kan-bayashi_ljspeech_vits",
42
+ token=HF_TOKEN
43
+ )
44
+
45
  # =============================
46
  # 3. Helper: extract text from files
47
  # =============================
 
110
  return response
111
 
112
  # =============================
113
+ # 8. Voice-enabled Query
114
+ # =============================
115
+ def voice_query(audio):
116
+ if audio is None:
117
+ return "⚠️ Please record your question.", None
118
+
119
+ # Step 1: Speech-to-Text
120
+ stt_result = stt_model(audio)
121
+ text_query = stt_result["text"]
122
+
123
+ # Step 2: Get Answer from RAG
124
+ answer = answer_query(text_query)
125
+
126
+ # Step 3: Text-to-Speech
127
+ tts_result = tts_model(answer)
128
+ return answer, (tts_result["audio"], tts_result["sampling_rate"])
129
+
130
+ # =============================
131
+ # 9. Gradio UI (Visually Appealing)
132
  # =============================
133
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="cyan")) as demo:
134
  gr.Markdown("""
135
+ # πŸ“š HyDE RAG Chatbot + 🎀 Voice Assistant
136
+ Talk with your documents using **Hypothetical Document Embeddings (HyDE)**.
137
+ Upload a PDF/DOCX/TXT and start asking questions by **typing or speaking**!
138
  """)
139
 
140
  with gr.Row():
 
144
  status = gr.Textbox(label="Status", interactive=False)
145
 
146
  with gr.Column(scale=2):
147
+ query = gr.Textbox(label="❓ Ask a Question (Text)", placeholder="Type your question here...")
148
  ask_btn = gr.Button("πŸš€ Get Answer", variant="primary")
149
  answer = gr.Textbox(label="πŸ’‘ Answer", lines=6)
150
 
151
+ gr.Markdown("### 🎀 Or Ask by Voice")
152
+ mic_input = gr.Audio(sources=["microphone"], type="filepath", label="Speak your question")
153
+ voice_answer = gr.Textbox(label="πŸ’‘ Answer (from voice)", lines=6)
154
+ voice_output = gr.Audio(label="πŸ”Š Bot Voice Reply")
155
+
156
+ # Events
157
  upload_btn.click(upload_file, inputs=file_input, outputs=status)
158
  ask_btn.click(answer_query, inputs=query, outputs=answer)
159
+ mic_input.change(voice_query, inputs=mic_input, outputs=[voice_answer, voice_output])
160
 
161
  demo.launch()