Tanish Gupta commited on
Commit
6a709b5
β€’
1 Parent(s): d95628d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from pydub import AudioSegment
6
+
7
+ load_dotenv()
8
+
9
+ #accessing openapi Key
10
+ openai.api_key = os.getenv("OPENAI_API_KEY")
11
+
12
+ audio_messages = [{"role": "system", "content": 'You are an AI assistant expert. Respond to all input in precise, crisp and easy to understand language.'}]
13
+ text_messages = [{"role": "system", "content": 'You are an AI assistant expert. Respond to all input in precise, crisp and easy to understand language.'}]
14
+ global user_text_input, text_output, user_audio_input, audio_output
15
+
16
+ """
17
+ It seems like the gr.Audio source is not generating a WAV file, which is required for the openai.Audio.transcribe() method to work.
18
+ To convert the audio file to WAV format, i have used a library like Pydub.
19
+ """
20
+
21
+ def audio_transcribe(audio):
22
+ global audio_messages
23
+ audio_message = audio_messages
24
+
25
+ #audio processing to whisper API.
26
+ audio_file = AudioSegment.from_file(audio)
27
+ audio_file.export("temp.wav", format="wav")
28
+ final_audio_file = open("temp.wav", "rb")
29
+ transcript = openai.Audio.transcribe("whisper-1", final_audio_file)
30
+ os.remove("temp.wav")
31
+
32
+ #transcripted input to chatGPT API for chatCompletion
33
+ audio_message.append({"role": "user", "content": transcript["text"]}) # type: ignore
34
+ response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=audio_message)
35
+ system_message = response["choices"][0]["message"] # type: ignore
36
+ audio_message.append(system_message)
37
+
38
+ chat_transcript = ""
39
+ for message in audio_message:
40
+ if message['role'] != 'system':
41
+ chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
42
+
43
+ return chat_transcript
44
+
45
+ def text_transcribe(name):
46
+ global text_messages
47
+ text_message = text_messages
48
+ user_text_input.update("")
49
+ #transcripted input to chatGPT API
50
+ text_message.append({"role": "user", "content": name}) # type: ignore
51
+ response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=text_message)
52
+ system_message = response["choices"][0]["message"] # type: ignore
53
+ text_message.append(system_message)
54
+
55
+ chat_transcript = ""
56
+ for message in text_message:
57
+ if message['role'] != 'system':
58
+ chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
59
+ return chat_transcript
60
+
61
+ title = """<h1 align="center">Your Chat-GPT AI Assistant at your Service!! 😎 </h1>"""
62
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
63
+ gr.HTML(title)
64
+ with gr.Tab("Audio Input"):
65
+ with gr.Row():
66
+ user_audio_input = (gr.Audio(source="microphone", type="filepath", label="Speak Here"))
67
+ audio_input = user_audio_input
68
+ audio_output = gr.Textbox(label="AI Response", lines=20, placeholder="AI Response will be displayed here...")
69
+ with gr.Row():
70
+ audio_submit_button = gr.Button("Submit")
71
+ with gr.Tab("Text Input"):
72
+ with gr.Row():
73
+ user_text_input = (gr.Textbox(label="Type Here", lines=20, placeholder="Type your message here..."))
74
+ text_input = user_text_input
75
+ text_output = gr.Textbox(label="AI Response", lines=20, placeholder="AI Response will be displayed here...")
76
+ with gr.Row():
77
+ text_submit_button = gr.Button("Submit")
78
+ audio_submit_button.click(fn=audio_transcribe, inputs=audio_input, outputs=audio_output)
79
+ text_submit_button.click(fn=text_transcribe, inputs=text_input, outputs=text_output)
80
+
81
+ gr.Markdown("<center> Made with ❀️ by Tanish Gupta. Credits to πŸ€— Spaces for Hosting this App </center>")
82
+
83
+ demo.launch(share=True)