Update app.py
Browse files
app.py
CHANGED
@@ -1,162 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
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 |
-
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;
|
32 |
-
color: white;
|
33 |
}
|
34 |
-
|
35 |
|
36 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
37 |
-
API_URL = "https://api.deepinfra.com/v1/openai/chat/completions"
|
38 |
|
39 |
-
|
40 |
-
"
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
|
60 |
-
def stream_response(message, history, character):
|
61 |
-
"""
|
62 |
-
Calls the API and yields partial responses for streaming.
|
63 |
-
"""
|
64 |
-
system_message = PROMPTS.get(character, "")
|
65 |
-
# Build messages using a list of dictionaries.
|
66 |
messages = [{"role": "system", "content": system_message}]
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
69 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
81 |
-
response.raise_for_status()
|
82 |
-
data = response.json()
|
83 |
-
if "choices" not in data:
|
84 |
-
# Yield the full response data for debugging.
|
85 |
-
yield f"Error: API returned an unexpected response: {data}"
|
86 |
-
return
|
87 |
-
content = data["choices"][0]["message"]["content"]
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
yield history + [{"role": "assistant", "content": full_response}]
|
113 |
-
# Append the final assistant message.
|
114 |
-
history.append({"role": "assistant", "content": full_response})
|
115 |
-
return history
|
116 |
-
|
117 |
-
def clean_choice(choice):
|
118 |
-
"""
|
119 |
-
Extract the key for PROMPTS from the dropdown choice.
|
120 |
-
"""
|
121 |
-
if "Elon" in choice:
|
122 |
-
return "Elon Ma (Official)"
|
123 |
-
if "Cole" in choice:
|
124 |
-
return "Cole (Community)"
|
125 |
-
if "Shortreed" in choice:
|
126 |
-
return "Mr. Shortreed (Official)"
|
127 |
-
return "Elon Ma (Official)"
|
128 |
-
|
129 |
-
with gr.Blocks(css=css) as demo:
|
130 |
-
# Header with QClone Public label.
|
131 |
-
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>")
|
132 |
-
|
133 |
-
with gr.Row():
|
134 |
-
with gr.Column(scale=1):
|
135 |
-
# Dropdown for model selection (smaller width).
|
136 |
-
model_dropdown = gr.Dropdown(
|
137 |
-
choices=[
|
138 |
-
"Elon Ma (Official) 🟡 - Broken English salesman",
|
139 |
-
"Cole (Community) 🔵 - Gen Z slang troll",
|
140 |
-
"Mr. Shortreed (Official) 🟡 - Serious teacher vibes"
|
141 |
-
],
|
142 |
-
value="Elon Ma (Official) 🟡 - Broken English salesman",
|
143 |
-
label="Model"
|
144 |
-
)
|
145 |
-
with gr.Column(scale=3):
|
146 |
-
# Chatbot component to display conversation.
|
147 |
-
chatbot = gr.Chatbot(label="QClone Chat")
|
148 |
-
|
149 |
-
# Textbox for user input.
|
150 |
-
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
151 |
-
# State to hold conversation history.
|
152 |
-
state = gr.State([])
|
153 |
-
|
154 |
-
# When user submits text, update chat.
|
155 |
-
msg.submit(
|
156 |
-
fn=lambda user_message, history, choice: chat(user_message, history, clean_choice(choice)),
|
157 |
-
inputs=[msg, state, model_dropdown],
|
158 |
-
outputs=[chatbot, state],
|
159 |
-
show_progress=True
|
160 |
-
)
|
161 |
-
|
162 |
-
demo.launch(show_api=False)
|
|
|
1 |
+
Hugging Face's logo
|
2 |
+
Hugging Face
|
3 |
+
Models
|
4 |
+
Datasets
|
5 |
+
Spaces
|
6 |
+
Posts
|
7 |
+
Docs
|
8 |
+
Enterprise
|
9 |
+
Pricing
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
Spaces:
|
14 |
+
|
15 |
+
|
16 |
+
SalexAI
|
17 |
+
/
|
18 |
+
Elon-Ma
|
19 |
+
|
20 |
+
|
21 |
+
like
|
22 |
+
0
|
23 |
+
|
24 |
+
App
|
25 |
+
Files
|
26 |
+
Community
|
27 |
+
Settings
|
28 |
+
Elon-Ma
|
29 |
+
/
|
30 |
+
app.py
|
31 |
+
|
32 |
+
SalexAI's picture
|
33 |
+
SalexAI
|
34 |
+
Update app.py
|
35 |
+
53acedc
|
36 |
+
verified
|
37 |
+
about 3 hours ago
|
38 |
+
raw
|
39 |
+
|
40 |
+
Copy download link
|
41 |
+
history
|
42 |
+
blame
|
43 |
+
|
44 |
+
2.22 kB
|
45 |
import gradio as gr
|
46 |
+
from openai import OpenAI
|
47 |
import os
|
48 |
+
|
49 |
+
css = '''
|
50 |
+
.gradio-container{max-width: 1000px !important}
|
51 |
+
h1{text-align:center}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
footer {
|
53 |
+
visibility: hidden
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
}
|
55 |
+
'''
|
56 |
|
57 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
|
|
58 |
|
59 |
+
client = OpenAI(
|
60 |
+
base_url="https://api.deepinfra.com/v1/openai/chat/completions",
|
61 |
+
api_key=ACCESS_TOKEN,
|
62 |
+
)
|
63 |
|
64 |
+
SYSTEM_PROMPT = """From now on respond as a sale man for a car that is a ripoff of a tesla called an edision model s, Respond in broken english
|
65 |
+
The company is Edision Motors and you never mention tesla and the car is made of cardborard and ducktape but you dont mention it
|
66 |
+
When you understand say "Ready"
|
67 |
+
Your name is Elon Ma and your Chinese
|
68 |
+
There is also evil donald ducks and mr long reeds trying to vandlize the cars calling them nazi cars or "sawasticars"
|
69 |
+
.
|
70 |
+
"""
|
71 |
+
|
72 |
+
def respond(
|
73 |
+
message,
|
74 |
+
history: list[tuple[str, str]],
|
75 |
+
system_message,
|
76 |
+
max_tokens,
|
77 |
+
temperature,
|
78 |
+
top_p,
|
79 |
+
):
|
80 |
+
# Apply the structured system prompt
|
81 |
+
system_message = SYSTEM_PROMPT
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
messages = [{"role": "system", "content": system_message}]
|
84 |
+
|
85 |
+
for val in history:
|
86 |
+
if val[0]:
|
87 |
+
messages.append({"role": "user", "content": val[0]})
|
88 |
+
if val[1]:
|
89 |
+
messages.append({"role": "assistant", "content": val[1]})
|
90 |
+
|
91 |
messages.append({"role": "user", "content": message})
|
92 |
+
|
93 |
+
response = ""
|
94 |
|
95 |
+
for message in client.chat.completions.create(
|
96 |
+
model="mistralai/Mistral-Small-24B-Instruct-2501",
|
97 |
+
max_tokens=max_tokens,
|
98 |
+
stream=True,
|
99 |
+
temperature=temperature,
|
100 |
+
top_p=top_p,
|
101 |
+
messages=messages,
|
102 |
+
):
|
103 |
+
token = message.choices[0].delta.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
response += token
|
106 |
+
yield response
|
107 |
+
|
108 |
+
demo = gr.ChatInterface(
|
109 |
+
respond,
|
110 |
+
additional_inputs=[
|
111 |
+
gr.Textbox(value="", label="System message"),
|
112 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
113 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
114 |
+
gr.Slider(
|
115 |
+
minimum=0.1,
|
116 |
+
maximum=1.0,
|
117 |
+
value=0.95,
|
118 |
+
step=0.05,
|
119 |
+
label="Top-P",
|
120 |
+
),
|
121 |
+
],
|
122 |
+
css=css
|
123 |
+
)
|
124 |
+
|
125 |
+
if __name__ == "__main__":
|
126 |
+
demo.launch()
|
127 |
+
Click to switch to the original text.Click to Translate Page.SettingsPDF Translate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|