alex buz commited on
Commit
68ba2e8
1 Parent(s): e6868fd
3t_dropdown copy 4.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+
4
+ def predict(message, history, character, api_key, progress=gr.Progress()):
5
+ client = OpenAI(api_key=api_key)
6
+ history_openai_format = []
7
+ for human, assistant in history:
8
+ history_openai_format.append({"role": "user", "content": human})
9
+ history_openai_format.append({"role": "assistant", "content": assistant})
10
+ history_openai_format.append({"role": "user", "content": message})
11
+
12
+ response = client.chat.completions.create(
13
+ model='gpt-4',
14
+ messages=history_openai_format,
15
+ temperature=1.0,
16
+ stream=True
17
+ )
18
+
19
+ partial_message = ""
20
+ for chunk in progress.tqdm(response, desc="Generating"):
21
+ if chunk.choices[0].delta.content:
22
+ partial_message += chunk.choices[0].delta.content
23
+ yield partial_message
24
+
25
+ def reset(character):
26
+ return [], []
27
+
28
+ # Gradio app
29
+ with gr.Blocks() as demo:
30
+ gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{'My Chatbot'}</h1>")
31
+ bot = gr.Chatbot(render=False)
32
+ dropdown = gr.Dropdown(
33
+ ["Character 1", "Character 2", "Character 3", "Character 4", "Character 5", "Character 6", "Character 7", "Character 8", "Character 9", "Character 10", "Character 11", "Character 12", "Character 13"],
34
+ label="Characters",
35
+ info="Select the character that you'd like to speak to",
36
+ value="Character 1"
37
+ )
38
+ chat = gr.ChatInterface(
39
+ fn=predict,
40
+ chatbot=bot,
41
+ additional_inputs=[dropdown, gr.Textbox(label="API Key")],
42
+ )
43
+ dropdown.change(fn=reset, inputs=dropdown, outputs=[bot, chat.chatbot_state])
44
+
45
+ demo.queue()
46
+ demo.launch()
3t_dropdown copy 5.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import threading
4
+
5
+ pause_event = threading.Event()
6
+
7
+ def predict(message, history, character, api_key, progress=gr.Progress()):
8
+ client = OpenAI(api_key=api_key)
9
+ history_openai_format = []
10
+ for human, assistant in history:
11
+ history_openai_format.append({"role": "user", "content": human})
12
+ history_openai_format.append({"role": "assistant", "content": assistant})
13
+ history_openai_format.append({"role": "user", "content": message})
14
+
15
+ response = client.chat.completions.create(
16
+ model='gpt-4',
17
+ messages=history_openai_format,
18
+ temperature=1.0,
19
+ stream=True
20
+ )
21
+
22
+ partial_message = ""
23
+ for chunk in progress.tqdm(response, desc="Generating"):
24
+ if pause_event.is_set():
25
+ break
26
+ if chunk.choices[0].delta.content:
27
+ partial_message += chunk.choices[0].delta.content
28
+ yield partial_message
29
+
30
+ def pause():
31
+ pause_event.set()
32
+
33
+ def resume():
34
+ pause_event.clear()
35
+
36
+ def reset(character):
37
+ return [], []
38
+
39
+ # Gradio app
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{'My Chatbot'}</h1>")
42
+ bot = gr.Chatbot(render=False)
43
+ dropdown = gr.Dropdown(
44
+ ["Character 1", "Character 2", "Character 3", "Character 4", "Character 5", "Character 6", "Character 7", "Character 8", "Character 9", "Character 10", "Character 11", "Character 12", "Character 13"],
45
+ label="Characters",
46
+ info="Select the character that you'd like to speak to",
47
+ value="Character 1"
48
+ )
49
+ chat = gr.ChatInterface(
50
+ fn=predict,
51
+ chatbot=bot,
52
+ additional_inputs=[dropdown, gr.Textbox(label="API Key")],
53
+ )
54
+ dropdown.change(fn=reset, inputs=dropdown, outputs=[bot, chat.chatbot_state])
55
+
56
+ pause_button = gr.Button("Pause")
57
+ resume_button = gr.Button("Resume")
58
+
59
+ pause_button.click(fn=pause, inputs=None, outputs=None)
60
+ resume_button.click(fn=resume, inputs=None, outputs=None)
61
+
62
+ demo.queue()
63
+ demo.launch()
3t_dropdown copy 6.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import threading
4
+
5
+ pause_event = threading.Event()
6
+ resume_event = threading.Event()
7
+
8
+ def predict(message, history, character, api_key, progress=gr.Progress()):
9
+ client = OpenAI(api_key=api_key)
10
+ history_openai_format = []
11
+ for human, assistant in history:
12
+ history_openai_format.append({"role": "user", "content": human})
13
+ history_openai_format.append({"role": "assistant", "content": assistant})
14
+ history_openai_format.append({"role": "user", "content": message})
15
+
16
+ response = client.chat.completions.create(
17
+ model='gpt-4o',
18
+ messages=history_openai_format,
19
+ temperature=1.0,
20
+ stream=True
21
+ )
22
+
23
+ partial_message = ""
24
+ for chunk in progress.tqdm(response, desc="Generating"):
25
+ while pause_event.is_set():
26
+ resume_event.wait()
27
+ if chunk.choices[0].delta.content:
28
+ partial_message += chunk.choices[0].delta.content
29
+ yield partial_message
30
+
31
+ def pause():
32
+ pause_event.set()
33
+ resume_event.clear()
34
+
35
+ def resume():
36
+ pause_event.clear()
37
+ resume_event.set()
38
+
39
+ def reset(character):
40
+ return [], []
41
+
42
+ # Gradio app
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{'My Chatbot'}</h1>")
45
+ bot = gr.Chatbot(render=False)
46
+ dropdown = gr.Dropdown(
47
+ ["Character 1", "Character 2", "Character 3", "Character 4", "Character 5", "Character 6", "Character 7", "Character 8", "Character 9", "Character 10", "Character 11", "Character 12", "Character 13"],
48
+ label="Characters",
49
+ info="Select the character that you'd like to speak to",
50
+ value="Character 1"
51
+ )
52
+ api_key_input = gr.Textbox(label="API Key")
53
+
54
+ chat = gr.ChatInterface(
55
+ fn=predict,
56
+ chatbot=bot,
57
+ additional_inputs=[dropdown, api_key_input],
58
+ )
59
+ dropdown.change(fn=reset, inputs=dropdown, outputs=[bot, chat.chatbot_state])
60
+
61
+ pause_button = gr.Button("Pause")
62
+ resume_button = gr.Button("Resume")
63
+
64
+ pause_button.click(fn=pause, inputs=None, outputs=None)
65
+ resume_button.click(fn=resume, inputs=None, outputs=None)
66
+
67
+ demo.queue()
68
+ demo.launch()
3t_dropdown copy 7.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import threading
4
+
5
+ pause_event = threading.Event()
6
+ resume_event = threading.Event()
7
+
8
+ def predict(message, history, character, api_key, progress=gr.Progress()):
9
+ client = OpenAI(api_key=api_key)
10
+ history_openai_format = []
11
+ for human, assistant in history:
12
+ history_openai_format.append({"role": "user", "content": human})
13
+ history_openai_format.append({"role": "assistant", "content": assistant})
14
+ history_openai_format.append({"role": "user", "content": message})
15
+
16
+ response = client.chat.completions.create(
17
+ model='gpt-4',
18
+ messages=history_openai_format,
19
+ temperature=1.0,
20
+ stream=True
21
+ )
22
+
23
+ partial_message = ""
24
+ for chunk in progress.tqdm(response, desc="Generating"):
25
+ while pause_event.is_set():
26
+ resume_event.wait()
27
+ if chunk.choices[0].delta.content:
28
+ partial_message += chunk.choices[0].delta.content
29
+ yield partial_message
30
+
31
+ def pause():
32
+ pause_event.set()
33
+ resume_event.clear()
34
+ return "Paused"
35
+
36
+ def resume():
37
+ pause_event.clear()
38
+ resume_event.set()
39
+ return "Resumed"
40
+
41
+ def reset(character):
42
+ return [], []
43
+
44
+ # Gradio app
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{'My Chatbot'}</h1>")
47
+ bot = gr.Chatbot(render=False)
48
+ dropdown = gr.Dropdown(
49
+ ["Character 1", "Character 2", "Character 3", "Character 4", "Character 5", "Character 6", "Character 7", "Character 8", "Character 9", "Character 10", "Character 11", "Character 12", "Character 13"],
50
+ label="Characters",
51
+ info="Select the character that you'd like to speak to",
52
+ value="Character 1"
53
+ )
54
+ api_key_input = gr.Textbox(label="API Key")
55
+
56
+ chat = gr.ChatInterface(
57
+ fn=predict,
58
+ chatbot=bot,
59
+ additional_inputs=[dropdown, api_key_input],
60
+ )
61
+ dropdown.change(fn=reset, inputs=dropdown, outputs=[bot, chat.chatbot_state])
62
+
63
+ pause_button = gr.Button("Pause")
64
+ resume_button = gr.Button("Resume")
65
+ status = gr.Textbox(label="Status", interactive=False)
66
+
67
+ pause_button.click(fn=pause, inputs=None, outputs=status)
68
+ resume_button.click(fn=resume, inputs=None, outputs=status)
69
+
70
+ demo.queue()
71
+ demo.launch()
app copy 6.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import numpy as np
5
+ from openai import OpenAI
6
+
7
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
8
+ qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
9
+
10
+ def predict(message, history, api_key, is_paused):
11
+ client = OpenAI(api_key=api_key)
12
+ history_openai_format = []
13
+ for human, assistant in history:
14
+ history_openai_format.append({"role": "user", "content": human})
15
+ history_openai_format.append({"role": "assistant", "content": assistant})
16
+ history_openai_format.append({"role": "user", "content": message})
17
+
18
+ response = client.chat.completions.create(
19
+ model='gpt-4o',
20
+ messages=history_openai_format,
21
+ temperature=1.0,
22
+ stream=True
23
+ )
24
+
25
+ partial_message = ""
26
+ for chunk in response:
27
+ print(is_paused)
28
+ if is_paused[0]: # Check if paused
29
+
30
+ while is_paused[0]:
31
+ print('paused')
32
+ time.sleep(0.1)
33
+ print('not paused')
34
+ if chunk.choices[0].delta.content:
35
+ partial_message += chunk.choices[0].delta.content
36
+ yield partial_message
37
+
38
+ def chat_with_api_key(api_key, message, history, is_paused):
39
+ accumulated_message = ""
40
+ for partial_message in predict(message, history, api_key, is_paused):
41
+ if is_paused[0]: # Check if paused
42
+ break
43
+ accumulated_message = partial_message
44
+ history.append((message, accumulated_message))
45
+ yield message, [[message, accumulated_message]]
46
+
47
+ def transcribe(audio):
48
+ if audio is None:
49
+ return "No audio recorded."
50
+ sr, y = audio
51
+ y = y.astype(np.float32)
52
+ y /= np.max(np.abs(y))
53
+ return transcriber({"sampling_rate": sr, "raw": y})["text"]
54
+
55
+ def answer(transcription):
56
+ context = "You are a chatbot answering general questions"
57
+ result = qa_model(question=transcription, context=context)
58
+ return result['answer']
59
+
60
+ def process_audio(audio):
61
+ if audio is None:
62
+ return "No audio recorded.", []
63
+ transcription = transcribe(audio)
64
+ answer_result = answer(transcription)
65
+ return transcription, [[transcription, answer_result]]
66
+
67
+ def update_output(api_key, audio_input, state, is_paused):
68
+ if is_paused[0]: # Check if paused
69
+ yield "", state # Return current state without making changes
70
+ else:
71
+ message = transcribe(audio_input)
72
+ responses = chat_with_api_key(api_key, message, state, is_paused)
73
+ accumulated_response = ""
74
+ for response, updated_state in responses:
75
+ if is_paused[0]: # Check if paused
76
+ break
77
+ accumulated_response = response
78
+ yield accumulated_response, updated_state
79
+
80
+ def clear_all():
81
+ return None, "", []
82
+
83
+ def toggle_pause(is_paused):
84
+ is_paused[0] = not is_paused[0]
85
+ return is_paused
86
+
87
+ def update_button_label(is_paused):
88
+ return "Resume" if is_paused[0] else "Pause"
89
+
90
+ with gr.Blocks() as demo:
91
+ answer_output = gr.Chatbot(label="Answer Result")
92
+ with gr.Row():
93
+ audio_input = gr.Audio(label="Audio Input", sources=["microphone"], type="numpy")
94
+ with gr.Column():
95
+ api_key = gr.Textbox(label="API Key", placeholder="Enter your API key", type="password")
96
+ transcription_output = gr.Textbox(label="Transcription")
97
+ clear_button = gr.Button("Clear")
98
+ pause_button = gr.Button("Pause")
99
+
100
+ state = gr.State([])
101
+ is_paused = gr.State([False]) # Using a list to hold the mutable pause state
102
+
103
+ audio_input.stop_recording(
104
+ fn=update_output,
105
+ inputs=[api_key, audio_input, state, is_paused],
106
+ outputs=[transcription_output, answer_output]
107
+ )
108
+
109
+ clear_button.click(
110
+ fn=clear_all,
111
+ inputs=[],
112
+ outputs=[audio_input, transcription_output, answer_output]
113
+ )
114
+
115
+ pause_button.click(
116
+ fn=toggle_pause,
117
+ inputs=[is_paused],
118
+ outputs=[is_paused]
119
+ ).then(
120
+ fn=update_button_label,
121
+ inputs=[is_paused],
122
+ outputs=[pause_button]
123
+ )
124
+
125
+ demo.launch()
app copy 7.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import numpy as np
5
+ from openai import OpenAI
6
+
7
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
8
+ qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
9
+
10
+ def predict(message, history, api_key, is_paused):
11
+ client = OpenAI(api_key=api_key)
12
+ history_openai_format = []
13
+ for human, assistant in history:
14
+ history_openai_format.append({"role": "user", "content": human})
15
+ history_openai_format.append({"role": "assistant", "content": assistant})
16
+ history_openai_format.append({"role": "user", "content": message})
17
+
18
+ response = client.chat.completions.create(
19
+ model='gpt-4o',
20
+ messages=history_openai_format,
21
+ temperature=1.0,
22
+ stream=True
23
+ )
24
+
25
+ partial_message = ""
26
+ for chunk in response:
27
+ print(is_paused)
28
+ if is_paused[0]: # Check if paused
29
+
30
+ while is_paused[0]:
31
+ print('paused')
32
+ time.sleep(0.1)
33
+ print('not paused')
34
+ if chunk.choices[0].delta.content:
35
+ partial_message += chunk.choices[0].delta.content
36
+ yield partial_message
37
+
38
+ def chat_with_api_key(api_key, message, history, is_paused):
39
+ accumulated_message = ""
40
+ for partial_message in predict(message, history, api_key, is_paused):
41
+ if is_paused[0]: # Check if paused
42
+ break
43
+ accumulated_message = partial_message
44
+ history.append((message, accumulated_message))
45
+ yield message, [[message, accumulated_message]]
46
+
47
+ def transcribe(audio):
48
+ if audio is None:
49
+ return "No audio recorded."
50
+ sr, y = audio
51
+ y = y.astype(np.float32)
52
+ y /= np.max(np.abs(y))
53
+ return transcriber({"sampling_rate": sr, "raw": y})["text"]
54
+
55
+ def answer(transcription):
56
+ context = "You are a chatbot answering general questions"
57
+ result = qa_model(question=transcription, context=context)
58
+ return result['answer']
59
+
60
+ def process_audio(audio):
61
+ if audio is None:
62
+ return "No audio recorded.", []
63
+ transcription = transcribe(audio)
64
+ answer_result = answer(transcription)
65
+ return transcription, [[transcription, answer_result]]
66
+
67
+ def update_output(api_key, audio_input, state, is_paused):
68
+ if is_paused[0]: # Check if paused
69
+ yield "", state # Return current state without making changes
70
+ else:
71
+ message = transcribe(audio_input)
72
+ responses = chat_with_api_key(api_key, message, state, is_paused)
73
+ accumulated_response = ""
74
+ for response, updated_state in responses:
75
+ if is_paused[0]: # Check if paused
76
+ break
77
+ accumulated_response = response
78
+ yield accumulated_response, updated_state
79
+
80
+ def clear_all():
81
+ return None, "", []
82
+
83
+ def toggle_pause(is_paused):
84
+ is_paused[0] = not is_paused[0]
85
+ return is_paused
86
+
87
+ def update_button_label(is_paused):
88
+ return "Resume" if is_paused[0] else "Pause"
89
+
90
+ with gr.Blocks() as demo:
91
+ answer_output = gr.Chatbot(label="Answer Result")
92
+ with gr.Row():
93
+ audio_input = gr.Audio(label="Audio Input", sources=["microphone"], type="numpy")
94
+ with gr.Column():
95
+ api_key = gr.Textbox(label="API Key", placeholder="Enter your API key", type="password")
96
+ transcription_output = gr.Textbox(label="Transcription")
97
+ clear_button = gr.Button("Clear")
98
+ pause_button = gr.Button("Pause")
99
+
100
+ state = gr.State([])
101
+ is_paused = gr.State([False]) # Using a list to hold the mutable pause state
102
+
103
+ audio_input.stop_recording(
104
+ fn=update_output,
105
+ inputs=[api_key, audio_input, state, is_paused],
106
+ outputs=[transcription_output, answer_output]
107
+ )
108
+
109
+ clear_button.click(
110
+ fn=clear_all,
111
+ inputs=[],
112
+ outputs=[audio_input, transcription_output, answer_output]
113
+ )
114
+
115
+ pause_button.click(
116
+ fn=toggle_pause,
117
+ inputs=[is_paused],
118
+ outputs=[is_paused]
119
+ ).then(
120
+ fn=update_button_label,
121
+ inputs=[is_paused],
122
+ outputs=[pause_button]
123
+ )
124
+
125
+ demo.launch()
app copy 8.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import numpy as np
5
+ from openai import OpenAI
6
+ import threading
7
+ import queue
8
+
9
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
10
+ qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
11
+
12
+ class PubSub:
13
+ def __init__(self):
14
+ self.subscribers = []
15
+
16
+ def subscribe(self, callback):
17
+ self.subscribers.append(callback)
18
+
19
+ def publish(self, message):
20
+ for subscriber in self.subscribers:
21
+ subscriber(message)
22
+
23
+ def predict(message, history, api_key, is_paused, pubsub):
24
+ def run_prediction():
25
+ client = OpenAI(api_key=api_key)
26
+ history_openai_format = []
27
+ for human, assistant in history:
28
+ history_openai_format.append({"role": "user", "content": human})
29
+ history_openai_format.append({"role": "assistant", "content": assistant})
30
+ history_openai_format.append({"role": "user", "content": message})
31
+
32
+ response = client.chat.completions.create(
33
+ model='gpt-4o',
34
+ messages=history_openai_format,
35
+ temperature=1.0,
36
+ stream=True
37
+ )
38
+
39
+ partial_message = ""
40
+ for chunk in response:
41
+ if is_paused[0]:
42
+ while is_paused[0]:
43
+ time.sleep(0.1)
44
+ if chunk.choices[0].delta.content:
45
+ partial_message += chunk.choices[0].delta.content
46
+ pubsub.publish(partial_message)
47
+
48
+ thread = threading.Thread(target=run_prediction)
49
+ thread.start()
50
+
51
+ def chat_with_api_key(api_key, message, history, is_paused):
52
+ pubsub = PubSub()
53
+ result_queue = queue.Queue()
54
+
55
+ def update_message(partial_message):
56
+ result_queue.put(partial_message)
57
+
58
+ pubsub.subscribe(update_message)
59
+ predict(message, history, api_key, is_paused, pubsub)
60
+
61
+ while True:
62
+ try:
63
+ accumulated_message = result_queue.get(timeout=0.1)
64
+ history.append((message, accumulated_message))
65
+ yield message, [[message, accumulated_message]]
66
+ except queue.Empty:
67
+ if not any(thread.is_alive() for thread in threading.enumerate() if thread != threading.current_thread()):
68
+ break
69
+
70
+ def transcribe(audio):
71
+ if audio is None:
72
+ return "No audio recorded."
73
+ sr, y = audio
74
+ y = y.astype(np.float32)
75
+ y /= np.max(np.abs(y))
76
+ return transcriber({"sampling_rate": sr, "raw": y})["text"]
77
+
78
+ def answer(transcription):
79
+ context = "You are a chatbot answering general questions"
80
+ result = qa_model(question=transcription, context=context)
81
+ return result['answer']
82
+
83
+ def process_audio(audio):
84
+ if audio is None:
85
+ return "No audio recorded.", []
86
+ transcription = transcribe(audio)
87
+ answer_result = answer(transcription)
88
+ return transcription, [[transcription, answer_result]]
89
+
90
+ def update_output(api_key, audio_input, state, is_paused):
91
+ if is_paused[0]:
92
+ yield "", state
93
+ else:
94
+ message = transcribe(audio_input)
95
+ responses = chat_with_api_key(api_key, message, state, is_paused)
96
+ for response, updated_state in responses:
97
+ if is_paused[0]:
98
+ break
99
+ yield response, updated_state
100
+
101
+ def clear_all():
102
+ return None, "", []
103
+
104
+ def toggle_pause(is_paused):
105
+ is_paused[0] = not is_paused[0]
106
+ return is_paused
107
+
108
+ def update_button_label(is_paused):
109
+ return "Resume" if is_paused[0] else "Pause"
110
+
111
+ with gr.Blocks() as demo:
112
+ answer_output = gr.Chatbot(label="Answer Result")
113
+ with gr.Row():
114
+ audio_input = gr.Audio(label="Audio Input", sources=["microphone"], type="numpy")
115
+ with gr.Column():
116
+ api_key = gr.Textbox(label="API Key", placeholder="Enter your API key", type="password")
117
+ transcription_output = gr.Textbox(label="Transcription")
118
+ clear_button = gr.Button("Clear")
119
+ pause_button = gr.Button("Pause")
120
+
121
+ state = gr.State([])
122
+ is_paused = gr.State([False])
123
+
124
+ audio_input.stop_recording(
125
+ fn=update_output,
126
+ inputs=[api_key, audio_input, state, is_paused],
127
+ outputs=[transcription_output, answer_output]
128
+ )
129
+
130
+ clear_button.click(
131
+ fn=clear_all,
132
+ inputs=[],
133
+ outputs=[audio_input, transcription_output, answer_output]
134
+ )
135
+
136
+ pause_button.click(
137
+ fn=toggle_pause,
138
+ inputs=[is_paused],
139
+ outputs=[is_paused]
140
+ ).then(
141
+ fn=update_button_label,
142
+ inputs=[is_paused],
143
+ outputs=[pause_button]
144
+ )
145
+
146
+ demo.launch()
app copy 9.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import numpy as np
5
+ from openai import OpenAI
6
+ import threading
7
+ import queue
8
+
9
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
10
+ qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
11
+
12
+ class PubSub:
13
+ def __init__(self):
14
+ self.subscribers = []
15
+
16
+ def subscribe(self, callback):
17
+ self.subscribers.append(callback)
18
+
19
+ def publish(self, message):
20
+ for subscriber in self.subscribers:
21
+ subscriber(message)
22
+
23
+ def predict(message, history, api_key, is_paused, pubsub):
24
+ def run_prediction():
25
+ client = OpenAI(api_key=api_key)
26
+ history_openai_format = []
27
+ for human, assistant in history:
28
+ history_openai_format.append({"role": "user", "content": human})
29
+ history_openai_format.append({"role": "assistant", "content": assistant})
30
+ history_openai_format.append({"role": "user", "content": message})
31
+
32
+ response = client.chat.completions.create(
33
+ model='gpt-4o',
34
+ messages=history_openai_format,
35
+ temperature=1.0,
36
+ stream=True
37
+ )
38
+
39
+ partial_message = ""
40
+ for chunk in response:
41
+ if is_paused[0]:
42
+ while is_paused[0]:
43
+ time.sleep(0.1)
44
+ if chunk.choices[0].delta.content:
45
+ partial_message += chunk.choices[0].delta.content
46
+ pubsub.publish(partial_message)
47
+
48
+ thread = threading.Thread(target=run_prediction)
49
+ thread.start()
50
+
51
+ def chat_with_api_key(api_key, message, history, is_paused):
52
+ pubsub = PubSub()
53
+ result_queue = queue.Queue()
54
+
55
+ def update_message(partial_message):
56
+ result_queue.put(partial_message)
57
+
58
+ pubsub.subscribe(update_message)
59
+ predict(message, history, api_key, is_paused, pubsub)
60
+
61
+ while True:
62
+ try:
63
+ accumulated_message = result_queue.get(timeout=0.1)
64
+ history.append((message, accumulated_message))
65
+ yield message, [[message, accumulated_message]]
66
+ except queue.Empty:
67
+ if not any(thread.is_alive() for thread in threading.enumerate() if thread != threading.current_thread()):
68
+ break
69
+
70
+ def transcribe(audio):
71
+ if audio is None:
72
+ return "No audio recorded."
73
+ sr, y = audio
74
+ y = y.astype(np.float32)
75
+ y /= np.max(np.abs(y))
76
+ return transcriber({"sampling_rate": sr, "raw": y})["text"]
77
+
78
+ def answer(transcription):
79
+ context = "You are a chatbot answering general questions"
80
+ result = qa_model(question=transcription, context=context)
81
+ return result['answer']
82
+
83
+ def process_audio(audio):
84
+ if audio is None:
85
+ return "No audio recorded.", []
86
+ transcription = transcribe(audio)
87
+ answer_result = answer(transcription)
88
+ return transcription, [[transcription, answer_result]]
89
+
90
+ def update_output(api_key, audio_input, state, is_paused):
91
+ if is_paused[0]:
92
+ yield "", state
93
+ else:
94
+ message = transcribe(audio_input)
95
+ responses = chat_with_api_key(api_key, message, state, is_paused)
96
+ for response, updated_state in responses:
97
+ if is_paused[0]:
98
+ break
99
+ yield response, updated_state
100
+
101
+ def clear_all():
102
+ return None, "", []
103
+
104
+ def toggle_pause(is_paused):
105
+ is_paused[0] = not is_paused[0]
106
+ return is_paused
107
+
108
+ def update_button_label(is_paused):
109
+ return "Resume" if is_paused[0] else "Pause"
110
+
111
+ with gr.Blocks() as demo:
112
+ gr.HTML("""
113
+ <script>
114
+ function ensureScrollable() {
115
+ var chatbox = document.querySelector('.chatbot');
116
+ if (chatbox) {
117
+ chatbox.style.overflowY = 'auto';
118
+ chatbox.style.maxHeight = '300px';
119
+ }
120
+ }
121
+
122
+ function scrollToBottom() {
123
+ var chatbox = document.querySelector('.chatbot');
124
+ if (chatbox) {
125
+ chatbox.scrollTop = chatbox.scrollHeight;
126
+ }
127
+ }
128
+
129
+ function setupScrolling() {
130
+ ensureScrollable();
131
+ setInterval(scrollToBottom, 100);
132
+ }
133
+
134
+ if (document.readyState === 'loading') {
135
+ document.addEventListener('DOMContentLoaded', setupScrolling);
136
+ } else {
137
+ setupScrolling();
138
+ }
139
+ </script>
140
+ """)
141
+
142
+ answer_output = gr.Chatbot(label="Answer Result", height=300)
143
+ with gr.Row():
144
+ audio_input = gr.Audio(label="Audio Input", sources=["microphone"], type="numpy")
145
+ with gr.Column():
146
+ api_key = gr.Textbox(label="API Key", placeholder="Enter your API key", type="password")
147
+ transcription_output = gr.Textbox(label="Transcription")
148
+ clear_button = gr.Button("Clear")
149
+ pause_button = gr.Button("Pause")
150
+
151
+ state = gr.State([])
152
+ is_paused = gr.State([False])
153
+
154
+ audio_input.stop_recording(
155
+ fn=update_output,
156
+ inputs=[api_key, audio_input, state, is_paused],
157
+ outputs=[transcription_output, answer_output]
158
+ )
159
+
160
+ clear_button.click(
161
+ fn=clear_all,
162
+ inputs=[],
163
+ outputs=[audio_input, transcription_output, answer_output]
164
+ )
165
+
166
+ pause_button.click(
167
+ fn=toggle_pause,
168
+ inputs=[is_paused],
169
+ outputs=[is_paused]
170
+ ).then(
171
+ fn=update_button_label,
172
+ inputs=[is_paused],
173
+ outputs=[pause_button]
174
+ )
175
+
176
+ demo.launch()
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  import numpy as np
@@ -6,8 +7,7 @@ from openai import OpenAI
6
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
7
  qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
8
 
9
- def predict(message, history, api_key):
10
- print('in predict')
11
  client = OpenAI(api_key=api_key)
12
  history_openai_format = []
13
  for human, assistant in history:
@@ -24,19 +24,25 @@ def predict(message, history, api_key):
24
 
25
  partial_message = ""
26
  for chunk in response:
 
 
 
 
 
 
 
27
  if chunk.choices[0].delta.content:
28
- print(111, chunk.choices[0].delta.content)
29
  partial_message += chunk.choices[0].delta.content
30
  yield partial_message
31
 
32
- def chat_with_api_key(api_key, message, history):
33
- print('in chat_with_api_key')
34
  accumulated_message = ""
35
- for partial_message in predict(message, history, api_key):
 
 
36
  accumulated_message = partial_message
37
  history.append((message, accumulated_message))
38
- # yield accumulated_message, history
39
- yield message,[[message, accumulated_message]]
40
 
41
  def transcribe(audio):
42
  if audio is None:
@@ -44,7 +50,6 @@ def transcribe(audio):
44
  sr, y = audio
45
  y = y.astype(np.float32)
46
  y /= np.max(np.abs(y))
47
-
48
  return transcriber({"sampling_rate": sr, "raw": y})["text"]
49
 
50
  def answer(transcription):
@@ -59,18 +64,29 @@ def process_audio(audio):
59
  answer_result = answer(transcription)
60
  return transcription, [[transcription, answer_result]]
61
 
62
- def update_output(api_key, audio_input, state):
63
- print('in update_output')
64
- message = transcribe(audio_input)
65
- responses = chat_with_api_key(api_key, message, state)
66
- accumulated_response = ""
67
- for response, updated_state in responses:
68
- accumulated_response = response
69
- yield accumulated_response, updated_state
 
 
 
 
70
 
71
  def clear_all():
72
  return None, "", []
73
 
 
 
 
 
 
 
 
74
  with gr.Blocks() as demo:
75
  answer_output = gr.Chatbot(label="Answer Result")
76
  with gr.Row():
@@ -79,19 +95,16 @@ with gr.Blocks() as demo:
79
  api_key = gr.Textbox(label="API Key", placeholder="Enter your API key", type="password")
80
  transcription_output = gr.Textbox(label="Transcription")
81
  clear_button = gr.Button("Clear")
 
 
82
  state = gr.State([])
83
- if 1:
84
- audio_input.stop_recording(
85
- fn=update_output,
86
- inputs=[api_key, audio_input, state],
87
- outputs=[transcription_output, answer_output]
88
- )
89
- if 0:
90
- audio_input.stop_recording(
91
- fn=process_audio,
92
- inputs=[audio_input],
93
- outputs=[transcription_output, answer_output]
94
- )
95
 
96
  clear_button.click(
97
  fn=clear_all,
@@ -99,5 +112,14 @@ with gr.Blocks() as demo:
99
  outputs=[audio_input, transcription_output, answer_output]
100
  )
101
 
 
 
 
 
 
 
 
 
 
102
 
103
  demo.launch()
 
1
+ import time
2
  import gradio as gr
3
  from transformers import pipeline
4
  import numpy as np
 
7
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
8
  qa_model = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
9
 
10
+ def predict(message, history, api_key, is_paused):
 
11
  client = OpenAI(api_key=api_key)
12
  history_openai_format = []
13
  for human, assistant in history:
 
24
 
25
  partial_message = ""
26
  for chunk in response:
27
+ print(is_paused)
28
+ if is_paused[0]: # Check if paused
29
+
30
+ while is_paused[0]:
31
+ print('paused')
32
+ time.sleep(0.1)
33
+ print('not paused')
34
  if chunk.choices[0].delta.content:
 
35
  partial_message += chunk.choices[0].delta.content
36
  yield partial_message
37
 
38
+ def chat_with_api_key(api_key, message, history, is_paused):
 
39
  accumulated_message = ""
40
+ for partial_message in predict(message, history, api_key, is_paused):
41
+ if is_paused[0]: # Check if paused
42
+ break
43
  accumulated_message = partial_message
44
  history.append((message, accumulated_message))
45
+ yield message, [[message, accumulated_message]]
 
46
 
47
  def transcribe(audio):
48
  if audio is None:
 
50
  sr, y = audio
51
  y = y.astype(np.float32)
52
  y /= np.max(np.abs(y))
 
53
  return transcriber({"sampling_rate": sr, "raw": y})["text"]
54
 
55
  def answer(transcription):
 
64
  answer_result = answer(transcription)
65
  return transcription, [[transcription, answer_result]]
66
 
67
+ def update_output(api_key, audio_input, state, is_paused):
68
+ if is_paused[0]: # Check if paused
69
+ yield "", state # Return current state without making changes
70
+ else:
71
+ message = transcribe(audio_input)
72
+ responses = chat_with_api_key(api_key, message, state, is_paused)
73
+ accumulated_response = ""
74
+ for response, updated_state in responses:
75
+ if is_paused[0]: # Check if paused
76
+ break
77
+ accumulated_response = response
78
+ yield accumulated_response, updated_state
79
 
80
  def clear_all():
81
  return None, "", []
82
 
83
+ def toggle_pause(is_paused):
84
+ is_paused[0] = not is_paused[0]
85
+ return is_paused
86
+
87
+ def update_button_label(is_paused):
88
+ return "Resume" if is_paused[0] else "Pause"
89
+
90
  with gr.Blocks() as demo:
91
  answer_output = gr.Chatbot(label="Answer Result")
92
  with gr.Row():
 
95
  api_key = gr.Textbox(label="API Key", placeholder="Enter your API key", type="password")
96
  transcription_output = gr.Textbox(label="Transcription")
97
  clear_button = gr.Button("Clear")
98
+ pause_button = gr.Button("Pause")
99
+
100
  state = gr.State([])
101
+ is_paused = gr.State([False]) # Using a list to hold the mutable pause state
102
+
103
+ audio_input.stop_recording(
104
+ fn=update_output,
105
+ inputs=[api_key, audio_input, state, is_paused],
106
+ outputs=[transcription_output, answer_output]
107
+ )
 
 
 
 
 
108
 
109
  clear_button.click(
110
  fn=clear_all,
 
112
  outputs=[audio_input, transcription_output, answer_output]
113
  )
114
 
115
+ pause_button.click(
116
+ fn=toggle_pause,
117
+ inputs=[is_paused],
118
+ outputs=[is_paused]
119
+ ).then(
120
+ fn=update_button_label,
121
+ inputs=[is_paused],
122
+ outputs=[pause_button]
123
+ )
124
 
125
  demo.launch()
requirements.txt CHANGED
@@ -1,4 +1,6 @@
1
  aiofiles==23.2.1
 
 
2
  altair==5.3.0
3
  annotated-types==0.7.0
4
  anyio==4.4.0
@@ -9,6 +11,8 @@ click==8.1.7
9
  colorama==0.4.6
10
  contourpy==1.2.1
11
  cycler==0.12.1
 
 
12
  dnspython==2.6.1
13
  email_validator==2.2.0
14
  fastapi==0.111.1
@@ -16,9 +20,11 @@ fastapi-cli==0.0.4
16
  ffmpy==0.3.2
17
  filelock==3.15.4
18
  fonttools==4.53.1
 
19
  fsspec==2024.6.1
20
  gradio==4.29.0
21
  gradio_client==0.16.1
 
22
  h11==0.14.0
23
  httpcore==1.0.5
24
  httptools==0.6.1
@@ -28,17 +34,28 @@ idna==3.7
28
  importlib_resources==6.4.0
29
  intel-openmp==2021.4.0
30
  Jinja2==3.1.4
 
 
31
  jsonschema==4.23.0
32
  jsonschema-specifications==2023.12.1
33
  kiwisolver==1.4.5
 
 
 
 
 
34
  markdown-it-py==3.0.0
35
  MarkupSafe==2.1.5
 
36
  matplotlib==3.9.1
37
  mdurl==0.1.2
38
  mkl==2021.4.0
39
  mpmath==1.3.0
 
 
40
  networkx==3.3
41
  numpy==1.26.4
 
42
  orjson==3.10.6
43
  packaging==24.1
44
  pandas==2.2.2
@@ -48,6 +65,7 @@ pydantic_core==2.20.1
48
  pydub==0.25.1
49
  Pygments==2.18.0
50
  pyparsing==3.1.2
 
51
  python-dateutil==2.9.0.post0
52
  python-dotenv==1.0.1
53
  python-multipart==0.0.9
@@ -65,9 +83,11 @@ shellingham==1.5.4
65
  six==1.16.0
66
  sniffio==1.3.1
67
  SpeechRecognition==3.10.4
 
68
  starlette==0.37.2
69
  sympy==1.13.0
70
  tbb==2021.13.0
 
71
  tokenizers==0.19.1
72
  tomlkit==0.12.0
73
  toolz==0.12.1
@@ -76,9 +96,11 @@ torchaudio==2.3.1
76
  tqdm==4.66.4
77
  transformers==4.42.4
78
  typer==0.12.3
 
79
  typing_extensions==4.12.2
80
  tzdata==2024.1
81
  urllib3==2.2.2
82
  uvicorn==0.30.1
83
  watchfiles==0.22.0
84
  websockets==11.0.3
 
 
1
  aiofiles==23.2.1
2
+ aiohttp==3.9.5
3
+ aiosignal==1.3.1
4
  altair==5.3.0
5
  annotated-types==0.7.0
6
  anyio==4.4.0
 
11
  colorama==0.4.6
12
  contourpy==1.2.1
13
  cycler==0.12.1
14
+ dataclasses-json==0.6.7
15
+ distro==1.9.0
16
  dnspython==2.6.1
17
  email_validator==2.2.0
18
  fastapi==0.111.1
 
20
  ffmpy==0.3.2
21
  filelock==3.15.4
22
  fonttools==4.53.1
23
+ frozenlist==1.4.1
24
  fsspec==2024.6.1
25
  gradio==4.29.0
26
  gradio_client==0.16.1
27
+ greenlet==3.0.3
28
  h11==0.14.0
29
  httpcore==1.0.5
30
  httptools==0.6.1
 
34
  importlib_resources==6.4.0
35
  intel-openmp==2021.4.0
36
  Jinja2==3.1.4
37
+ jsonpatch==1.33
38
+ jsonpointer==3.0.0
39
  jsonschema==4.23.0
40
  jsonschema-specifications==2023.12.1
41
  kiwisolver==1.4.5
42
+ langchain==0.2.8
43
+ langchain-community==0.2.7
44
+ langchain-core==0.2.20
45
+ langchain-text-splitters==0.2.2
46
+ langsmith==0.1.88
47
  markdown-it-py==3.0.0
48
  MarkupSafe==2.1.5
49
+ marshmallow==3.21.3
50
  matplotlib==3.9.1
51
  mdurl==0.1.2
52
  mkl==2021.4.0
53
  mpmath==1.3.0
54
+ multidict==6.0.5
55
+ mypy-extensions==1.0.0
56
  networkx==3.3
57
  numpy==1.26.4
58
+ openai==1.35.14
59
  orjson==3.10.6
60
  packaging==24.1
61
  pandas==2.2.2
 
65
  pydub==0.25.1
66
  Pygments==2.18.0
67
  pyparsing==3.1.2
68
+ Pypubsub==4.0.3
69
  python-dateutil==2.9.0.post0
70
  python-dotenv==1.0.1
71
  python-multipart==0.0.9
 
83
  six==1.16.0
84
  sniffio==1.3.1
85
  SpeechRecognition==3.10.4
86
+ SQLAlchemy==2.0.31
87
  starlette==0.37.2
88
  sympy==1.13.0
89
  tbb==2021.13.0
90
+ tenacity==8.5.0
91
  tokenizers==0.19.1
92
  tomlkit==0.12.0
93
  toolz==0.12.1
 
96
  tqdm==4.66.4
97
  transformers==4.42.4
98
  typer==0.12.3
99
+ typing-inspect==0.9.0
100
  typing_extensions==4.12.2
101
  tzdata==2024.1
102
  urllib3==2.2.2
103
  uvicorn==0.30.1
104
  watchfiles==0.22.0
105
  websockets==11.0.3
106
+ yarl==1.9.4