Spaces:
Sleeping
Sleeping
File size: 11,178 Bytes
785f4dd 9602f8e 785f4dd 4ab54db 785f4dd bf8feae 785f4dd 9602f8e 785f4dd 9602f8e 785f4dd 9602f8e 785f4dd 9602f8e |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# import sounddevice as sd
# import soundfile as sf
# import speech_recognition as sr
# from gtts import gTTS
# import pygame
# import time
# import gradio as gr
# from transformers import AutoTokenizer, AutoModelForQuestionAnswering
# model = AutoModelForQuestionAnswering.from_pretrained('AVISHKAARAM/avishkaarak-ekta-hindi')
# tokenizer = AutoTokenizer.from_pretrained('AVISHKAARAM/avishkaarak-ekta-hindi')
# class AvishkaaramEkta:
# def __init__(self, model):
# self.model = model
# self.tokenizer = tokenizer
# def text_to_speech(self, text, output_file):
# # Create a gTTS object with the text and desired language
# tts = gTTS(text=text, lang='en')
# # Save the audio to a file
# tts.save(output_file)
# def play_mp3(self, file_path):
# pygame.mixer.init()
# pygame.mixer.music.load(file_path)
# pygame.mixer.music.play()
# while pygame.mixer.music.get_busy():
# continue
# def ask_question(self, audio_file):
# print("Recording audio...")
# audio = sd.rec(int(44100 * 6), samplerate=44100, channels=1)
# sd.wait()
# # Save the audio to a file
# sf.write(audio_file, audio, 44100)
# print(f"Audio saved to {audio_file}")
# r = sr.Recognizer()
# with sr.AudioFile(audio_file) as source:
# audio_data = r.record(source)
# text = ""
# try:
# text = r.recognize_google(audio_data)
# print("Transcription:", text)
# except sr.UnknownValueError:
# print("Speech recognition could not understand audio")
# except sr.RequestError as e:
# print("Could not request results from Google Speech Recognition service; {0}".format(e))
# return text
# def answer_question(self, passage, question):
# inputs = self.tokenizer(passage, question, return_tensors="pt")
# outputs = self.model(**inputs)
# start_logits = outputs.start_logits
# end_logits = outputs.end_logits
# start_index = start_logits.argmax(dim=1).item()
# end_index = end_logits.argmax(dim=1).item()
# tokens = self.tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
# answer = self.tokenizer.convert_tokens_to_string(tokens[start_index:end_index+1])
# return answer
# def question_answer(self, passage, question):
# passage_audio_file = "passage.mp3"
# question_audio_file = "question.wav"
# answer_audio_file = "answer.mp3"
# self.text_to_speech(passage, passage_audio_file)
# self.play_mp3(passage_audio_file)
# question_text = self.ask_question(question_audio_file)
# answer = self.answer_question(passage, question_text)
# self.text_to_speech("The answer to the question is: " + answer, answer_audio_file)
# self.play_mp3(answer_audio_file)
# time.sleep(5) # Wait for 5 seconds before ending
# return answer
# # Create an instance of the AvishkaaramEkta class
# avishkaaram_ekta = AvishkaaramEkta(model)
# # Define the Gradio interface
# iface = gr.Interface(
# fn=avishkaaram_ekta.question_answer,
# inputs=["text", "text"],
# outputs="text",
# title="Audio Question Answering",
# description="Ask a question about a given passage using audio input",
# examples=[
# ["In 1960, Dr. Jane Goodall arrived in Gombe, Tanzania to study chimpanzees.", "What did Dr. Jane Goodall study?"],
# ["The Taj Mahal is located in Agra, India.", "Where is the Taj Mahal situated?"],
# ],
# interpretation="default",
# )
# # Launch the Gradio interface
# iface.launch()
# import torch
# import torchaudio
# import soundfile as sf
# import speech_recognition as sr
# from gtts import gTTS
# import pygame
# import time
# import gradio as gr
# import os
# from transformers import AutoTokenizer, AutoModelForQuestionAnswering
# model = AutoModelForQuestionAnswering.from_pretrained('AVISHKAARAM/avishkaarak-ekta-hindi')
# tokenizer = AutoTokenizer.from_pretrained('AVISHKAARAM/avishkaarak-ekta-hindi')
# os.environ['SDL_AUDIODRIVER'] = 'dsp'
# class AvishkaaramEkta:
# def __init__(self, model):
# self.model = model
# self.tokenizer = tokenizer
# def text_to_speech(self, text, output_file):
# # Create a gTTS object with the text and desired language
# tts = gTTS(text=text, lang='en')
# # Save the audio to a file
# tts.save(output_file)
# def play_mp3(self, file_path):
# pygame.mixer.init()
# pygame.mixer.music.load(file_path)
# pygame.mixer.music.play()
# while pygame.mixer.music.get_busy():
# continue
# def ask_question(self, audio_file):
# print("Recording audio...")
# waveform, sample_rate = torchaudio.rec(6, sr=44100, channels=1)
# # Save the audio to a file
# sf.write(audio_file, waveform.squeeze().numpy(), sample_rate)
# print(f"Audio saved to {audio_file}")
# r = sr.Recognizer()
# with sr.AudioFile(audio_file) as source:
# audio_data = r.record(source)
# text = ""
# try:
# text = r.recognize_google(audio_data)
# print("Transcription:", text)
# except sr.UnknownValueError:
# print("Speech recognition could not understand audio")
# except sr.RequestError as e:
# print("Could not request results from Google Speech Recognition service; {0}".format(e))
# return text
# def answer_question(self, passage, question):
# inputs = self.tokenizer(passage, question, return_tensors="pt")
# outputs = self.model(**inputs)
# start_logits = outputs.start_logits
# end_logits = outputs.end_logits
# start_index = start_logits.argmax(dim=1).item()
# end_index = end_logits.argmax(dim=1).item()
# tokens = self.tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
# answer = self.tokenizer.convert_tokens_to_string(tokens[start_index:end_index+1])
# return answer
# def question_answer(self, passage, question):
# passage_audio_file = "passage.mp3"
# question_audio_file = "question.wav"
# answer_audio_file = "answer.mp3"
# self.text_to_speech(passage, passage_audio_file)
# self.play_mp3(passage_audio_file)
# question_text = self.ask_question(question_audio_file)
# answer = self.answer_question(passage, question_text)
# self.text_to_speech("The answer to the question is: " + answer, answer_audio_file)
# self.play_mp3(answer_audio_file)
# time.sleep(5) # Wait for 5 seconds before ending
# return answer
# # Create an instance of the AvishkaaramEkta class
# avishkaaram_ekta = AvishkaaramEkta(model)
# # Define the Gradio interface
# iface = gr.Interface(
# fn=avishkaaram_ekta.question_answer,
# inputs=["text", "text"],
# outputs="text",
# title="Audio Question Answering",
# description="Ask a question about a given passage using audio input",
# examples=[
# ["In 1960, Dr. Jane Goodall arrived in Gombe, Tanzania to study chimpanzees.", "What did Dr. Jane Goodall study?"],
# ["The Taj Mahal is located in Agra, India.", "Where is the Taj Mahal situated?"],
# ],
# interpretation="default",
# )
# # Launch the Gradio interface
# iface.launch()
import torch
import torchaudio
import soundfile as sf
import speech_recognition as sr
from gtts import gTTS
import pygame
import time
import os
import gradio as gr
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
os.environ["SDL_AUDIODRIVER"] = "pulseaudio"
model = AutoModelForQuestionAnswering.from_pretrained('AVISHKAARAM/avishkaarak-ekta-hindi')
tokenizer = AutoTokenizer.from_pretrained('AVISHKAARAM/avishkaarak-ekta-hindi')
class AvishkaaramEkta:
def __init__(self, model):
self.model = model
self.tokenizer = tokenizer
def text_to_speech(self, text, output_file):
# Create a gTTS object with the text and desired language
tts = gTTS(text=text, lang='en')
# Save the audio to a file
tts.save(output_file)
def play_mp3(self, file_path):
pygame.mixer.init()
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
def ask_question(self, audio_file):
print("Recording audio...")
waveform, sample_rate = torchaudio.rec(6, sr=44100, channels=1)
# Save the audio to a file
sf.write(audio_file, waveform.squeeze().numpy(), sample_rate)
print(f"Audio saved to {audio_file}")
r = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio_data = r.record(source)
text = ""
try:
text = r.recognize_google(audio_data)
print("Transcription:", text)
except sr.UnknownValueError:
print("Speech recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return text
def answer_question(self, passage, question):
inputs = self.tokenizer(passage, question, return_tensors="pt")
outputs = self.model(**inputs)
start_logits = outputs.start_logits
end_logits = outputs.end_logits
start_index = start_logits.argmax(dim=1).item()
end_index = end_logits.argmax(dim=1).item()
tokens = self.tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
answer = self.tokenizer.convert_tokens_to_string(tokens[start_index:end_index+1])
return answer
def question_answer(self, passage, question):
passage_audio_file = "passage.mp3"
question_audio_file = "question.wav"
answer_audio_file = "answer.mp3"
self.text_to_speech(passage, passage_audio_file)
self.play_mp3(passage_audio_file)
question_text = self.ask_question(question_audio_file)
answer = self.answer_question(passage, question_text)
self.text_to_speech("The answer to the question is: " + answer, answer_audio_file)
self.play_mp3(answer_audio_file)
time.sleep(5) # Wait for 5 seconds before ending
return answer
# Create an instance of the AvishkaaramEkta class
avishkaaram_ekta = AvishkaaramEkta(model)
# Define the Gradio interface
iface = gr.Interface(
fn=avishkaaram_ekta.question_answer,
inputs=["text", "text"],
outputs="text",
title="Audio Question Answering",
description="Ask a question about a given passage using audio input",
examples=[
["In 1960, Dr. Jane Goodall arrived in Gombe, Tanzania, to study the behavior of chimpanzees in the wild."],
["What was the purpose of Dr. Jane Goodall's visit to Gombe?"]
]
)
# Launch the interface
iface.launch()
|