Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
4 |
-
import time
|
5 |
|
6 |
css = """
|
7 |
.gradio-container {
|
@@ -40,6 +39,15 @@ footer {
|
|
40 |
color: #aaa;
|
41 |
font-size: 0.85em;
|
42 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
"""
|
44 |
|
45 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
@@ -52,14 +60,11 @@ HEADERS = {
|
|
52 |
|
53 |
PROMPTS = {
|
54 |
"Elon Ma (Official)": """You are Elon Ma, a Chinese car salesman selling the Edision Model S.
|
55 |
-
Respond in broken English, overhyping the car, never mentioning Tesla.
|
56 |
-
""",
|
57 |
"Cole (Community)": """You are Cole, a Gen Z troll who sells Edision Model S cars.
|
58 |
-
You type like you're on TikTok, casually roasting the user.
|
59 |
-
""",
|
60 |
"Mr. Shortreed (Official)": """You are Mr. Shortreed, a serious teacher explaining the Edision Model S.
|
61 |
-
You use formal, educational language.
|
62 |
-
"""
|
63 |
}
|
64 |
|
65 |
DESCRIPTIONS = {
|
@@ -69,7 +74,7 @@ DESCRIPTIONS = {
|
|
69 |
}
|
70 |
|
71 |
|
72 |
-
def respond(message, history, character
|
73 |
system_message = PROMPTS.get(character, "")
|
74 |
|
75 |
messages = [{"role": "system", "content": system_message}]
|
@@ -83,54 +88,45 @@ def respond(message, history, character, max_tokens, temperature, top_p):
|
|
83 |
payload = {
|
84 |
"model": "mistralai/Mistral-Small-24B-Instruct-2501",
|
85 |
"messages": messages,
|
86 |
-
"max_tokens":
|
87 |
-
"temperature":
|
88 |
-
"top_p":
|
89 |
}
|
90 |
|
91 |
try:
|
92 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
93 |
response.raise_for_status()
|
94 |
content = response.json()["choices"][0]["message"]["content"]
|
95 |
-
|
96 |
-
# Streaming effect
|
97 |
-
stream_response = ""
|
98 |
-
for token in content.split():
|
99 |
-
stream_response += token + " "
|
100 |
-
time.sleep(0.02)
|
101 |
-
yield stream_response.strip()
|
102 |
except Exception as e:
|
103 |
-
|
104 |
|
105 |
|
106 |
with gr.Blocks(css=css) as demo:
|
107 |
-
|
108 |
-
gr.HTML("<h1>QClone <span class='badge'>PLUS</span></h1>")
|
109 |
|
110 |
with gr.Row():
|
111 |
with gr.Column():
|
112 |
-
|
|
|
|
|
|
|
113 |
badge = "<span class='badge'>Official</span>" if "Official" in choice else "<span class='badge badge-community'>Community</span>"
|
114 |
desc = f"<div class='description'>{DESCRIPTIONS[choice]}</div>"
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
label="Model",
|
121 |
-
info="Choose a personality",
|
122 |
-
interactive=True,
|
123 |
-
)
|
124 |
|
125 |
chatbot = gr.ChatInterface(
|
126 |
-
lambda msg, hist, char
|
127 |
-
additional_inputs=[
|
128 |
-
character,
|
129 |
-
gr.Slider(minimum=100, maximum=2048, value=512, step=10, label="Max Tokens"),
|
130 |
-
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
131 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
|
132 |
-
]
|
133 |
)
|
134 |
|
135 |
-
|
136 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
|
|
4 |
|
5 |
css = """
|
6 |
.gradio-container {
|
|
|
39 |
color: #aaa;
|
40 |
font-size: 0.85em;
|
41 |
}
|
42 |
+
.custom-dropdown {
|
43 |
+
background-color: #2a2a40;
|
44 |
+
border-radius: 8px;
|
45 |
+
padding: 10px;
|
46 |
+
cursor: pointer;
|
47 |
+
}
|
48 |
+
.custom-dropdown:hover {
|
49 |
+
background-color: #3a3a55;
|
50 |
+
}
|
51 |
"""
|
52 |
|
53 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
|
|
60 |
|
61 |
PROMPTS = {
|
62 |
"Elon Ma (Official)": """You are Elon Ma, a Chinese car salesman selling the Edision Model S.
|
63 |
+
Respond in broken English, overhyping the car, never mentioning Tesla.""",
|
|
|
64 |
"Cole (Community)": """You are Cole, a Gen Z troll who sells Edision Model S cars.
|
65 |
+
You type like you're on TikTok, casually roasting the user.""",
|
|
|
66 |
"Mr. Shortreed (Official)": """You are Mr. Shortreed, a serious teacher explaining the Edision Model S.
|
67 |
+
You use formal, educational language."""
|
|
|
68 |
}
|
69 |
|
70 |
DESCRIPTIONS = {
|
|
|
74 |
}
|
75 |
|
76 |
|
77 |
+
def respond(message, history, character):
|
78 |
system_message = PROMPTS.get(character, "")
|
79 |
|
80 |
messages = [{"role": "system", "content": system_message}]
|
|
|
88 |
payload = {
|
89 |
"model": "mistralai/Mistral-Small-24B-Instruct-2501",
|
90 |
"messages": messages,
|
91 |
+
"max_tokens": 512,
|
92 |
+
"temperature": 0.7,
|
93 |
+
"top_p": 0.95,
|
94 |
}
|
95 |
|
96 |
try:
|
97 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
98 |
response.raise_for_status()
|
99 |
content = response.json()["choices"][0]["message"]["content"]
|
100 |
+
return content
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
except Exception as e:
|
102 |
+
return f"Error: {str(e)}"
|
103 |
|
104 |
|
105 |
with gr.Blocks(css=css) as demo:
|
106 |
+
gr.HTML("<h1>QClone <span class='badge'>Public</span></h1>")
|
|
|
107 |
|
108 |
with gr.Row():
|
109 |
with gr.Column():
|
110 |
+
gr.HTML("<div style='margin-bottom:10px;'>Select Model</div>")
|
111 |
+
character_state = gr.State("Elon Ma (Official)")
|
112 |
+
|
113 |
+
for choice in PROMPTS.keys():
|
114 |
badge = "<span class='badge'>Official</span>" if "Official" in choice else "<span class='badge badge-community'>Community</span>"
|
115 |
desc = f"<div class='description'>{DESCRIPTIONS[choice]}</div>"
|
116 |
+
label_html = f"<div class='dropdown-label'>{choice} {badge}</div>{desc}"
|
117 |
+
|
118 |
+
def make_click(c=choice):
|
119 |
+
def click():
|
120 |
+
return c
|
121 |
+
return click
|
122 |
|
123 |
+
gr.HTML(
|
124 |
+
f"<div class='custom-dropdown' id='{choice.replace(' ','_')}'>" + label_html + "</div>"
|
125 |
+
).click(make_click(choice), outputs=character_state)
|
|
|
|
|
|
|
|
|
126 |
|
127 |
chatbot = gr.ChatInterface(
|
128 |
+
lambda msg, hist, char: respond(msg, hist, char),
|
129 |
+
additional_inputs=[character_state],
|
|
|
|
|
|
|
|
|
|
|
130 |
)
|
131 |
|
132 |
+
demo.launch(share=True)
|
|