techysanoj commited on
Commit
ebc7318
·
verified ·
1 Parent(s): 8be7657

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -10
app.py CHANGED
@@ -1,3 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torchaudio
2
  import gradio as gr
3
  from transformers import pipeline
@@ -9,14 +70,14 @@ import time
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
 
@@ -37,23 +98,24 @@ def play_audio(audio_path):
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
  # Launch the Gradio interface
59
- inter.launch()
 
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
+ # # Launch the Gradio interface
59
+ # inter.launch()
60
+
61
+
62
  import torchaudio
63
  import gradio as gr
64
  from transformers import pipeline
 
70
  # Initialize the speech-to-text transcriber
71
  transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-english")
72
 
73
+ # Load the pre-trained question-answering model
74
  model_name = "AVISHKAARAM/avishkaarak-ekta-hindi"
75
  qa_model = pipeline("question-answering", model=model_name)
76
 
77
  def answer_question(context, question=None, audio=None):
78
  if audio is not None:
79
+ text = transcriber(audio)["text"]
80
+ question_text = text
81
  else:
82
  question_text = question
83
 
 
98
  time.sleep(0.1)
99
 
100
  # Define the Gradio interface
101
+ context_input = gr.Textbox(label="Context")
102
+ question_input = gr.Textbox(label="Question")
103
+ audio_input = gr.Audio(type="filepath", label="Question Audio")
104
 
105
+ output_text = gr.Textbox(label="Answer")
106
+ output_audio = gr.Audio(label="Answer Audio")
107
 
108
  inter = gr.Interface(
109
  fn=answer_question,
110
  inputs=[context_input, question_input, audio_input],
111
  outputs=[output_text, output_audio],
112
  title="Question Answering",
113
+ description="Enter a context and a question to get an answer. You can also record audio with the question.",
114
  examples=[
115
  ["The capital of France is Paris.", "What is the capital of France?"],
116
  ["OpenAI is famous for developing GPT-3.", "What is OpenAI known for?"],
117
  ]
118
  )
119
+
120
  # Launch the Gradio interface
121
+ inter.launch()