Update app.py
Browse files
app.py
CHANGED
@@ -22,10 +22,10 @@ footer {
|
|
22 |
select {
|
23 |
background-color: #2a2a40;
|
24 |
color: white;
|
25 |
-
padding: 10px;
|
26 |
border-radius: 8px;
|
27 |
border: 1px solid #444;
|
28 |
-
width:
|
29 |
}
|
30 |
option {
|
31 |
background-color: #2a2a40;
|
@@ -41,7 +41,6 @@ HEADERS = {
|
|
41 |
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
42 |
}
|
43 |
|
44 |
-
# Define our system prompts
|
45 |
PROMPTS = {
|
46 |
"Elon Ma (Official)": (
|
47 |
"You are Elon Ma, a Chinese car salesman selling the Edision Model S.\n"
|
@@ -79,12 +78,12 @@ def respond(message, history, character):
|
|
79 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
80 |
response.raise_for_status()
|
81 |
content = response.json()["choices"][0]["message"]["content"]
|
82 |
-
|
83 |
stream_response = ""
|
84 |
for token in content.split():
|
85 |
stream_response += token + " "
|
86 |
time.sleep(0.02)
|
87 |
-
# Yield a dictionary
|
88 |
yield {"role": "assistant", "content": stream_response.strip()}
|
89 |
except Exception as e:
|
90 |
yield {"role": "assistant", "content": f"Error: {str(e)}"}
|
@@ -93,8 +92,6 @@ with gr.Blocks(css=css) as demo:
|
|
93 |
# Header with QClone Public label
|
94 |
gr.HTML("<h1>QClone <span style='background-color:#3b82f6;color:white;font-size:0.75em;padding:2px 6px;border-radius:5px;margin-left:8px;'>Public</span></h1>")
|
95 |
|
96 |
-
# Create a dropdown with rich labels in the text.
|
97 |
-
# The dropdown options include emojis and descriptions.
|
98 |
with gr.Row():
|
99 |
with gr.Column():
|
100 |
character = gr.Dropdown(
|
@@ -107,7 +104,6 @@ with gr.Blocks(css=css) as demo:
|
|
107 |
label="Model"
|
108 |
)
|
109 |
|
110 |
-
# Helper function to extract the clean key for our prompts.
|
111 |
def clean_choice(choice):
|
112 |
if "Elon" in choice:
|
113 |
return "Elon Ma (Official)"
|
@@ -117,11 +113,11 @@ with gr.Blocks(css=css) as demo:
|
|
117 |
return "Mr. Shortreed (Official)"
|
118 |
return "Elon Ma (Official)"
|
119 |
|
120 |
-
# ChatInterface with streaming enabled
|
121 |
chatbot = gr.ChatInterface(
|
122 |
lambda msg, hist, char: respond(msg, hist, clean_choice(char)),
|
123 |
additional_inputs=[character],
|
124 |
-
type="messages"
|
|
|
125 |
)
|
126 |
|
127 |
demo.launch(share=True)
|
|
|
22 |
select {
|
23 |
background-color: #2a2a40;
|
24 |
color: white;
|
25 |
+
padding: 6px 10px;
|
26 |
border-radius: 8px;
|
27 |
border: 1px solid #444;
|
28 |
+
width: 300px;
|
29 |
}
|
30 |
option {
|
31 |
background-color: #2a2a40;
|
|
|
41 |
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
42 |
}
|
43 |
|
|
|
44 |
PROMPTS = {
|
45 |
"Elon Ma (Official)": (
|
46 |
"You are Elon Ma, a Chinese car salesman selling the Edision Model S.\n"
|
|
|
78 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
79 |
response.raise_for_status()
|
80 |
content = response.json()["choices"][0]["message"]["content"]
|
81 |
+
|
82 |
stream_response = ""
|
83 |
for token in content.split():
|
84 |
stream_response += token + " "
|
85 |
time.sleep(0.02)
|
86 |
+
# Yield a message dictionary for streaming updates.
|
87 |
yield {"role": "assistant", "content": stream_response.strip()}
|
88 |
except Exception as e:
|
89 |
yield {"role": "assistant", "content": f"Error: {str(e)}"}
|
|
|
92 |
# Header with QClone Public label
|
93 |
gr.HTML("<h1>QClone <span style='background-color:#3b82f6;color:white;font-size:0.75em;padding:2px 6px;border-radius:5px;margin-left:8px;'>Public</span></h1>")
|
94 |
|
|
|
|
|
95 |
with gr.Row():
|
96 |
with gr.Column():
|
97 |
character = gr.Dropdown(
|
|
|
104 |
label="Model"
|
105 |
)
|
106 |
|
|
|
107 |
def clean_choice(choice):
|
108 |
if "Elon" in choice:
|
109 |
return "Elon Ma (Official)"
|
|
|
113 |
return "Mr. Shortreed (Official)"
|
114 |
return "Elon Ma (Official)"
|
115 |
|
|
|
116 |
chatbot = gr.ChatInterface(
|
117 |
lambda msg, hist, char: respond(msg, hist, clean_choice(char)),
|
118 |
additional_inputs=[character],
|
119 |
+
type="messages", # Use OpenAI-style message format.
|
120 |
+
stream=True # Enable streaming.
|
121 |
)
|
122 |
|
123 |
demo.launch(share=True)
|