Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,15 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import json
|
4 |
import requests
|
5 |
-
from
|
|
|
|
|
|
|
6 |
|
|
|
7 |
|
|
|
|
|
8 |
|
9 |
#Streaming endpoint
|
10 |
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
@@ -12,6 +18,22 @@ API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "
|
|
12 |
#Huggingface provided GPT4 OpenAI API Key
|
13 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
#Inferenec function
|
16 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
17 |
|
@@ -129,7 +151,7 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
129 |
system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="")
|
130 |
accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
|
131 |
chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
|
132 |
-
inputs =
|
133 |
state = gr.State([])
|
134 |
with gr.Row():
|
135 |
with gr.Column(scale=7):
|
@@ -144,8 +166,9 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
144 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
145 |
|
146 |
#Event handling
|
147 |
-
inputs.submit(
|
148 |
-
b1.click(
|
|
|
149 |
|
150 |
inputs.submit(set_visible_false, [], [system_msg])
|
151 |
b1.click(set_visible_false, [], [system_msg])
|
|
|
2 |
import os
|
3 |
import json
|
4 |
import requests
|
5 |
+
from google.cloud import speech_v1p1beta1 as speech
|
6 |
+
from google.oauth2 import service_account
|
7 |
+
import base64
|
8 |
+
import io
|
9 |
|
10 |
+
path_to_key_file = "/path/to/your/key-file.json"
|
11 |
|
12 |
+
credentials = service_account.Credentials.from_service_account_file(path_to_key_file)
|
13 |
+
client = speech.SpeechClient(credentials=credentials)
|
14 |
|
15 |
#Streaming endpoint
|
16 |
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
|
|
18 |
#Huggingface provided GPT4 OpenAI API Key
|
19 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
20 |
|
21 |
+
def transcribe_audio(audio):
|
22 |
+
audio_bytes = base64.b64decode(audio.split(",")[1])
|
23 |
+
audio_file = io.BytesIO(audio_bytes)
|
24 |
+
|
25 |
+
audio = speech.RecognitionAudio(content=audio_file.read())
|
26 |
+
config = speech.RecognitionConfig(
|
27 |
+
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
28 |
+
sample_rate_hertz=16000,
|
29 |
+
language_code="en-US",
|
30 |
+
)
|
31 |
+
|
32 |
+
response = client.recognize(config=config, audio=audio)
|
33 |
+
|
34 |
+
for result in response.results:
|
35 |
+
return result.alternatives[0].transcript
|
36 |
+
|
37 |
#Inferenec function
|
38 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
39 |
|
|
|
151 |
system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="")
|
152 |
accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
|
153 |
chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
|
154 |
+
inputs = gr.Audio(label="Record an input", source="microphone")
|
155 |
state = gr.State([])
|
156 |
with gr.Row():
|
157 |
with gr.Column(scale=7):
|
|
|
166 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
167 |
|
168 |
#Event handling
|
169 |
+
inputs.submit(predict, [system_msg, transcribe_audio(inputs), top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code])
|
170 |
+
b1.click(predict, [system_msg, transcribe_audio(inputs), top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code])
|
171 |
+
|
172 |
|
173 |
inputs.submit(set_visible_false, [], [system_msg])
|
174 |
b1.click(set_visible_false, [], [system_msg])
|