Spaces:
Sleeping
Sleeping
1lint
commited on
Commit
β’
11ec56e
1
Parent(s):
5c248e8
init commit
Browse files
README.md
CHANGED
@@ -4,10 +4,10 @@ emoji: π
|
|
4 |
colorFrom: green
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
|
|
|
4 |
colorFrom: green
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.30.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
Gradio app that streams chat completions from the OPENAI API service. Get an API Key at https://platform.openai.com/account/api-keys to try this out.
|
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
import openai
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
|
7 |
+
def transcribe(audio, chatbot_history, openai_key):
|
8 |
+
time.sleep(5)
|
9 |
+
transcript = openai.Audio.transcribe("whisper-1", open(audio, "rb"), api_key=openai_key)
|
10 |
+
|
11 |
+
content = transcript["text"]
|
12 |
+
if content:
|
13 |
+
if not chatbot_history:
|
14 |
+
return [[content, None]]
|
15 |
+
else:
|
16 |
+
return chatbot_history + [[content, None]]
|
17 |
+
else:
|
18 |
+
return chatbot_history
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
def openai_stream(history, openai_key, chat_model):
|
23 |
+
|
24 |
+
if not history or history[-1][1]:
|
25 |
+
return history
|
26 |
+
|
27 |
+
history[-1][1] = ""
|
28 |
+
for chunk in openai.ChatCompletion.create(
|
29 |
+
model=chat_model,
|
30 |
+
messages=[{
|
31 |
+
"role": "user",
|
32 |
+
"content": history[-1][0]
|
33 |
+
}],
|
34 |
+
stream=True,
|
35 |
+
api_key=openai_key,
|
36 |
+
):
|
37 |
+
content = chunk["choices"][0].get("delta", {}).get("content")
|
38 |
+
if content:
|
39 |
+
history[-1][1] += content
|
40 |
+
yield history
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
def show_message(user_message, history):
|
45 |
+
return "", history + [[user_message, None]]
|
46 |
+
|
47 |
+
theme = gr.themes.Soft(
|
48 |
+
primary_hue="blue",
|
49 |
+
neutral_hue="slate",
|
50 |
+
)
|
51 |
+
|
52 |
+
parent_path = Path(__file__).parent
|
53 |
+
|
54 |
+
with open(parent_path / "header.MD") as fp:
|
55 |
+
header = fp.read()
|
56 |
+
|
57 |
+
available_models = ['gpt-4', 'gpt-4-0314', 'gpt-4-32k', 'gpt-4-32k-0314', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301']
|
58 |
+
|
59 |
+
with gr.Blocks(theme=theme) as demo:
|
60 |
+
|
61 |
+
|
62 |
+
header_component = gr.Markdown(header)
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column(scale=1):
|
65 |
+
audio = gr.Audio(label="Talk with ChatGPT", source="microphone", type="filepath", streaming=True)
|
66 |
+
clear = gr.Button("Clear Chat History")
|
67 |
+
dark_mode_btn = gr.Button("Dark Mode", variant="primary")
|
68 |
+
|
69 |
+
with gr.Column(scale=2):
|
70 |
+
with gr.Row():
|
71 |
+
chat_model = gr.Dropdown(choices=available_models, value="gpt-3.5-turbo", allow_custom_value=True)
|
72 |
+
openai_key = gr.Textbox(label="Enter OPENAI API Key", placeholder="Example: sk-AJDKakdAJD...")
|
73 |
+
chatbot = gr.Chatbot(label="ChatGPT Dialog")
|
74 |
+
msg = gr.Textbox(label="Chat with ChatGPT", placeholder="Press <Enter> to submit")
|
75 |
+
|
76 |
+
streaming_event_kwargs = dict(
|
77 |
+
fn=openai_stream,
|
78 |
+
inputs=[chatbot, openai_key, chat_model],
|
79 |
+
outputs=chatbot,
|
80 |
+
)
|
81 |
+
|
82 |
+
|
83 |
+
msg.submit(show_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
84 |
+
**streaming_event_kwargs
|
85 |
+
)
|
86 |
+
audio.stream(transcribe, inputs=[audio, chatbot, openai_key], outputs=[chatbot]).then(
|
87 |
+
**streaming_event_kwargs
|
88 |
+
)
|
89 |
+
|
90 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
91 |
+
|
92 |
+
# from gradio.themes.builder
|
93 |
+
toggle_dark_mode_args = dict(
|
94 |
+
fn=None,
|
95 |
+
inputs=None,
|
96 |
+
outputs=None,
|
97 |
+
_js="""() => {
|
98 |
+
if (document.querySelectorAll('.dark').length) {
|
99 |
+
document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark'));
|
100 |
+
} else {
|
101 |
+
document.querySelector('body').classList.add('dark');
|
102 |
+
}
|
103 |
+
}""",
|
104 |
+
)
|
105 |
+
demo.load(**toggle_dark_mode_args)
|
106 |
+
dark_mode_btn.click(**toggle_dark_mode_args)
|
107 |
+
|
108 |
+
|
109 |
+
demo.queue()
|
110 |
+
demo.launch()
|
111 |
+
|
header.MD
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# <p style="text-align: center;">**Streaming Chatbot Interface**</p>
|
3 |
+
|
4 |
+
## <p style="text-align: center;">Talk or chat with ChatGPT</p>
|
5 |
+
|
6 |
+
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
openai
|