Respair commited on
Commit
9865d2f
·
verified ·
1 Parent(s): eea329a

Update chat_app_remote.py

Browse files
Files changed (1) hide show
  1. chat_app_remote.py +76 -237
chat_app_remote.py CHANGED
@@ -1,202 +1,15 @@
1
- # import gradio as gr
2
- # from gradio_client import Client
3
- # import uuid
4
- # import warnings
5
- # import numpy as np
6
- # import json
7
- # import os
8
- # from gradio_client import Client, FileData, handle_file
9
- # warnings.filterwarnings("ignore")
10
- # import tempfile
11
- # import scipy.io.wavfile as wavfile
12
-
13
- # client = Client(os.environ['src'])
14
-
15
- # custom_css = """
16
- # .gradio-container {
17
- # justify-content: flex-start !important;
18
- # }
19
- # """
20
-
21
- # def create_frontend_demo():
22
- # # This function always uses the session ID stored per browser session (via gr.State).
23
- # # The session_id is generated on startup if missing and should not be changed arbitrarily.
24
- # def chat_function(message, history, session_id):
25
- # if not session_id:
26
- # # Generate a new session ID if not available in the client's local state.
27
- # session_id = "user_" + uuid.uuid4().hex[:8]
28
-
29
- # result = client.predict(
30
- # message, # message
31
- # history, # conversation history
32
- # session_id, # session id as defined in the front-end per client
33
- # fn_index=0 # function to call in the backend
34
- # )
35
-
36
- # # The backend returns: empty_string, new_history, audio_path, display_text
37
- # _, new_history, audio_path, display_text = result
38
-
39
- # # Return an empty message, the updated history, the audio path, the session_id, and a display text.
40
- # return "", new_history, audio_path, session_id, display_text
41
-
42
- # with gr.Blocks(css=custom_css, theme="Respair/[email protected]") as demo:
43
- # # The session_id_state is stored per front-end session, so each user gets their own.
44
- # session_id_state = gr.State("")
45
-
46
- # with gr.Tabs() as tabs:
47
- # with gr.Tab("Chat"):
48
- # # Display the session id visibly in the Chat tab.
49
- # session_display = gr.Markdown("Current Session ID: None", label="Session ID")
50
- # chatbot = gr.Chatbot(
51
- # label="Conversation History",
52
- # height=400,
53
- # avatar_images=[
54
- # "photo_2024-03-01_22-30-42.jpg",
55
- # "colored_blured.png"
56
- # ],
57
- # placeholder="Start chatting with Aira..."
58
- # )
59
-
60
- # gr.Markdown(
61
- # """Please, go to the Options tab and set a session ID. Do not start with 'None'.<br>
62
- # オプションタブに移動して、セッションIDを設定してください。Noneから始めないでください"""
63
- # )
64
-
65
- # # Use only a textbox for input (no visible send button)
66
- # with gr.Column():
67
- # msg = gr.Textbox(
68
- # show_label=False,
69
- # placeholder="Enter text and press enter",
70
- # container=True
71
- # )
72
-
73
- # audio_output = gr.Audio(
74
- # label="Aira's Response",
75
- # type="filepath",
76
- # streaming=False,
77
- # autoplay=True
78
- # )
79
-
80
- # with gr.Row():
81
- # audio_input = gr.Audio(
82
- # sources=["microphone"],
83
- # type="numpy",
84
- # label="Audio Input",
85
- # streaming=False
86
- # )
87
-
88
- # with gr.Tab("Options"):
89
- # with gr.Column():
90
- # # This textbox lets the user set their session id only for this client
91
- # session_input = gr.Textbox(
92
- # value="",
93
- # label="Session ID (leave blank for new session)"
94
- # )
95
- # gen_id_btn = gr.Button("Set Session ID")
96
- # session_msg = gr.Markdown("")
97
- # clear_btn = gr.Button("Clear Conversation")
98
-
99
- # gr.Markdown(
100
- # """
101
- # This is a personal project I wanted to do for a while (G̶o̶t̶t̶a̶ ̶m̶a̶k̶e̶ ̶u̶s̶e̶ ̶o̶f̶ ̶t̶h̶i̶s̶ ̶P̶r̶o̶ ̶s̶u̶b̶ ̶p̶e̶r̶k̶s̶ ̶w̶h̶i̶l̶e̶ ̶I̶ ̶h̶a̶v̶e̶ ̶i̶t̶). <br>
102
- # Aira's voice was designed to be unique; it doesn't belong to any real person out there. <br>
103
-
104
- # Her design is also based on a VTuber project I did a few years ago (notice the lazy brush strokes).<br><br>
105
-
106
- # You can talk to her in English or Japanese, but she will only respond in Japanese
107
- # (Subs over dubs, bros). Ask her to give you a Subtitle if you can't talk in Japanese. <br>
108
-
109
- # The majority of the latency depends on HF's inference API.
110
- # The language modelling part is off-the-shelf and not fine-tuned – please beware of that.
111
-
112
- # 1. Enter your Session ID above or leave blank for a new one.
113
- # 2. Click 'Set Session ID' to confirm.
114
- # 3. Use 'Clear Conversation' to reset the chat.
115
- # 4. Your conversation history is saved on a per-session basis.
116
-
117
- # I'll try to keep this demo up for as long as I can afford.
118
- # """
119
- # )
120
-
121
- # def respond(message, chat_history, session_id):
122
- # return chat_function(message, chat_history, session_id)
123
-
124
- # # Submit text input with Enter key
125
- # msg.submit(
126
- # respond,
127
- # inputs=[msg, chatbot, session_id_state],
128
- # outputs=[msg, chatbot, audio_output, session_id_state, session_display]
129
- # )
130
-
131
- # # Set session function that simply uses or generates a unique session id
132
- # def set_session(user_id):
133
- # if not user_id.strip():
134
- # new_id = "user_" + uuid.uuid4().hex[:8]
135
- # display_text = f"Current Session ID: {new_id}"
136
- # return new_id, "", display_text
137
- # else:
138
- # display_text = f"Current Session ID: {user_id}"
139
- # return user_id, "", display_text
140
-
141
- # gen_id_btn.click(
142
- # set_session,
143
- # inputs=[session_input],
144
- # outputs=[session_id_state, session_msg, session_display]
145
- # )
146
-
147
- # def handle_audio(audio_data, history, session_id):
148
- # if audio_data is None:
149
- # return None, history, session_id, f"Current Session ID: {session_id}"
150
-
151
- # try:
152
- # sample_rate, audio_array = audio_data
153
- # with tempfile.NamedTemporaryFile(suffix='.wav', delete=True) as temp:
154
- # wavfile.write(temp.name, sample_rate, audio_array)
155
- # audio = {"path": temp.name, "meta": {"_type": "gradio.FileData"}}
156
- # result = client.predict(
157
- # audio,
158
- # history,
159
- # session_id,
160
- # api_name="/handle_audio"
161
- # )
162
- # audio_path, new_history, new_session_id = result
163
- # display_text = f"Current Session ID: {new_session_id}"
164
- # return audio_path, new_history, new_session_id, display_text
165
- # except Exception as e:
166
- # print(f"Error processing audio: {str(e)}")
167
- # import traceback
168
- # traceback.print_exc()
169
- # return None, history, session_id, f"Error processing audio. Session ID: {session_id}"
170
-
171
- # audio_input.stop_recording(
172
- # handle_audio,
173
- # inputs=[audio_input, chatbot, session_id_state],
174
- # outputs=[audio_output, chatbot, session_id_state, session_display]
175
- # )
176
-
177
- # clear_btn.click(
178
- # lambda: [],
179
- # None,
180
- # [chatbot]
181
- # )
182
-
183
- # return demo
184
-
185
- # if __name__ == "__main__":
186
- # demo = create_frontend_demo()
187
- # demo.launch(show_error=True)
188
  import gradio as gr
189
  from gradio_client import Client
190
  import uuid
191
  import warnings
192
  import numpy as np
 
193
  import os
 
 
194
  import tempfile
195
  import scipy.io.wavfile as wavfile
196
 
197
- warnings.filterwarnings("ignore")
198
-
199
- # Set up the client for your backend on Hugging Face Spaces
200
  client = Client(os.environ['src'])
201
 
202
  custom_css = """
@@ -206,58 +19,64 @@ custom_css = """
206
  """
207
 
208
  def create_frontend_demo():
209
- # The backend or conversation history might be providing a persistent session id.
210
- # To avoid that, we ignore any session id returned from the backend if our gr.State was empty.
211
  def chat_function(message, history, session_id):
212
- # If no session id exists in gr.State, generate a new one locally.
213
  if not session_id:
214
- new_session = "user_" + uuid.uuid4().hex[:8]
215
- else:
216
- new_session = session_id
217
-
218
- # Call the backend with our new_session even if one was previously returned.
219
  result = client.predict(
220
- message, # text message
221
- history, # conversation history
222
- new_session, # force our new_session
223
- fn_index=0 # backend function index
224
  )
 
 
 
 
 
 
225
 
226
- # Suppose backend returns: (_ , new_history, audio_path, returned_session, display_text)
227
- # We ignore the returned session id and instead continue with our new_session variable.
228
- _, new_history, audio_path, returned_session, display_text = result
229
-
230
- # Debug: if you want to check what was returned, uncomment the line below.
231
- # print("Backend returned session id:", returned_session, "using ours:", new_session)
232
-
233
- return "", new_history, audio_path, new_session, display_text
234
-
235
- with gr.Blocks(css=custom_css, theme="default") as demo:
236
- # gr.State stores the session id, which is kept in memory.
237
  session_id_state = gr.State("")
238
-
239
- with gr.Tabs():
240
  with gr.Tab("Chat"):
 
241
  session_display = gr.Markdown("Current Session ID: None", label="Session ID")
242
  chatbot = gr.Chatbot(
243
  label="Conversation History",
244
  height=400,
245
- placeholder="Start chatting..."
 
 
 
 
246
  )
 
247
  gr.Markdown(
248
- "If the session id shows None upon refresh, a new session will be generated once you send a message."
249
- )
250
- msg = gr.Textbox(
251
- show_label=False,
252
- placeholder="Enter text and press Enter",
253
- container=True
254
  )
 
 
 
 
 
 
 
 
 
255
  audio_output = gr.Audio(
256
- label="Bot's Audio Response",
257
  type="filepath",
258
  streaming=False,
259
  autoplay=True
260
  )
 
261
  with gr.Row():
262
  audio_input = gr.Audio(
263
  sources=["microphone"],
@@ -265,9 +84,10 @@ def create_frontend_demo():
265
  label="Audio Input",
266
  streaming=False
267
  )
268
-
269
  with gr.Tab("Options"):
270
  with gr.Column():
 
271
  session_input = gr.Textbox(
272
  value="",
273
  label="Session ID (leave blank for new session)"
@@ -275,24 +95,40 @@ def create_frontend_demo():
275
  gen_id_btn = gr.Button("Set Session ID")
276
  session_msg = gr.Markdown("")
277
  clear_btn = gr.Button("Clear Conversation")
 
278
  gr.Markdown(
279
  """
280
- 1. Enter your Session ID above (or leave blank to get a new one).<br>
281
- 2. Click 'Set Session ID' to confirm.<br>
282
- 3. Click 'Clear Conversation' to reset the chat.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  """
284
  )
285
 
286
  def respond(message, chat_history, session_id):
287
  return chat_function(message, chat_history, session_id)
288
 
 
289
  msg.submit(
290
  respond,
291
  inputs=[msg, chatbot, session_id_state],
292
  outputs=[msg, chatbot, audio_output, session_id_state, session_display]
293
  )
294
 
295
- # A separate button handler to let the user set a session id manually.
296
  def set_session(user_id):
297
  if not user_id.strip():
298
  new_id = "user_" + uuid.uuid4().hex[:8]
@@ -307,8 +143,7 @@ def create_frontend_demo():
307
  inputs=[session_input],
308
  outputs=[session_id_state, session_msg, session_display]
309
  )
310
-
311
- # Handler for audio input (if implemented).
312
  def handle_audio(audio_data, history, session_id):
313
  if audio_data is None:
314
  return None, history, session_id, f"Current Session ID: {session_id}"
@@ -324,13 +159,13 @@ def create_frontend_demo():
324
  session_id,
325
  api_name="/handle_audio"
326
  )
327
- # Assume backend returns (audio_path, new_history, returned_session)
328
- audio_path, new_history, returned_session = result
329
- # Force our session id rather than any returned one
330
- display_text = f"Current Session ID: {session_id}"
331
- return audio_path, new_history, session_id, display_text
332
  except Exception as e:
333
  print(f"Error processing audio: {str(e)}")
 
 
334
  return None, history, session_id, f"Error processing audio. Session ID: {session_id}"
335
 
336
  audio_input.stop_recording(
@@ -339,10 +174,14 @@ def create_frontend_demo():
339
  outputs=[audio_output, chatbot, session_id_state, session_display]
340
  )
341
 
342
- clear_btn.click(lambda: [], None, [chatbot])
 
 
 
 
343
 
344
  return demo
345
 
346
  if __name__ == "__main__":
347
  demo = create_frontend_demo()
348
- demo.launch(show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from gradio_client import Client
3
  import uuid
4
  import warnings
5
  import numpy as np
6
+ import json
7
  import os
8
+ from gradio_client import Client, FileData, handle_file
9
+ warnings.filterwarnings("ignore")
10
  import tempfile
11
  import scipy.io.wavfile as wavfile
12
 
 
 
 
13
  client = Client(os.environ['src'])
14
 
15
  custom_css = """
 
19
  """
20
 
21
  def create_frontend_demo():
22
+ # This function always uses the session ID stored per browser session (via gr.State).
23
+ # The session_id is generated on startup if missing and should not be changed arbitrarily.
24
  def chat_function(message, history, session_id):
 
25
  if not session_id:
26
+ # Generate a new session ID if not available in the client's local state.
27
+ session_id = "user_" + uuid.uuid4().hex[:8]
28
+
 
 
29
  result = client.predict(
30
+ message, # message
31
+ history, # conversation history
32
+ session_id, # session id as defined in the front-end per client
33
+ fn_index=0 # function to call in the backend
34
  )
35
+
36
+ # The backend returns: empty_string, new_history, audio_path, display_text
37
+ _, new_history, audio_path, display_text = result
38
+
39
+ # Return an empty message, the updated history, the audio path, the session_id, and a display text.
40
+ return "", new_history, audio_path, session_id, display_text
41
 
42
+ with gr.Blocks(css=custom_css, theme="Respair/[email protected]") as demo:
43
+ # The session_id_state is stored per front-end session, so each user gets their own.
 
 
 
 
 
 
 
 
 
44
  session_id_state = gr.State("")
45
+
46
+ with gr.Tabs() as tabs:
47
  with gr.Tab("Chat"):
48
+ # Display the session id visibly in the Chat tab.
49
  session_display = gr.Markdown("Current Session ID: None", label="Session ID")
50
  chatbot = gr.Chatbot(
51
  label="Conversation History",
52
  height=400,
53
+ avatar_images=[
54
+ "photo_2024-03-01_22-30-42.jpg",
55
+ "colored_blured.png"
56
+ ],
57
+ placeholder="Start chatting with Aira..."
58
  )
59
+
60
  gr.Markdown(
61
+ """Please, go to the Options tab and set a session ID. Do not start with 'None'.<br>
62
+ オプションタブに移動して、セッションIDを設定してください。Noneから始めないでください"""
 
 
 
 
63
  )
64
+
65
+ # Use only a textbox for input (no visible send button)
66
+ with gr.Column():
67
+ msg = gr.Textbox(
68
+ show_label=False,
69
+ placeholder="Enter text and press enter",
70
+ container=True
71
+ )
72
+
73
  audio_output = gr.Audio(
74
+ label="Aira's Response",
75
  type="filepath",
76
  streaming=False,
77
  autoplay=True
78
  )
79
+
80
  with gr.Row():
81
  audio_input = gr.Audio(
82
  sources=["microphone"],
 
84
  label="Audio Input",
85
  streaming=False
86
  )
87
+
88
  with gr.Tab("Options"):
89
  with gr.Column():
90
+ # This textbox lets the user set their session id only for this client
91
  session_input = gr.Textbox(
92
  value="",
93
  label="Session ID (leave blank for new session)"
 
95
  gen_id_btn = gr.Button("Set Session ID")
96
  session_msg = gr.Markdown("")
97
  clear_btn = gr.Button("Clear Conversation")
98
+
99
  gr.Markdown(
100
  """
101
+ This is a personal project I wanted to do for a while (G̶o̶t̶t̶a̶ ̶m̶a̶k̶e̶ ̶u̶s̶e̶ ̶o̶f̶ ̶t̶h̶i̶s̶ ̶P̶r̶o̶ ̶s̶u̶b̶ ̶p̶e̶r̶k̶s̶ ̶w̶h̶i̶l̶e̶ ̶I̶ ̶h̶a̶v̶e̶ ̶i̶t̶). <br>
102
+ Aira's voice was designed to be unique; it doesn't belong to any real person out there. <br>
103
+
104
+ Her design is also based on a VTuber project I did a few years ago (notice the lazy brush strokes).<br><br>
105
+
106
+ You can talk to her in English or Japanese, but she will only respond in Japanese
107
+ (Subs over dubs, bros). Ask her to give you a Subtitle if you can't talk in Japanese. <br>
108
+
109
+ The majority of the latency depends on HF's inference API.
110
+ The language modelling part is off-the-shelf and not fine-tuned – please beware of that.
111
+
112
+ 1. Enter your Session ID above or leave blank for a new one.
113
+ 2. Click 'Set Session ID' to confirm.
114
+ 3. Use 'Clear Conversation' to reset the chat.
115
+ 4. Your conversation history is saved on a per-session basis.
116
+
117
+ I'll try to keep this demo up for as long as I can afford.
118
  """
119
  )
120
 
121
  def respond(message, chat_history, session_id):
122
  return chat_function(message, chat_history, session_id)
123
 
124
+ # Submit text input with Enter key
125
  msg.submit(
126
  respond,
127
  inputs=[msg, chatbot, session_id_state],
128
  outputs=[msg, chatbot, audio_output, session_id_state, session_display]
129
  )
130
 
131
+ # Set session function that simply uses or generates a unique session id
132
  def set_session(user_id):
133
  if not user_id.strip():
134
  new_id = "user_" + uuid.uuid4().hex[:8]
 
143
  inputs=[session_input],
144
  outputs=[session_id_state, session_msg, session_display]
145
  )
146
+
 
147
  def handle_audio(audio_data, history, session_id):
148
  if audio_data is None:
149
  return None, history, session_id, f"Current Session ID: {session_id}"
 
159
  session_id,
160
  api_name="/handle_audio"
161
  )
162
+ audio_path, new_history, new_session_id = result
163
+ display_text = f"Current Session ID: {new_session_id}"
164
+ return audio_path, new_history, new_session_id, display_text
 
 
165
  except Exception as e:
166
  print(f"Error processing audio: {str(e)}")
167
+ import traceback
168
+ traceback.print_exc()
169
  return None, history, session_id, f"Error processing audio. Session ID: {session_id}"
170
 
171
  audio_input.stop_recording(
 
174
  outputs=[audio_output, chatbot, session_id_state, session_display]
175
  )
176
 
177
+ clear_btn.click(
178
+ lambda: [],
179
+ None,
180
+ [chatbot]
181
+ )
182
 
183
  return demo
184
 
185
  if __name__ == "__main__":
186
  demo = create_frontend_demo()
187
+ demo.launch(show_error=True)