Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 6 |
|
| 7 |
-
def
|
|
|
|
|
|
|
|
|
|
| 8 |
response = client.chat.completions.create(
|
| 9 |
model="gpt-4o-mini",
|
| 10 |
messages=[
|
| 11 |
{"role": "system", "content": "You are a helpful assistant."},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
{"role": "user", "content": message}
|
| 13 |
-
]
|
|
|
|
| 14 |
)
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
css = """
|
| 18 |
body {
|
| 19 |
background-color: #0f0f0f;
|
| 20 |
}
|
| 21 |
.gradio-container {
|
| 22 |
-
max-width:
|
| 23 |
margin: auto;
|
| 24 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
textarea {
|
| 26 |
background-color: #1e1e1e !important;
|
| 27 |
color: white !important;
|
|
|
|
| 28 |
}
|
| 29 |
"""
|
| 30 |
|
|
@@ -32,25 +55,26 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
| 32 |
gr.Markdown(
|
| 33 |
"""
|
| 34 |
<h1 style="text-align:center;">💬 ChatGPT‑style AI</h1>
|
| 35 |
-
<p style="text-align:center; color:
|
| 36 |
-
Powered by OpenAI
|
| 37 |
</p>
|
| 38 |
"""
|
| 39 |
)
|
| 40 |
|
| 41 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
msg = gr.Textbox(
|
| 43 |
-
placeholder="Send a message
|
| 44 |
-
show_label=False
|
|
|
|
| 45 |
)
|
| 46 |
-
clear = gr.Button("Clear")
|
| 47 |
|
| 48 |
-
|
| 49 |
-
reply = chat(message)
|
| 50 |
-
history.append((message, reply))
|
| 51 |
-
return "", history
|
| 52 |
|
| 53 |
-
msg.submit(
|
| 54 |
clear.click(lambda: [], None, chatbot)
|
| 55 |
|
| 56 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
+
def stream_chat(message, history):
|
| 9 |
+
history = history or []
|
| 10 |
+
history.append((message, ""))
|
| 11 |
+
|
| 12 |
response = client.chat.completions.create(
|
| 13 |
model="gpt-4o-mini",
|
| 14 |
messages=[
|
| 15 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 16 |
+
*[
|
| 17 |
+
{"role": "user" if i % 2 == 0 else "assistant", "content": m}
|
| 18 |
+
for pair in history[:-1]
|
| 19 |
+
for i, m in enumerate(pair)
|
| 20 |
+
],
|
| 21 |
{"role": "user", "content": message}
|
| 22 |
+
],
|
| 23 |
+
stream=True
|
| 24 |
)
|
| 25 |
+
|
| 26 |
+
partial = ""
|
| 27 |
+
for chunk in response:
|
| 28 |
+
if chunk.choices[0].delta.get("content"):
|
| 29 |
+
partial += chunk.choices[0].delta["content"]
|
| 30 |
+
history[-1] = (message, partial)
|
| 31 |
+
yield history
|
| 32 |
|
| 33 |
css = """
|
| 34 |
body {
|
| 35 |
background-color: #0f0f0f;
|
| 36 |
}
|
| 37 |
.gradio-container {
|
| 38 |
+
max-width: 720px !important;
|
| 39 |
margin: auto;
|
| 40 |
}
|
| 41 |
+
.message.user {
|
| 42 |
+
background: #343541 !important;
|
| 43 |
+
}
|
| 44 |
+
.message.bot {
|
| 45 |
+
background: #1e1e1e !important;
|
| 46 |
+
}
|
| 47 |
textarea {
|
| 48 |
background-color: #1e1e1e !important;
|
| 49 |
color: white !important;
|
| 50 |
+
border-radius: 12px !important;
|
| 51 |
}
|
| 52 |
"""
|
| 53 |
|
|
|
|
| 55 |
gr.Markdown(
|
| 56 |
"""
|
| 57 |
<h1 style="text-align:center;">💬 ChatGPT‑style AI</h1>
|
| 58 |
+
<p style="text-align:center; color:#888;">
|
| 59 |
+
Powered by OpenAI ChatGPT
|
| 60 |
</p>
|
| 61 |
"""
|
| 62 |
)
|
| 63 |
|
| 64 |
+
chatbot = gr.Chatbot(
|
| 65 |
+
height=420,
|
| 66 |
+
avatar_images=("https://i.imgur.com/7k12EPD.png", "https://i.imgur.com/9k6dKQy.png")
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
msg = gr.Textbox(
|
| 70 |
+
placeholder="Send a message…",
|
| 71 |
+
show_label=False,
|
| 72 |
+
autofocus=True
|
| 73 |
)
|
|
|
|
| 74 |
|
| 75 |
+
clear = gr.Button("🗑 Clear chat")
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
msg.submit(stream_chat, [msg, chatbot], chatbot)
|
| 78 |
clear.click(lambda: [], None, chatbot)
|
| 79 |
|
| 80 |
demo.launch()
|