techysanoj commited on
Commit
8899039
·
verified ·
1 Parent(s): 47c9700

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -61,9 +61,8 @@
61
  import torchaudio
62
  import gradio as gr
63
  from transformers import pipeline
64
- from gtts import gTTS
65
  import tempfile
66
- import pygame
67
  import time
68
 
69
  # Initialize the speech-to-text transcriber
@@ -73,29 +72,28 @@ transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav
73
  model_name = "AVISHKAARAM/avishkaarak-ekta-hindi"
74
  qa_model = pipeline("question-answering", model=model_name)
75
 
 
 
 
76
  def answer_question(context, question=None, audio=None):
 
77
  if audio is not None:
78
  text = transcriber(audio)["text"]
79
  question_text = text
80
  else:
81
  question_text = question
82
 
 
83
  qa_result = qa_model(question=question_text, context=context)
84
  answer = qa_result["answer"]
85
 
86
- tts = gTTS(text=answer, lang='en')
87
- audio_path = tempfile.NamedTemporaryFile(suffix=".mp3").name
88
- tts.save(audio_path)
 
89
 
90
  return answer, audio_path
91
 
92
- def play_audio(audio_path):
93
- pygame.mixer.init()
94
- pygame.mixer.music.load(audio_path)
95
- pygame.mixer.music.play()
96
- while pygame.mixer.music.get_busy():
97
- time.sleep(0.1)
98
-
99
  # Define the Gradio interface
100
  context_input = gr.Textbox(label="Context")
101
  question_input = gr.Textbox(label="Question")
@@ -111,10 +109,11 @@ inter = gr.Interface(
111
  title="Question Answering",
112
  description="Enter a context and a question to get an answer. You can also record audio with the question.",
113
  examples=[
114
- ["The capital of France is Paris.", "What is the capital of France?"],
115
- ["OpenAI is famous for developing GPT-3.", "What is OpenAI known for?"],
116
  ]
117
  )
118
 
119
  # Launch the Gradio interface
120
  inter.launch()
 
 
61
  import torchaudio
62
  import gradio as gr
63
  from transformers import pipeline
64
+ import pyttsx3
65
  import tempfile
 
66
  import time
67
 
68
  # Initialize the speech-to-text transcriber
 
72
  model_name = "AVISHKAARAM/avishkaarak-ekta-hindi"
73
  qa_model = pipeline("question-answering", model=model_name)
74
 
75
+ # Initialize pyttsx3 TTS
76
+ engine = pyttsx3.init()
77
+
78
  def answer_question(context, question=None, audio=None):
79
+ # Handle audio input
80
  if audio is not None:
81
  text = transcriber(audio)["text"]
82
  question_text = text
83
  else:
84
  question_text = question
85
 
86
+ # Generate the answer
87
  qa_result = qa_model(question=question_text, context=context)
88
  answer = qa_result["answer"]
89
 
90
+ # Convert answer to speech
91
+ audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
92
+ engine.save_to_file(answer, audio_path)
93
+ engine.runAndWait()
94
 
95
  return answer, audio_path
96
 
 
 
 
 
 
 
 
97
  # Define the Gradio interface
98
  context_input = gr.Textbox(label="Context")
99
  question_input = gr.Textbox(label="Question")
 
109
  title="Question Answering",
110
  description="Enter a context and a question to get an answer. You can also record audio with the question.",
111
  examples=[
112
+ ["The capital of France is Paris.", "What is the capital of France?", None],
113
+ ["OpenAI is famous for developing GPT-3.", "What is OpenAI known for?", None],
114
  ]
115
  )
116
 
117
  # Launch the Gradio interface
118
  inter.launch()
119
+