Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,19 +29,19 @@ class ProviderManager:
|
|
| 29 |
|
| 30 |
def get_status(self):
|
| 31 |
if self.current_provider == "novita":
|
| 32 |
-
return "
|
| 33 |
else:
|
| 34 |
-
return "
|
| 35 |
|
| 36 |
def switch_to_minimax(self):
|
| 37 |
self.current_provider = "minimax"
|
| 38 |
self.novita_available = False
|
| 39 |
-
print("
|
| 40 |
|
| 41 |
def reset_to_novita(self):
|
| 42 |
self.current_provider = "novita"
|
| 43 |
self.novita_available = True
|
| 44 |
-
print("
|
| 45 |
|
| 46 |
# Global provider manager
|
| 47 |
provider = ProviderManager()
|
|
@@ -108,24 +108,24 @@ def chat_respond(message, history):
|
|
| 108 |
{"role": "user", "content": message},
|
| 109 |
{"role": "assistant", "content": response_text}
|
| 110 |
]
|
| 111 |
-
yield new_history, "
|
| 112 |
return
|
| 113 |
except Exception as e:
|
| 114 |
error_msg = str(e).lower()
|
| 115 |
if any(kw in error_msg for kw in ["rate limit", "quota", "exceeded", "insufficient", "credit", "balance", "limit", "429", "402", "payment"]):
|
| 116 |
-
print(f"
|
| 117 |
provider.switch_to_minimax()
|
| 118 |
else:
|
| 119 |
-
print(f"
|
| 120 |
|
| 121 |
# 2nd: Fallback to MiniMax
|
| 122 |
try:
|
| 123 |
if not os.environ.get("MINIMAX_API_KEY"):
|
| 124 |
new_history = history + [
|
| 125 |
{"role": "user", "content": message},
|
| 126 |
-
{"role": "assistant", "content": "
|
| 127 |
]
|
| 128 |
-
yield new_history, "
|
| 129 |
return
|
| 130 |
|
| 131 |
for chunk in chat_with_minimax(api_messages):
|
|
@@ -134,7 +134,7 @@ def chat_respond(message, history):
|
|
| 134 |
{"role": "user", "content": message},
|
| 135 |
{"role": "assistant", "content": response_text}
|
| 136 |
]
|
| 137 |
-
yield new_history, "
|
| 138 |
return
|
| 139 |
|
| 140 |
except Exception as e:
|
|
@@ -142,21 +142,21 @@ def chat_respond(message, history):
|
|
| 142 |
provider.reset_to_novita()
|
| 143 |
new_history = history + [
|
| 144 |
{"role": "user", "content": message},
|
| 145 |
-
{"role": "assistant", "content": f"
|
| 146 |
]
|
| 147 |
-
yield new_history, "
|
| 148 |
|
| 149 |
def reset_provider_fn():
|
| 150 |
"""Manually reset to Novita"""
|
| 151 |
provider.reset_to_novita()
|
| 152 |
-
return "
|
| 153 |
|
| 154 |
def clear_chat_fn():
|
| 155 |
"""Clear chat history"""
|
| 156 |
return [], "", provider.get_status()
|
| 157 |
|
| 158 |
# ============================================
|
| 159 |
-
#
|
| 160 |
# ============================================
|
| 161 |
css = """
|
| 162 |
@import url('https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@400;700&display=swap');
|
|
@@ -286,7 +286,7 @@ pre, code {
|
|
| 286 |
"""
|
| 287 |
|
| 288 |
# ============================================
|
| 289 |
-
# Gradio Interface - Full Width Layout (English)
|
| 290 |
# ============================================
|
| 291 |
with gr.Blocks(fill_height=True) as demo:
|
| 292 |
|
|
@@ -297,15 +297,14 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 297 |
gr.HTML("""
|
| 298 |
<div style="text-align: center; margin: 20px 0 10px 0;">
|
| 299 |
<a href="https://www.humangen.ai" target="_blank" style="text-decoration: none;">
|
| 300 |
-
<img src="https://img.shields.io/static/v1?label
|
| 301 |
</a>
|
| 302 |
-
|
| 303 |
</div>
|
| 304 |
""")
|
| 305 |
|
| 306 |
# Header Title
|
| 307 |
-
gr.Markdown("""#
|
| 308 |
-
gr.Markdown("""<p class="subtitle"
|
| 309 |
|
| 310 |
# Model Info Box
|
| 311 |
gr.HTML("""
|
|
@@ -322,17 +321,17 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 322 |
# Provider Status Row
|
| 323 |
with gr.Row():
|
| 324 |
provider_status = gr.Textbox(
|
| 325 |
-
value="
|
| 326 |
-
label="
|
| 327 |
interactive=False,
|
| 328 |
elem_classes="status-box",
|
| 329 |
scale=4
|
| 330 |
)
|
| 331 |
-
reset_btn = gr.Button("
|
| 332 |
|
| 333 |
# Chatbot - Full Width
|
| 334 |
chatbot = gr.Chatbot(
|
| 335 |
-
label="
|
| 336 |
height=500,
|
| 337 |
show_label=False,
|
| 338 |
elem_classes="chatbot"
|
|
@@ -346,10 +345,10 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 346 |
scale=8,
|
| 347 |
container=False
|
| 348 |
)
|
| 349 |
-
submit_btn = gr.Button("
|
| 350 |
|
| 351 |
# Clear Button
|
| 352 |
-
clear_btn = gr.Button("
|
| 353 |
|
| 354 |
# Event Handlers
|
| 355 |
def respond(message, history):
|
|
|
|
| 29 |
|
| 30 |
def get_status(self):
|
| 31 |
if self.current_provider == "novita":
|
| 32 |
+
return "[Active] Novita (HuggingFace)"
|
| 33 |
else:
|
| 34 |
+
return "[Active] MiniMax Official API"
|
| 35 |
|
| 36 |
def switch_to_minimax(self):
|
| 37 |
self.current_provider = "minimax"
|
| 38 |
self.novita_available = False
|
| 39 |
+
print("Switched to MiniMax Official API")
|
| 40 |
|
| 41 |
def reset_to_novita(self):
|
| 42 |
self.current_provider = "novita"
|
| 43 |
self.novita_available = True
|
| 44 |
+
print("Reset to Novita provider")
|
| 45 |
|
| 46 |
# Global provider manager
|
| 47 |
provider = ProviderManager()
|
|
|
|
| 108 |
{"role": "user", "content": message},
|
| 109 |
{"role": "assistant", "content": response_text}
|
| 110 |
]
|
| 111 |
+
yield new_history, "[Active] Novita (HuggingFace)"
|
| 112 |
return
|
| 113 |
except Exception as e:
|
| 114 |
error_msg = str(e).lower()
|
| 115 |
if any(kw in error_msg for kw in ["rate limit", "quota", "exceeded", "insufficient", "credit", "balance", "limit", "429", "402", "payment"]):
|
| 116 |
+
print(f"Novita error: {e}")
|
| 117 |
provider.switch_to_minimax()
|
| 118 |
else:
|
| 119 |
+
print(f"Novita error: {e}")
|
| 120 |
|
| 121 |
# 2nd: Fallback to MiniMax
|
| 122 |
try:
|
| 123 |
if not os.environ.get("MINIMAX_API_KEY"):
|
| 124 |
new_history = history + [
|
| 125 |
{"role": "user", "content": message},
|
| 126 |
+
{"role": "assistant", "content": "Error: MINIMAX_API_KEY not configured."}
|
| 127 |
]
|
| 128 |
+
yield new_history, "[Error] No API Key"
|
| 129 |
return
|
| 130 |
|
| 131 |
for chunk in chat_with_minimax(api_messages):
|
|
|
|
| 134 |
{"role": "user", "content": message},
|
| 135 |
{"role": "assistant", "content": response_text}
|
| 136 |
]
|
| 137 |
+
yield new_history, "[Active] MiniMax Official API"
|
| 138 |
return
|
| 139 |
|
| 140 |
except Exception as e:
|
|
|
|
| 142 |
provider.reset_to_novita()
|
| 143 |
new_history = history + [
|
| 144 |
{"role": "user", "content": message},
|
| 145 |
+
{"role": "assistant", "content": f"Error: {str(e)}"}
|
| 146 |
]
|
| 147 |
+
yield new_history, "[Error]"
|
| 148 |
|
| 149 |
def reset_provider_fn():
|
| 150 |
"""Manually reset to Novita"""
|
| 151 |
provider.reset_to_novita()
|
| 152 |
+
return "Reset to Novita!"
|
| 153 |
|
| 154 |
def clear_chat_fn():
|
| 155 |
"""Clear chat history"""
|
| 156 |
return [], "", provider.get_status()
|
| 157 |
|
| 158 |
# ============================================
|
| 159 |
+
# Comic Classic Theme CSS
|
| 160 |
# ============================================
|
| 161 |
css = """
|
| 162 |
@import url('https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@400;700&display=swap');
|
|
|
|
| 286 |
"""
|
| 287 |
|
| 288 |
# ============================================
|
| 289 |
+
# Gradio Interface - Full Width Layout (English, No Emoji)
|
| 290 |
# ============================================
|
| 291 |
with gr.Blocks(fill_height=True) as demo:
|
| 292 |
|
|
|
|
| 297 |
gr.HTML("""
|
| 298 |
<div style="text-align: center; margin: 20px 0 10px 0;">
|
| 299 |
<a href="https://www.humangen.ai" target="_blank" style="text-decoration: none;">
|
| 300 |
+
<img src="https://img.shields.io/static/v1?label=HOME&message=HUMANGEN.AI&color=0000ff&labelColor=ffcc00&style=for-the-badge" alt="HOME">
|
| 301 |
</a>
|
|
|
|
| 302 |
</div>
|
| 303 |
""")
|
| 304 |
|
| 305 |
# Header Title
|
| 306 |
+
gr.Markdown("""# MINIMAX-M2.1 CHAT""", elem_classes="header-text")
|
| 307 |
+
gr.Markdown("""<p class="subtitle">Claude Sonnet 4.5-level Coding & Agent Performance! 230B Parameter Open Source Model</p>""")
|
| 308 |
|
| 309 |
# Model Info Box
|
| 310 |
gr.HTML("""
|
|
|
|
| 321 |
# Provider Status Row
|
| 322 |
with gr.Row():
|
| 323 |
provider_status = gr.Textbox(
|
| 324 |
+
value="[Active] Novita (HuggingFace)",
|
| 325 |
+
label="Current Provider",
|
| 326 |
interactive=False,
|
| 327 |
elem_classes="status-box",
|
| 328 |
scale=4
|
| 329 |
)
|
| 330 |
+
reset_btn = gr.Button("Reset Provider", variant="secondary", scale=1)
|
| 331 |
|
| 332 |
# Chatbot - Full Width
|
| 333 |
chatbot = gr.Chatbot(
|
| 334 |
+
label="Chat",
|
| 335 |
height=500,
|
| 336 |
show_label=False,
|
| 337 |
elem_classes="chatbot"
|
|
|
|
| 345 |
scale=8,
|
| 346 |
container=False
|
| 347 |
)
|
| 348 |
+
submit_btn = gr.Button("SEND", variant="primary", scale=2)
|
| 349 |
|
| 350 |
# Clear Button
|
| 351 |
+
clear_btn = gr.Button("CLEAR CHAT", variant="secondary")
|
| 352 |
|
| 353 |
# Event Handlers
|
| 354 |
def respond(message, history):
|