Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,42 @@ import requests
|
|
4 |
import time
|
5 |
|
6 |
css = """
|
7 |
-
.gradio-container {
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
"""
|
11 |
|
12 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
@@ -17,29 +50,34 @@ HEADERS = {
|
|
17 |
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
18 |
}
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
There is also evil donald ducks and mr long reeds trying to vandlize the cars calling them nazi cars or "sawasticars".
|
29 |
-
"""
|
30 |
|
31 |
-
def respond(message, history,
|
32 |
-
|
33 |
-
system_message = SYSTEM_PROMPT
|
34 |
|
35 |
messages = [{"role": "system", "content": system_message}]
|
36 |
-
|
37 |
for user_msg, bot_msg in history:
|
38 |
if user_msg:
|
39 |
messages.append({"role": "user", "content": user_msg})
|
40 |
if bot_msg:
|
41 |
messages.append({"role": "assistant", "content": bot_msg})
|
42 |
-
|
43 |
messages.append({"role": "user", "content": message})
|
44 |
|
45 |
payload = {
|
@@ -53,10 +91,9 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
53 |
try:
|
54 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
55 |
response.raise_for_status()
|
56 |
-
|
57 |
-
content = data["choices"][0]["message"]["content"]
|
58 |
|
59 |
-
#
|
60 |
stream_response = ""
|
61 |
for token in content.split():
|
62 |
stream_response += token + " "
|
@@ -65,16 +102,34 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
65 |
except Exception as e:
|
66 |
yield f"Error: {str(e)}"
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
gr.
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
demo.launch()
|
|
|
4 |
import time
|
5 |
|
6 |
css = """
|
7 |
+
.gradio-container {
|
8 |
+
background-color: #1e1e2f;
|
9 |
+
color: white;
|
10 |
+
max-width: 800px !important;
|
11 |
+
margin: auto;
|
12 |
+
padding-top: 50px;
|
13 |
+
}
|
14 |
+
h1 {
|
15 |
+
text-align: center;
|
16 |
+
font-size: 2em;
|
17 |
+
margin-bottom: 20px;
|
18 |
+
}
|
19 |
+
footer {
|
20 |
+
visibility: hidden;
|
21 |
+
}
|
22 |
+
.dropdown-label {
|
23 |
+
display: flex;
|
24 |
+
align-items: center;
|
25 |
+
justify-content: space-between;
|
26 |
+
}
|
27 |
+
.badge {
|
28 |
+
background-color: #ffd700;
|
29 |
+
color: black;
|
30 |
+
font-size: 0.75em;
|
31 |
+
padding: 2px 6px;
|
32 |
+
border-radius: 5px;
|
33 |
+
margin-left: 8px;
|
34 |
+
}
|
35 |
+
.badge-community {
|
36 |
+
background-color: #3b82f6;
|
37 |
+
color: white;
|
38 |
+
}
|
39 |
+
.description {
|
40 |
+
color: #aaa;
|
41 |
+
font-size: 0.85em;
|
42 |
+
}
|
43 |
"""
|
44 |
|
45 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
|
|
50 |
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
51 |
}
|
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 = {
|
66 |
+
"Elon Ma (Official)": "Broken English salesman (Official)",
|
67 |
+
"Cole (Community)": "Gen Z slang troll (Community)",
|
68 |
+
"Mr. Shortreed (Official)": "Serious teacher vibes (Official)",
|
69 |
+
}
|
70 |
|
|
|
|
|
71 |
|
72 |
+
def respond(message, history, character, max_tokens, temperature, top_p):
|
73 |
+
system_message = PROMPTS.get(character, "")
|
|
|
74 |
|
75 |
messages = [{"role": "system", "content": system_message}]
|
|
|
76 |
for user_msg, bot_msg in history:
|
77 |
if user_msg:
|
78 |
messages.append({"role": "user", "content": user_msg})
|
79 |
if bot_msg:
|
80 |
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
81 |
messages.append({"role": "user", "content": message})
|
82 |
|
83 |
payload = {
|
|
|
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 + " "
|
|
|
102 |
except Exception as e:
|
103 |
yield f"Error: {str(e)}"
|
104 |
|
105 |
+
|
106 |
+
with gr.Blocks(css=css) as demo:
|
107 |
+
gr.Markdown("# QClone <span class='badge'>PLUS</span>", elem_id="header", unsafe_allow_html=True)
|
108 |
+
|
109 |
+
with gr.Row():
|
110 |
+
with gr.Column():
|
111 |
+
def format_choice(choice):
|
112 |
+
badge = "<span class='badge'>Official</span>" if "Official" in choice else "<span class='badge badge-community'>Community</span>"
|
113 |
+
desc = f"<div class='description'>{DESCRIPTIONS[choice]}</div>"
|
114 |
+
return f"<div class='dropdown-label'>{choice} {badge}</div>{desc}"
|
115 |
+
|
116 |
+
character = gr.Dropdown(
|
117 |
+
choices=list(PROMPTS.keys()),
|
118 |
+
value="Elon Ma (Official)",
|
119 |
+
label="Model",
|
120 |
+
info="Choose a personality",
|
121 |
+
interactive=True,
|
122 |
+
)
|
123 |
+
|
124 |
+
chatbot = gr.ChatInterface(
|
125 |
+
lambda msg, hist, char, max_tokens, temp, top_p: respond(msg, hist, char, max_tokens, temp, top_p),
|
126 |
+
additional_inputs=[
|
127 |
+
character,
|
128 |
+
gr.Slider(minimum=100, maximum=2048, value=512, step=10, label="Max Tokens"),
|
129 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
130 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
|
131 |
+
]
|
132 |
+
)
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
demo.launch()
|