Spaces:
Runtime error

Dominic2000 commited on
Commit
6f27c6b
Β·
verified Β·
1 Parent(s): a21a801

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -68
app.py CHANGED
@@ -2,106 +2,90 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # --- CONFIGURATION ---
6
  MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
7
- HF_TOKEN = os.environ.get("HF_TOKEN")
 
8
 
9
- # --- THEME & STYLING ---
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), "Admin Access Granted"
28
- return gr.update(visible=True), gr.update(visible=False), "Invalid Credentials"
29
 
30
  def skip_login():
31
- return gr.update(visible=False), gr.update(visible=True), "Guest Access"
32
 
33
  def respond(message, history, system_message, max_tokens, temperature, top_p):
34
- client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
35
- messages = [{"role": "system", "content": system_message}]
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
- for msg in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Error: {str(e)}"
52
 
53
- # --- UI CONSTRUCTION ---
54
- with gr.Blocks(theme=dixars_theme, css=custom_css, title="Dixars Tech Intelligence") as dixars:
55
 
56
- # 1. GATEKEEPER UI (Login/Skip Screen)
57
- with gr.Column(elem_id="login-container", visible=True) as login_screen:
58
- gr.Markdown("# πŸ’Ž DIXARS TECHNOLOGY\n### Secure AI Terminal")
59
- user_input = gr.Textbox(label="Username", placeholder="Enter admin 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("Login as Admin", variant="primary")
65
- skip_btn = gr.Button("Skip & Enter as Guest", elem_classes="guest-btn")
66
 
67
- # 2. MAIN APPLICATION UI (Hidden by default)
68
  with gr.Column(visible=False) as main_app:
69
- with gr.Row(elem_id="dixars-header"):
70
- gr.Markdown("## πŸ’Ž DIXARS TECHNOLOGY | OS v1.0")
71
- gr.Markdown("🟒 System Status: Active")
72
-
73
  with gr.Tabs():
74
- with gr.Tab("πŸ’¬ AI Terminal"):
 
75
  with gr.Row():
76
  with gr.Column(scale=1):
77
- gr.Markdown("### πŸ› οΈ Controller")
78
- with gr.Accordion("Parameters", open=False):
79
- sys_msg = gr.Textbox(value="You are a Dixars Tech AI.", label="System Persona")
80
- tokens = gr.Slider(1, 4096, 1024, label="Max Tokens")
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
- with gr.Tab("πŸ“Š Analytics"):
91
- gr.Label("Guest Session" if not HF_TOKEN else "Authorized Session")
92
- gr.Markdown("Dixars Tech Analytics Dashboard - v1.0")
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()