techysanoj commited on
Commit
36fdb50
·
verified ·
1 Parent(s): 6daa386

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -74
app.py CHANGED
@@ -1,96 +1,33 @@
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
- # inter = 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
- # inter.launch()
59
-
60
-
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
 
69
  transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-english")
70
 
71
- # Load the pre-trained question-answering model
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
 
@@ -116,4 +53,3 @@ inter = gr.Interface(
116
 
117
  # Launch the Gradio interface
118
  inter.launch()
119
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from gtts import gTTS
4
  import tempfile
 
5
 
6
  # Initialize the speech-to-text transcriber
7
+ from transformers import pipeline
8
  transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-english")
9
 
10
+ # Initialize the pre-trained question-answering model
11
  model_name = "AVISHKAARAM/avishkaarak-ekta-hindi"
12
  qa_model = pipeline("question-answering", model=model_name)
13
 
 
 
 
14
  def answer_question(context, question=None, audio=None):
15
  # Handle audio input
16
  if audio is not None:
17
+ # Convert audio to text using transcriber
18
+ transcription_result = transcriber(audio)["text"]
19
+ question_text = transcription_result
20
  else:
21
  question_text = question
22
 
23
+ # Generate the answer using the QA model
24
  qa_result = qa_model(question=question_text, context=context)
25
  answer = qa_result["answer"]
26
 
27
+ # Convert the answer to speech using gTTS
28
+ tts = gTTS(text=answer, lang='en')
29
  audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
30
+ tts.save(audio_path)
 
31
 
32
  return answer, audio_path
33
 
 
53
 
54
  # Launch the Gradio interface
55
  inter.launch()