Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,106 +2,90 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
# --- THEME
|
| 10 |
-
dixars_theme = gr.themes.Soft(
|
| 11 |
-
primary_hue="blue",
|
| 12 |
-
secondary_hue="cyan",
|
| 13 |
-
).set(
|
| 14 |
-
block_title_text_weight="700",
|
| 15 |
-
endpoint_radius="12px",
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
custom_css = """
|
| 19 |
-
#login-container { margin-top: 10%; text-align: center; }
|
| 20 |
-
#dixars-header { background: linear-gradient(90deg, #1e3a8a, #3b82f6); padding: 15px; border-radius: 12px; color: white; }
|
| 21 |
-
.guest-btn { background-color: #64748b !important; color: white !important; }
|
| 22 |
-
"""
|
| 23 |
|
| 24 |
# --- LOGIC ---
|
| 25 |
def handle_login(user, pwd):
|
| 26 |
if user == "admin" and pwd == "dixars2026":
|
| 27 |
-
return gr.update(visible=False), gr.update(visible=True)
|
| 28 |
-
return gr.update(visible=True), gr.update(visible=False)
|
| 29 |
|
| 30 |
def skip_login():
|
| 31 |
-
return gr.update(visible=False), gr.update(visible=True)
|
| 32 |
|
| 33 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
for user_msg, assistant_msg in history:
|
| 38 |
-
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 39 |
-
if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg})
|
| 40 |
-
|
| 41 |
-
messages.append({"role": "user", "content": message})
|
| 42 |
|
| 43 |
-
response = ""
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
token = msg.choices[0].delta.content
|
| 47 |
if token:
|
| 48 |
response += token
|
| 49 |
yield response
|
| 50 |
except Exception as e:
|
| 51 |
-
yield f"β οΈ DIXARS
|
| 52 |
|
| 53 |
-
# --- UI
|
| 54 |
-
with gr.Blocks(theme=dixars_theme,
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
with gr.Column(
|
| 58 |
-
gr.Markdown("# π DIXARS TECHNOLOGY\n###
|
| 59 |
-
user_input = gr.Textbox(label="Username"
|
| 60 |
-
pass_input = gr.Textbox(label="Password", type="password")
|
| 61 |
-
status_msg = gr.Markdown("")
|
| 62 |
-
|
| 63 |
with gr.Row():
|
| 64 |
-
login_btn = gr.Button("
|
| 65 |
-
skip_btn = gr.Button("Skip
|
| 66 |
|
| 67 |
-
#
|
| 68 |
with gr.Column(visible=False) as main_app:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
gr.Markdown("π’ System Status: Active")
|
| 72 |
-
|
| 73 |
with gr.Tabs():
|
| 74 |
-
with gr.Tab("π¬ AI
|
|
|
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column(scale=1):
|
| 77 |
-
gr.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
temp = gr.Slider(0.1, 2.0, 0.7, label="Temp")
|
| 82 |
-
top_p = gr.Slider(0.1, 1.0, 0.9, label="Top-p")
|
| 83 |
|
|
|
|
| 84 |
with gr.Column(scale=3):
|
| 85 |
gr.ChatInterface(
|
| 86 |
respond,
|
| 87 |
additional_inputs=[sys_msg, tokens, temp, top_p],
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# --- INTERACTION LOGIC ---
|
| 95 |
-
login_btn.click(
|
| 96 |
-
handle_login,
|
| 97 |
-
inputs=[user_input, pass_input],
|
| 98 |
-
outputs=[login_screen, main_app, status_msg]
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
skip_btn.click(
|
| 102 |
-
skip_login,
|
| 103 |
-
outputs=[login_screen, main_app, status_msg]
|
| 104 |
-
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
dixars.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# --- DIXARS TECHNOLOGY CONFIG ---
|
| 6 |
MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 7 |
+
# Safety check for the token to prevent startup crashes
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 9 |
|
| 10 |
+
# --- THEME ---
|
| 11 |
+
dixars_theme = gr.themes.Soft(primary_hue="blue", secondary_hue="slate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# --- LOGIC ---
|
| 14 |
def handle_login(user, pwd):
|
| 15 |
if user == "admin" and pwd == "dixars2026":
|
| 16 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 17 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 18 |
|
| 19 |
def skip_login():
|
| 20 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 21 |
|
| 22 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 23 |
+
if not HF_TOKEN:
|
| 24 |
+
yield "β SYSTEM ERROR: HF_TOKEN is missing. Please add it to Space Secrets."
|
| 25 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
try:
|
| 28 |
+
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 29 |
+
messages = [{"role": "system", "content": system_message}]
|
| 30 |
+
|
| 31 |
+
for user_msg, assistant_msg in history:
|
| 32 |
+
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 33 |
+
if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg})
|
| 34 |
+
|
| 35 |
+
messages.append({"role": "user", "content": message})
|
| 36 |
+
|
| 37 |
+
response = ""
|
| 38 |
+
# Updated for high compatibility with Mistral v0.3
|
| 39 |
+
for msg in client.chat_completion(
|
| 40 |
+
messages,
|
| 41 |
+
max_tokens=max_tokens,
|
| 42 |
+
stream=True,
|
| 43 |
+
temperature=temperature,
|
| 44 |
+
top_p=top_p
|
| 45 |
+
):
|
| 46 |
token = msg.choices[0].delta.content
|
| 47 |
if token:
|
| 48 |
response += token
|
| 49 |
yield response
|
| 50 |
except Exception as e:
|
| 51 |
+
yield f"β οΈ DIXARS OS RUNTIME ERROR: {str(e)}"
|
| 52 |
|
| 53 |
+
# --- UI ---
|
| 54 |
+
with gr.Blocks(theme=dixars_theme, title="Dixars Tech Intelligence") as dixars:
|
| 55 |
|
| 56 |
+
# LOGIN SCREEN
|
| 57 |
+
with gr.Column(visible=True) as login_screen:
|
| 58 |
+
gr.Markdown("# π DIXARS TECHNOLOGY\n### Enterprise AI Terminal")
|
| 59 |
+
user_input = gr.Textbox(label="Admin Username")
|
| 60 |
+
pass_input = gr.Textbox(label="Admin Password", type="password")
|
|
|
|
|
|
|
| 61 |
with gr.Row():
|
| 62 |
+
login_btn = gr.Button("Secure Login", variant="primary")
|
| 63 |
+
skip_btn = gr.Button("Skip to Guest Access")
|
| 64 |
|
| 65 |
+
# MAIN APP
|
| 66 |
with gr.Column(visible=False) as main_app:
|
| 67 |
+
gr.Markdown("## π DIXARS TECHNOLOGY OS v1.0")
|
| 68 |
+
|
|
|
|
|
|
|
| 69 |
with gr.Tabs():
|
| 70 |
+
with gr.Tab("π¬ AI Workspace"):
|
| 71 |
+
# Side parameters
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column(scale=1):
|
| 74 |
+
sys_msg = gr.Textbox(value="You are a Dixars Tech AI.", label="System Persona")
|
| 75 |
+
tokens = gr.Slider(1, 4096, 1024, label="Max Tokens")
|
| 76 |
+
temp = gr.Slider(0.1, 2.0, 0.7, label="Temperature")
|
| 77 |
+
top_p = gr.Slider(0.1, 1.0, 0.9, label="Top-p")
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Chat Interface
|
| 80 |
with gr.Column(scale=3):
|
| 81 |
gr.ChatInterface(
|
| 82 |
respond,
|
| 83 |
additional_inputs=[sys_msg, tokens, temp, top_p],
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# BUTTON ACTIONS
|
| 87 |
+
login_btn.click(handle_login, [user_input, pass_input], [login_screen, main_app])
|
| 88 |
+
skip_btn.click(skip_login, None, [login_screen, main_app])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
dixars.launch()
|