gerasdf commited on
Commit
db7f523
·
1 Parent(s): 5b2ac2e

audio input is working "seamlessly"

Browse files
Files changed (1) hide show
  1. query.py +55 -8
query.py CHANGED
@@ -8,7 +8,10 @@ from langchain_core.runnables import RunnablePassthrough, RunnableLambda
8
  from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
9
  from langchain_openai import OpenAIEmbeddings, ChatOpenAI
10
 
 
 
11
  from json import loads as json_loads
 
12
  import os
13
 
14
  prompt_template = os.environ.get("PROMPT_TEMPLATE")
@@ -18,10 +21,11 @@ prompt = ChatPromptTemplate.from_messages([('system', prompt_template)])
18
  AI = True
19
 
20
  def ai_setup():
21
- global llm, prompt_chain
22
- llm = ChatOpenAI(model = "gpt-4o", temperature=0.8)
23
 
24
  if AI:
 
 
25
  embedding = OpenAIEmbeddings()
26
  vstore = AstraDBVectorStore(
27
  embedding=embedding,
@@ -97,8 +101,8 @@ def new_state():
97
  })
98
 
99
  def auth(token, state):
100
- tokens=os.environ.get("APP_TOKENS", None)
101
- if tokens is None:
102
  state["user"] = "anonymous"
103
  else:
104
  tokens=json_loads(tokens)
@@ -118,7 +122,7 @@ def chat(message, history, state):
118
  if (state is None) or (not state['user']):
119
  gr.Warning("You need to authenticate first")
120
  yield "You need to authenticate first"
121
- else:
122
  if not history:
123
  system_prompt = prompt_chain.invoke(message)
124
  system_prompt = system_prompt.messages[0]
@@ -136,6 +140,22 @@ def chat(message, history, state):
136
  for response in llm.stream(messages):
137
  all += response.content
138
  yield all
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
  def gr_main():
141
  theme = gr.Theme.from_hub("freddyaboulton/[email protected]")
@@ -151,10 +171,19 @@ def gr_main():
151
  theme=theme
152
  ) as app:
153
  state = new_state()
154
- gr.ChatInterface(
 
155
  chat,
156
- chatbot=gr.Chatbot(show_label=False, render=False, scale=1),
157
  title="Sherlock Holmes stories",
 
 
 
 
 
 
 
 
158
  examples=[
159
  ["I arrived late last night and found a dead goose in my bed"],
160
  ["Help please sir. I'm about to get married, to the most lovely lady,"
@@ -162,6 +191,21 @@ def gr_main():
162
  "of my past I'd rather keep quiet, unless I don't marry"],
163
  ],
164
  additional_inputs=[state])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  token = gr.Textbox(visible=False)
166
  app.load(auth,
167
  [token,state],
@@ -172,4 +216,7 @@ def gr_main():
172
 
173
  if __name__ == "__main__":
174
  ai_setup()
175
- gr_main()
 
 
 
 
8
  from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
9
  from langchain_openai import OpenAIEmbeddings, ChatOpenAI
10
 
11
+ from openai import OpenAI
12
+
13
  from json import loads as json_loads
14
+ import time
15
  import os
16
 
17
  prompt_template = os.environ.get("PROMPT_TEMPLATE")
 
21
  AI = True
22
 
23
  def ai_setup():
24
+ global llm, prompt_chain, oai_client
 
25
 
26
  if AI:
27
+ oai_client = OpenAI()
28
+ llm = ChatOpenAI(model = "gpt-4o", temperature=0.8)
29
  embedding = OpenAIEmbeddings()
30
  vstore = AstraDBVectorStore(
31
  embedding=embedding,
 
101
  })
102
 
103
  def auth(token, state):
104
+ tokens=os.environ.get("APP_TOKENS")
105
+ if not tokens:
106
  state["user"] = "anonymous"
107
  else:
108
  tokens=json_loads(tokens)
 
122
  if (state is None) or (not state['user']):
123
  gr.Warning("You need to authenticate first")
124
  yield "You need to authenticate first"
125
+ elif AI:
126
  if not history:
127
  system_prompt = prompt_chain.invoke(message)
128
  system_prompt = system_prompt.messages[0]
 
140
  for response in llm.stream(messages):
141
  all += response.content
142
  yield all
143
+ else:
144
+ yield f"{time.ctime()}: You said: {message}"
145
+
146
+ def on_audio(path):
147
+ if not path:
148
+ return [gr.update(), None]
149
+ if AI:
150
+ text = oai_client.audio.transcriptions.create(
151
+ model="whisper-1",
152
+ file=open(path, "rb"),
153
+ response_format="text"
154
+ )
155
+ else:
156
+ text = f"{time.ctime()}: You said something"
157
+
158
+ return (text, None)
159
 
160
  def gr_main():
161
  theme = gr.Theme.from_hub("freddyaboulton/[email protected]")
 
171
  theme=theme
172
  ) as app:
173
  state = new_state()
174
+ chatbot = gr.Chatbot(show_label=False, render=False, scale=1)
175
+ iface = gr.ChatInterface(
176
  chat,
177
+ chatbot=chatbot,
178
  title="Sherlock Holmes stories",
179
+ submit_btn=gr.Button(
180
+ "Submit",
181
+ variant="primary",
182
+ scale=1,
183
+ min_width=150,
184
+ elem_id="submit_btn",
185
+ render=False
186
+ ),
187
  examples=[
188
  ["I arrived late last night and found a dead goose in my bed"],
189
  ["Help please sir. I'm about to get married, to the most lovely lady,"
 
191
  "of my past I'd rather keep quiet, unless I don't marry"],
192
  ],
193
  additional_inputs=[state])
194
+
195
+ mic = gr.Audio(
196
+ sources=["microphone"],
197
+ type="filepath",
198
+ show_label=False,
199
+ format="mp3",
200
+
201
+ waveform_options=gr.WaveformOptions(sample_rate=16000))
202
+ mic.change(
203
+ on_audio, [mic], [iface.textbox, mic]
204
+ ).then(
205
+ lambda x:None,
206
+ js='function (text){debugger; if (text) document.getElementById("submit_btn").click(); return [text]}',
207
+ inputs=iface.textbox
208
+ )
209
  token = gr.Textbox(visible=False)
210
  app.load(auth,
211
  [token,state],
 
216
 
217
  if __name__ == "__main__":
218
  ai_setup()
219
+ gr_main()
220
+
221
+ # Autoplay audio
222
+ # https://github.com/gradio-app/gradio/issues/1349