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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# ============================================
|
| 6 |
+
# GLM-4.7 Streaming Chat
|
| 7 |
+
# Provider: Novita (HuggingFace)
|
| 8 |
+
# Gradio 6.0 Compatible - Full Width Layout
|
| 9 |
+
# ============================================
|
| 10 |
+
|
| 11 |
+
# Initialize Novita Client
|
| 12 |
+
client = InferenceClient(
|
| 13 |
+
provider="novita",
|
| 14 |
+
api_key=os.environ.get("HF_TOKEN"),
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def chat_stream(messages):
|
| 18 |
+
"""Streaming via Novita provider"""
|
| 19 |
+
stream = client.chat.completions.create(
|
| 20 |
+
model="zai-org/GLM-4.7",
|
| 21 |
+
messages=messages,
|
| 22 |
+
max_tokens=4096,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
top_p=0.95,
|
| 25 |
+
stream=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
for chunk in stream:
|
| 29 |
+
if chunk.choices[0].delta.content:
|
| 30 |
+
yield chunk.choices[0].delta.content
|
| 31 |
+
|
| 32 |
+
def chat_respond(message, history):
|
| 33 |
+
"""
|
| 34 |
+
Streaming chat
|
| 35 |
+
Gradio 6.0 messages format: [{"role": "user/assistant", "content": "..."}]
|
| 36 |
+
"""
|
| 37 |
+
if not message.strip():
|
| 38 |
+
return history
|
| 39 |
+
|
| 40 |
+
# Build API messages
|
| 41 |
+
api_messages = [{
|
| 42 |
+
"role": "system",
|
| 43 |
+
"content": "You are GLM-4.7, a helpful AI assistant built by Zhipu AI. You excel at coding, reasoning, and creative tasks. Respond in the same language as the user."
|
| 44 |
+
}]
|
| 45 |
+
|
| 46 |
+
# Add history (Gradio 6.0 messages format)
|
| 47 |
+
for h in history:
|
| 48 |
+
if isinstance(h, dict):
|
| 49 |
+
api_messages.append({"role": h.get("role", "user"), "content": h.get("content", "")})
|
| 50 |
+
|
| 51 |
+
api_messages.append({"role": "user", "content": message})
|
| 52 |
+
|
| 53 |
+
response_text = ""
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
for chunk in chat_stream(api_messages):
|
| 57 |
+
response_text += chunk
|
| 58 |
+
new_history = history + [
|
| 59 |
+
{"role": "user", "content": message},
|
| 60 |
+
{"role": "assistant", "content": response_text}
|
| 61 |
+
]
|
| 62 |
+
yield new_history
|
| 63 |
+
except Exception as e:
|
| 64 |
+
new_history = history + [
|
| 65 |
+
{"role": "user", "content": message},
|
| 66 |
+
{"role": "assistant", "content": f"Error: {str(e)}"}
|
| 67 |
+
]
|
| 68 |
+
yield new_history
|
| 69 |
+
|
| 70 |
+
def clear_chat_fn():
|
| 71 |
+
"""Clear chat history"""
|
| 72 |
+
return [], ""
|
| 73 |
+
|
| 74 |
+
# ============================================
|
| 75 |
+
# Comic Classic Theme CSS
|
| 76 |
+
# ============================================
|
| 77 |
+
css = """
|
| 78 |
+
@import url('https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@400;700&display=swap');
|
| 79 |
+
|
| 80 |
+
.gradio-container {
|
| 81 |
+
background-color: #FEF9C3 !important;
|
| 82 |
+
background-image: radial-gradient(#1F2937 1px, transparent 1px) !important;
|
| 83 |
+
background-size: 20px 20px !important;
|
| 84 |
+
min-height: 100vh !important;
|
| 85 |
+
font-family: 'Comic Neue', cursive, sans-serif !important;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
.huggingface-space-header, #space-header, .space-header, [class*="space-header"] {
|
| 89 |
+
display: none !important;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
footer, .footer, .gradio-footer, .built-with-gradio {
|
| 93 |
+
display: none !important;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
.header-text h1 {
|
| 97 |
+
font-family: 'Bangers', cursive !important;
|
| 98 |
+
color: #1F2937 !important;
|
| 99 |
+
font-size: 3.5rem !important;
|
| 100 |
+
text-align: center !important;
|
| 101 |
+
text-shadow: 4px 4px 0px #FACC15, 6px 6px 0px #1F2937 !important;
|
| 102 |
+
letter-spacing: 3px !important;
|
| 103 |
+
-webkit-text-stroke: 2px #1F2937 !important;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.subtitle {
|
| 107 |
+
text-align: center !important;
|
| 108 |
+
font-family: 'Comic Neue', cursive !important;
|
| 109 |
+
font-size: 1.2rem !important;
|
| 110 |
+
color: #1F2937 !important;
|
| 111 |
+
font-weight: 700 !important;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
.gr-panel, .gr-box, .block, .gr-group {
|
| 115 |
+
background: #FFFFFF !important;
|
| 116 |
+
border: 3px solid #1F2937 !important;
|
| 117 |
+
border-radius: 8px !important;
|
| 118 |
+
box-shadow: 6px 6px 0px #1F2937 !important;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
textarea, input[type="text"] {
|
| 122 |
+
background: #FFFFFF !important;
|
| 123 |
+
border: 3px solid #1F2937 !important;
|
| 124 |
+
border-radius: 8px !important;
|
| 125 |
+
color: #1F2937 !important;
|
| 126 |
+
font-family: 'Comic Neue', cursive !important;
|
| 127 |
+
font-size: 1rem !important;
|
| 128 |
+
font-weight: 700 !important;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
textarea:focus, input[type="text"]:focus {
|
| 132 |
+
border-color: #3B82F6 !important;
|
| 133 |
+
box-shadow: 4px 4px 0px #3B82F6 !important;
|
| 134 |
+
outline: none !important;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.gr-button-primary, button.primary {
|
| 138 |
+
background: #3B82F6 !important;
|
| 139 |
+
border: 3px solid #1F2937 !important;
|
| 140 |
+
border-radius: 8px !important;
|
| 141 |
+
color: #FFFFFF !important;
|
| 142 |
+
font-family: 'Bangers', cursive !important;
|
| 143 |
+
font-size: 1.3rem !important;
|
| 144 |
+
letter-spacing: 2px !important;
|
| 145 |
+
padding: 14px 28px !important;
|
| 146 |
+
box-shadow: 5px 5px 0px #1F2937 !important;
|
| 147 |
+
text-shadow: 1px 1px 0px #1F2937 !important;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
.gr-button-primary:hover, button.primary:hover {
|
| 151 |
+
background: #2563EB !important;
|
| 152 |
+
transform: translate(-2px, -2px) !important;
|
| 153 |
+
box-shadow: 7px 7px 0px #1F2937 !important;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.gr-button-secondary, button.secondary {
|
| 157 |
+
background: #EF4444 !important;
|
| 158 |
+
border: 3px solid #1F2937 !important;
|
| 159 |
+
border-radius: 8px !important;
|
| 160 |
+
color: #FFFFFF !important;
|
| 161 |
+
font-family: 'Bangers', cursive !important;
|
| 162 |
+
font-size: 1.1rem !important;
|
| 163 |
+
box-shadow: 4px 4px 0px #1F2937 !important;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.gr-button-secondary:hover, button.secondary:hover {
|
| 167 |
+
background: #DC2626 !important;
|
| 168 |
+
transform: translate(-2px, -2px) !important;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
.chatbot {
|
| 172 |
+
border: 3px solid #1F2937 !important;
|
| 173 |
+
border-radius: 12px !important;
|
| 174 |
+
box-shadow: 6px 6px 0px #1F2937 !important;
|
| 175 |
+
background: #FFFFFF !important;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
pre, code {
|
| 179 |
+
background: #1F2937 !important;
|
| 180 |
+
color: #10B981 !important;
|
| 181 |
+
border: 2px solid #10B981 !important;
|
| 182 |
+
border-radius: 6px !important;
|
| 183 |
+
font-family: 'Courier New', monospace !important;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
::-webkit-scrollbar { width: 12px; }
|
| 187 |
+
::-webkit-scrollbar-track { background: #FEF9C3; border: 2px solid #1F2937; }
|
| 188 |
+
::-webkit-scrollbar-thumb { background: #3B82F6; border: 2px solid #1F2937; }
|
| 189 |
+
::-webkit-scrollbar-thumb:hover { background: #EF4444; }
|
| 190 |
+
|
| 191 |
+
@media (max-width: 768px) {
|
| 192 |
+
.header-text h1 { font-size: 2.2rem !important; }
|
| 193 |
+
}
|
| 194 |
+
"""
|
| 195 |
+
|
| 196 |
+
# ============================================
|
| 197 |
+
# Gradio Interface - Full Width Layout
|
| 198 |
+
# ============================================
|
| 199 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 200 |
+
|
| 201 |
+
# Inject CSS via HTML
|
| 202 |
+
gr.HTML(f"<style>{css}</style>")
|
| 203 |
+
|
| 204 |
+
# HOME Badge
|
| 205 |
+
gr.HTML("""
|
| 206 |
+
<div style="text-align: center; margin: 20px 0 10px 0;">
|
| 207 |
+
<a href="https://www.humangen.ai" target="_blank" style="text-decoration: none;">
|
| 208 |
+
<img src="https://img.shields.io/static/v1?label=HOME&message=HUMANGEN.AI&color=0000ff&labelColor=ffcc00&style=for-the-badge" alt="HOME">
|
| 209 |
+
</a>
|
| 210 |
+
</div>
|
| 211 |
+
""")
|
| 212 |
+
|
| 213 |
+
# Header Title
|
| 214 |
+
gr.Markdown("""# GLM-4.7 CHAT""", elem_classes="header-text")
|
| 215 |
+
gr.Markdown("""<p class="subtitle">Zhipu AI's Latest Open Source Model - Powerful Reasoning & Coding Capabilities</p>""")
|
| 216 |
+
|
| 217 |
+
# Model Info Box
|
| 218 |
+
gr.HTML("""
|
| 219 |
+
<div style="background: linear-gradient(135deg, #10B981 0%, #3B82F6 100%); border: 3px solid #1F2937; border-radius: 12px; padding: 15px; color: white; box-shadow: 5px 5px 0px #1F2937; margin: 0 auto 20px auto; max-width: 1200px;">
|
| 220 |
+
<div style="display: flex; justify-content: space-around; flex-wrap: wrap; text-align: center;">
|
| 221 |
+
<div><strong style="font-size: 1.5rem;">GLM-4.7</strong><br><span style="font-size: 0.9rem;">Model</span></div>
|
| 222 |
+
<div><strong style="font-size: 1.5rem;">Zhipu AI</strong><br><span style="font-size: 0.9rem;">Developer</span></div>
|
| 223 |
+
<div><strong style="font-size: 1.5rem;">Open Source</strong><br><span style="font-size: 0.9rem;">License</span></div>
|
| 224 |
+
<div><strong style="font-size: 1.5rem;">Novita</strong><br><span style="font-size: 0.9rem;">Provider</span></div>
|
| 225 |
+
</div>
|
| 226 |
+
</div>
|
| 227 |
+
""")
|
| 228 |
+
|
| 229 |
+
# Chatbot - Full Width
|
| 230 |
+
chatbot = gr.Chatbot(
|
| 231 |
+
label="Chat",
|
| 232 |
+
height=500,
|
| 233 |
+
show_label=False,
|
| 234 |
+
elem_classes="chatbot"
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
# Input Row
|
| 238 |
+
with gr.Row():
|
| 239 |
+
msg = gr.Textbox(
|
| 240 |
+
label="",
|
| 241 |
+
placeholder="Enter your message... (coding, analysis, creative writing, anything!)",
|
| 242 |
+
scale=8,
|
| 243 |
+
container=False
|
| 244 |
+
)
|
| 245 |
+
submit_btn = gr.Button("SEND", variant="primary", scale=2)
|
| 246 |
+
|
| 247 |
+
# Clear Button
|
| 248 |
+
clear_btn = gr.Button("CLEAR CHAT", variant="secondary")
|
| 249 |
+
|
| 250 |
+
# Event Handlers
|
| 251 |
+
def respond(message, history):
|
| 252 |
+
if not message.strip():
|
| 253 |
+
yield history
|
| 254 |
+
return
|
| 255 |
+
for result in chat_respond(message, history):
|
| 256 |
+
yield result
|
| 257 |
+
|
| 258 |
+
# Connect events
|
| 259 |
+
msg.submit(respond, [msg, chatbot], [chatbot]).then(
|
| 260 |
+
lambda: "", outputs=[msg]
|
| 261 |
+
)
|
| 262 |
+
submit_btn.click(respond, [msg, chatbot], [chatbot]).then(
|
| 263 |
+
lambda: "", outputs=[msg]
|
| 264 |
+
)
|
| 265 |
+
clear_btn.click(clear_chat_fn, outputs=[chatbot, msg])
|
| 266 |
+
|
| 267 |
+
if __name__ == "__main__":
|
| 268 |
+
demo.launch(ssr_mode=False)
|