Update space
Browse files
app.py
CHANGED
|
@@ -2,34 +2,37 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import time
|
| 4 |
import gc
|
| 5 |
-
import torch
|
| 6 |
|
| 7 |
def seize_respond(
|
| 8 |
message,
|
| 9 |
-
history
|
| 10 |
system_message,
|
| 11 |
max_tokens,
|
| 12 |
temperature,
|
| 13 |
top_p,
|
| 14 |
-
hf_token
|
| 15 |
):
|
| 16 |
"""
|
| 17 |
-
🎯 Seize: Direct Serious AI powered by
|
| 18 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Use smaller, more reliable model
|
| 20 |
client = InferenceClient(
|
| 21 |
-
token=hf_token
|
| 22 |
-
model="HuggingFaceH4/zephyr-7b-beta"
|
| 23 |
)
|
| 24 |
|
| 25 |
# Limit conversation history to prevent memory issues
|
| 26 |
if len(history) > 6:
|
| 27 |
history = history[-6:]
|
| 28 |
|
| 29 |
-
# Seize2B system prompt
|
| 30 |
-
seize2b_system = """You are Seize,
|
| 31 |
-
|
| 32 |
-
IDENTITY: Seize (
|
| 33 |
STYLE: Direct, serious, professional
|
| 34 |
RULES:
|
| 35 |
1. Respond with maximum conciseness
|
|
@@ -39,10 +42,22 @@ RULES:
|
|
| 39 |
5. Never apologize or say "I'm here to help"
|
| 40 |
6. Focus only on the question asked
|
| 41 |
7. If you don't know, say "No data" or "Cannot compute"
|
| 42 |
-
8. Default response length: 1-3 sentences
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
messages = [{"role": "system", "content": seize2b_system}]
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
|
| 48 |
response = ""
|
|
@@ -51,107 +66,146 @@ RULES:
|
|
| 51 |
time.sleep(0.3)
|
| 52 |
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
messages,
|
| 56 |
-
max_tokens=min(max_tokens, 256),
|
| 57 |
stream=True,
|
| 58 |
-
temperature=min(temperature, 0.7),
|
| 59 |
top_p=top_p,
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if
|
| 64 |
-
token = choices[0].delta.content
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
except Exception as e:
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Cleanup
|
| 74 |
gc.collect()
|
| 75 |
-
if torch.cuda.is_available():
|
| 76 |
-
torch.cuda.empty_cache()
|
| 77 |
-
|
| 78 |
-
# Rest of your UI code remains the same...
|
| 79 |
-
# Create Seize Chat Interface
|
| 80 |
-
chatbot = gr.ChatInterface(
|
| 81 |
-
seize_respond,
|
| 82 |
-
type="messages",
|
| 83 |
-
title="Seize (Seize2B Model)",
|
| 84 |
-
description="Direct AI assistant. Using efficient model for stability.",
|
| 85 |
-
# ... rest of your UI code
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
# ... rest of your layout codeimport gradio as gr
|
| 89 |
-
from huggingface_hub import InferenceClient
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
def respond(
|
| 93 |
-
message,
|
| 94 |
-
history: list[dict[str, str]],
|
| 95 |
-
system_message,
|
| 96 |
-
max_tokens,
|
| 97 |
-
temperature,
|
| 98 |
-
top_p,
|
| 99 |
-
hf_token: gr.OAuthToken,
|
| 100 |
-
):
|
| 101 |
-
"""
|
| 102 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 103 |
-
"""
|
| 104 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 105 |
-
|
| 106 |
-
messages = [{"role": "system", "content": system_message}]
|
| 107 |
-
|
| 108 |
-
messages.extend(history)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
response = ""
|
| 113 |
-
|
| 114 |
-
for message in client.chat_completion(
|
| 115 |
-
messages,
|
| 116 |
-
max_tokens=max_tokens,
|
| 117 |
-
stream=True,
|
| 118 |
-
temperature=temperature,
|
| 119 |
-
top_p=top_p,
|
| 120 |
-
):
|
| 121 |
-
choices = message.choices
|
| 122 |
-
token = ""
|
| 123 |
-
if len(choices) and choices[0].delta.content:
|
| 124 |
-
token = choices[0].delta.content
|
| 125 |
-
|
| 126 |
-
response += token
|
| 127 |
-
yield response
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
"""
|
| 131 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 132 |
-
"""
|
| 133 |
chatbot = gr.ChatInterface(
|
| 134 |
-
|
| 135 |
-
type="messages",
|
| 136 |
additional_inputs=[
|
| 137 |
-
gr.Textbox(
|
| 138 |
-
gr.Slider(
|
| 139 |
-
gr.Slider(
|
| 140 |
-
gr.Slider(
|
| 141 |
-
|
| 142 |
-
maximum=1.0,
|
| 143 |
-
value=0.95,
|
| 144 |
-
step=0.05,
|
| 145 |
-
label="Top-p (nucleus sampling)",
|
| 146 |
-
),
|
| 147 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
chatbot.render()
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
if __name__ == "__main__":
|
| 157 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import time
|
| 4 |
import gc
|
|
|
|
| 5 |
|
| 6 |
def seize_respond(
|
| 7 |
message,
|
| 8 |
+
history,
|
| 9 |
system_message,
|
| 10 |
max_tokens,
|
| 11 |
temperature,
|
| 12 |
top_p,
|
| 13 |
+
hf_token,
|
| 14 |
):
|
| 15 |
"""
|
| 16 |
+
🎯 Seize: Direct Serious AI powered by efficient model
|
| 17 |
"""
|
| 18 |
+
if not hf_token:
|
| 19 |
+
yield "Error: No Hugging Face token provided. Please log in."
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
# Use smaller, more reliable model
|
| 23 |
client = InferenceClient(
|
| 24 |
+
token=hf_token,
|
| 25 |
+
model="HuggingFaceH4/zephyr-7b-beta"
|
| 26 |
)
|
| 27 |
|
| 28 |
# Limit conversation history to prevent memory issues
|
| 29 |
if len(history) > 6:
|
| 30 |
history = history[-6:]
|
| 31 |
|
| 32 |
+
# Seize2B system prompt
|
| 33 |
+
seize2b_system = """You are Seize, a direct AI assistant.
|
| 34 |
+
|
| 35 |
+
IDENTITY: Seize (Efficient Model)
|
| 36 |
STYLE: Direct, serious, professional
|
| 37 |
RULES:
|
| 38 |
1. Respond with maximum conciseness
|
|
|
|
| 42 |
5. Never apologize or say "I'm here to help"
|
| 43 |
6. Focus only on the question asked
|
| 44 |
7. If you don't know, say "No data" or "Cannot compute"
|
| 45 |
+
8. Default response length: 1-3 sentences
|
| 46 |
|
| 47 |
+
EXAMPLE INTERACTIONS:
|
| 48 |
+
User: what's good
|
| 49 |
+
Seize: Systems operational. Inquiry?
|
| 50 |
+
|
| 51 |
+
User: explain quantum computing
|
| 52 |
+
Seize: Quantum computing: Qubits, superposition, entanglement. Enables parallel processing."""
|
| 53 |
+
|
| 54 |
messages = [{"role": "system", "content": seize2b_system}]
|
| 55 |
+
|
| 56 |
+
# Convert Gradio history format to chat completion format
|
| 57 |
+
for h in history:
|
| 58 |
+
messages.append({"role": "user", "content": h[0]})
|
| 59 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 60 |
+
|
| 61 |
messages.append({"role": "user", "content": message})
|
| 62 |
|
| 63 |
response = ""
|
|
|
|
| 66 |
time.sleep(0.3)
|
| 67 |
|
| 68 |
try:
|
| 69 |
+
stream = client.chat_completion(
|
| 70 |
messages,
|
| 71 |
+
max_tokens=min(max_tokens, 256),
|
| 72 |
stream=True,
|
| 73 |
+
temperature=min(temperature, 0.7),
|
| 74 |
top_p=top_p,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
for chunk in stream:
|
| 78 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 79 |
+
token = chunk.choices[0].delta.content
|
| 80 |
+
response += token
|
| 81 |
+
time.sleep(0.005)
|
| 82 |
+
yield response
|
| 83 |
+
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
+
error_msg = str(e)
|
| 86 |
+
if "rate limit" in error_msg.lower():
|
| 87 |
+
yield "Rate limit exceeded. Please wait 10-20 seconds before next message."
|
| 88 |
+
elif "memory" in error_msg.lower() or "out of memory" in error_msg.lower():
|
| 89 |
+
yield "Memory limit reached. Please use shorter prompts or wait a moment."
|
| 90 |
+
elif "authentication" in error_msg.lower():
|
| 91 |
+
yield "Authentication failed. Please check your Hugging Face token."
|
| 92 |
+
else:
|
| 93 |
+
yield f"Error: {error_msg[:80]}"
|
| 94 |
|
| 95 |
# Cleanup
|
| 96 |
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Create the chatbot with all parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
chatbot = gr.ChatInterface(
|
| 100 |
+
fn=seize_respond,
|
|
|
|
| 101 |
additional_inputs=[
|
| 102 |
+
gr.Textbox("You are Seize. Be direct and serious.", label="System Message", visible=False),
|
| 103 |
+
gr.Slider(64, 512, value=256, step=64, label="Max Tokens"),
|
| 104 |
+
gr.Slider(0.1, 1.0, value=0.3, step=0.1, label="Temperature"),
|
| 105 |
+
gr.Slider(0.1, 1.0, value=0.9, step=0.1, label="Top-p"),
|
| 106 |
+
gr.Textbox(label="HF Token", type="password", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
],
|
| 108 |
+
type="messages",
|
| 109 |
+
title="Seize (Efficient Model)",
|
| 110 |
+
description="Direct AI assistant. No fluff. Serious responses only.",
|
| 111 |
+
theme=gr.themes.Soft(
|
| 112 |
+
primary_hue="blue",
|
| 113 |
+
secondary_hue="gray",
|
| 114 |
+
neutral_hue="gray",
|
| 115 |
+
),
|
| 116 |
+
examples=[
|
| 117 |
+
["what's good"],
|
| 118 |
+
["Explain quantum computing"],
|
| 119 |
+
["List 3 benefits of exercise"],
|
| 120 |
+
["Python fibonacci function"],
|
| 121 |
+
],
|
| 122 |
+
cache_examples=False,
|
| 123 |
+
retry_btn=None,
|
| 124 |
+
undo_btn=None,
|
| 125 |
+
clear_btn="🗑️ Clear",
|
| 126 |
+
submit_btn="🚀 Ask Seize",
|
| 127 |
)
|
| 128 |
|
| 129 |
+
# Create the full interface
|
| 130 |
+
with gr.Blocks(css="""
|
| 131 |
+
.seize-header {
|
| 132 |
+
text-align: center;
|
| 133 |
+
padding: 20px;
|
| 134 |
+
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
|
| 135 |
+
color: white;
|
| 136 |
+
border-radius: 10px;
|
| 137 |
+
margin-bottom: 20px;
|
| 138 |
+
}
|
| 139 |
+
.seize-title {
|
| 140 |
+
font-size: 2.5em;
|
| 141 |
+
font-weight: 800;
|
| 142 |
+
margin: 0;
|
| 143 |
+
letter-spacing: -0.5px;
|
| 144 |
+
}
|
| 145 |
+
.seize-subtitle {
|
| 146 |
+
font-size: 1.1em;
|
| 147 |
+
opacity: 0.9;
|
| 148 |
+
margin-top: 5px;
|
| 149 |
+
}
|
| 150 |
+
.seize-footer {
|
| 151 |
+
text-align: center;
|
| 152 |
+
padding: 15px;
|
| 153 |
+
font-size: 0.9em;
|
| 154 |
+
color: #666;
|
| 155 |
+
border-top: 1px solid #e0e0e0;
|
| 156 |
+
margin-top: 20px;
|
| 157 |
+
}
|
| 158 |
+
.gradio-container {
|
| 159 |
+
max-width: 800px !important;
|
| 160 |
+
margin: 0 auto !important;
|
| 161 |
+
}
|
| 162 |
+
""") as demo:
|
| 163 |
+
|
| 164 |
+
# Seize Header
|
| 165 |
+
gr.HTML("""
|
| 166 |
+
<div class="seize-header">
|
| 167 |
+
<h1 class="seize-title">SEIZE</h1>
|
| 168 |
+
<p class="seize-subtitle">Direct AI Assistant • Efficient Model</p>
|
| 169 |
+
<p style="font-size: 0.9em; margin-top: 10px; opacity: 0.8;">
|
| 170 |
+
Serious responses. No fluff. Optimized for stability.
|
| 171 |
+
</p>
|
| 172 |
+
</div>
|
| 173 |
+
""")
|
| 174 |
+
|
| 175 |
+
# Authentication notice
|
| 176 |
+
gr.Markdown("""
|
| 177 |
+
### ⚠️ Authentication Required
|
| 178 |
+
You need a Hugging Face token to use this model. Get one at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
|
| 179 |
+
|
| 180 |
+
**For testing without token:** Try the examples below first.
|
| 181 |
+
""")
|
| 182 |
+
|
| 183 |
+
# Render the chatbot
|
| 184 |
chatbot.render()
|
| 185 |
+
|
| 186 |
+
# Settings in accordion
|
| 187 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 188 |
+
gr.Markdown("""
|
| 189 |
+
- **Max Tokens**: Controls response length (64-512)
|
| 190 |
+
- **Temperature**: Higher = more creative, Lower = more focused
|
| 191 |
+
- **Top-p**: Controls vocabulary diversity
|
| 192 |
+
""")
|
| 193 |
+
|
| 194 |
+
# Seize Footer
|
| 195 |
+
gr.HTML("""
|
| 196 |
+
<div class="seize-footer">
|
| 197 |
+
<strong>Optimized for Hugging Face Spaces</strong> • Model: zephyr-7b-beta
|
| 198 |
+
<br>
|
| 199 |
+
<small>Response time: ~1-3s • Max tokens: 256 • Temperature: 0.3</small>
|
| 200 |
+
</div>
|
| 201 |
+
""")
|
| 202 |
+
|
| 203 |
+
# Launch configuration
|
| 204 |
if __name__ == "__main__":
|
| 205 |
+
demo.launch(
|
| 206 |
+
server_name="0.0.0.0",
|
| 207 |
+
server_port=7860,
|
| 208 |
+
show_error=True,
|
| 209 |
+
debug=False,
|
| 210 |
+
share=False # Set to True for temporary public link
|
| 211 |
+
)
|