simonraj commited on
Commit
384aa04
·
1 Parent(s): f61c7c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -29
app.py CHANGED
@@ -2,15 +2,6 @@ import gradio as gr
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 = "your_key_file_name.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,22 +9,6 @@ API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "
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,7 +126,7 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
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,9 +141,8 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
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])
 
2
  import os
3
  import json
4
  import requests
 
 
 
 
 
 
 
 
 
5
 
6
  #Streaming endpoint
7
  API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
 
9
  #Huggingface provided GPT4 OpenAI API Key
10
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  #Inferenec function
13
  def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
14
 
 
126
  system_msg = gr.Textbox(label="Instruct the AI Assistant to set its beaviour", info = system_msg_info, value="")
127
  accordion_msg = gr.HTML(value="🚧 To set System message you will have to refresh the app", visible=False)
128
  chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
129
+ inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter")
130
  state = gr.State([])
131
  with gr.Row():
132
  with gr.Column(scale=7):
 
141
  chat_counter = gr.Number(value=0, visible=False, precision=0)
142
 
143
  #Event handling
144
+ inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
145
+ b1.click( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) #openai_api_key
 
146
 
147
  inputs.submit(set_visible_false, [], [system_msg])
148
  b1.click(set_visible_false, [], [system_msg])