Spaces:
Build error
Build error
Shashwat2528
commited on
Commit
·
e3f2401
1
Parent(s):
1cd9e21
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torchaudio
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
from gtts import gTTS
|
5 |
+
import tempfile
|
6 |
+
import pygame
|
7 |
+
import time
|
8 |
+
|
9 |
+
# Initialize the speech-to-text transcriber
|
10 |
+
transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-english")
|
11 |
+
|
12 |
+
# Load the pre-trained question answering model
|
13 |
+
model_name = "AVISHKAARAM/avishkaarak-ekta-hindi"
|
14 |
+
qa_model = pipeline("question-answering", model=model_name)
|
15 |
+
|
16 |
+
def answer_question(context, question=None, audio=None):
|
17 |
+
if audio is not None:
|
18 |
+
text = transcriber(audio)
|
19 |
+
question_text = text['text']
|
20 |
+
else:
|
21 |
+
question_text = question
|
22 |
+
|
23 |
+
qa_result = qa_model(question=question_text, context=context)
|
24 |
+
answer = qa_result["answer"]
|
25 |
+
|
26 |
+
tts = gTTS(text=answer, lang='en')
|
27 |
+
audio_path = tempfile.NamedTemporaryFile(suffix=".mp3").name
|
28 |
+
tts.save(audio_path)
|
29 |
+
|
30 |
+
return answer, audio_path
|
31 |
+
|
32 |
+
def play_audio(audio_path):
|
33 |
+
pygame.mixer.init()
|
34 |
+
pygame.mixer.music.load(audio_path)
|
35 |
+
pygame.mixer.music.play()
|
36 |
+
while pygame.mixer.music.get_busy():
|
37 |
+
time.sleep(0.1)
|
38 |
+
|
39 |
+
# Define the Gradio interface
|
40 |
+
context_input = gr.components.Textbox(label="Context")
|
41 |
+
question_input = gr.components.Textbox(label="Question")
|
42 |
+
audio_input = gr.components.Audio(source="microphone", type="filepath")
|
43 |
+
|
44 |
+
output_text = gr.components.Textbox(label="Answer")
|
45 |
+
output_audio = gr.components.Audio(label="Answer Audio", type="numpy")
|
46 |
+
|
47 |
+
interface = gr.Interface(
|
48 |
+
fn=answer_question,
|
49 |
+
inputs=[context_input, question_input, audio_input],
|
50 |
+
outputs=[output_text, output_audio],
|
51 |
+
title="Question Answering",
|
52 |
+
description="Enter a context and a question to get an answer. You can also upload an audio file with the question.",
|
53 |
+
examples=[
|
54 |
+
["The capital of France is Paris.", "What is the capital of France?"],
|
55 |
+
["OpenAI is famous for developing GPT-3.", "What is OpenAI known for?"],
|
56 |
+
]
|
57 |
+
)
|
58 |
+
# Launch the Gradio interface
|
59 |
+
interface.launch()
|