Vihang28 commited on
Commit
1c66a80
1 Parent(s): 5d91013

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import speech_recognition as sr
2
+ from pydub import AudioSegment
3
+ import gradio as gr
4
+ from os import path
5
+ import requests
6
+ import openai
7
+
8
+ r = sr.Recognizer()
9
+
10
+ def record_text(audio_file):
11
+ sound = audio_file
12
+ sound_type = sound.split(".")
13
+ if sound_type[-1] == 'mp3':
14
+ input_file = sound
15
+ output_file = "con_sound.wav"
16
+ try:
17
+ # convert mp3 file to wav file
18
+ sound = AudioSegment.from_mp3(input_file)
19
+ sound.export(output_file, format="wav")
20
+ sound = "con_sound.wav"
21
+ except Exception as e:
22
+ print(f"Error converting MP3 to WAV: {e}")
23
+ return ""
24
+
25
+ MyText = ""
26
+ with sr.AudioFile(sound) as source:
27
+ r.adjust_for_ambient_noise(source)
28
+ print("Converting audio file to text..")
29
+ audio2 = r.record(source, duration=None) # Use record instead of listen
30
+ try:
31
+ MyText = r.recognize_google(audio2, language="en-US", key=None, show_all=False)
32
+ MyText = MyText.lower()
33
+ print("Converted audio is:\n" + MyText + '.')
34
+ except sr.UnknownValueError:
35
+ print("Google Speech Recognition could not understand audio")
36
+
37
+ return MyText
38
+
39
+ def message_and_history(audio_file, history, api_key):
40
+ history = history or []
41
+ input_text = "Type and press Enter"
42
+ output_text = record_text(audio_file)
43
+
44
+ if len(input_text) == 0:
45
+ input_text = "Apply proper punctuation on the given paragraph."
46
+ history.append((input_text, output_text))
47
+ else:
48
+ history.append((input_text, output_text))
49
+
50
+ return history, history
51
+
52
+ prompt = "Type and press Enter"
53
+ block = gr.Blocks(theme=gr.themes.Glass(primary_hue="slate"))
54
+ with block:
55
+ gr.Markdown("""<h1><center>Audio Recognition - Ask and Learn about an Audio</center></h1> """)
56
+ with gr.Row():
57
+ with gr.Column(scale=0.5):
58
+ aud_input = gr.Audio(type="filepath", label="Upload Audio")
59
+ api_input = gr.Textbox(label="Enter Api-key")
60
+ upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
61
+ with gr.Column():
62
+ chatbot = gr.Chatbot(label="Ask questions about the audio")
63
+ message = gr.Textbox(label="User", placeholder=prompt)
64
+ state = gr.State()
65
+
66
+ upload_button.click(message_and_history, inputs=[aud_input, state, api_input], outputs=[chatbot, state])
67
+ message.submit(message_and_history, inputs=[aud_input, state, api_input], outputs=[chatbot, state])
68
+ message.submit(lambda: None, None, message, queue=False)
69
+ block.launch(share=True)