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