Spaces:
Runtime error

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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -59
app.py CHANGED
@@ -2,78 +2,106 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # --- DIXARS TECHNOLOGY CONFIG ---
6
- # Use a fallback model if Mistral is down
7
- MODELS = [
8
- "mistralai/Mistral-7B-Instruct-v0.3",
9
- "meta-llama/Meta-Llama-3-8B-Instruct",
10
- "HuggingFaceH4/zephyr-7b-beta"
11
- ]
12
-
13
- # Get token from secrets
14
  HF_TOKEN = os.environ.get("HF_TOKEN")
15
 
16
- # --- AUTHENTICATION ---
17
- def dixars_auth(username, password):
18
- # Credentials for Dixars Technology
19
- return username == "dixars" and password == "tech2026"
 
 
 
 
20
 
21
- # --- CHAT FUNCTION ---
22
- def respond(message, history, system_message, max_tokens, temperature, top_p, model_id):
23
- if not HF_TOKEN:
24
- yield "❌ ERROR: HF_TOKEN not found in Space Secrets. Please add it to Settings."
25
- return
26
 
27
- try:
28
- client = InferenceClient(model=model_id, token=HF_TOKEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- messages = [{"role": "system", "content": system_message}]
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
- messages.append({"role": "user", "content": message})
35
 
36
- response = ""
37
- # Using chat_completion for compatibility
38
- for msg in client.chat_completion(
39
- messages,
40
- max_tokens=max_tokens,
41
- stream=True,
42
- temperature=temperature,
43
- top_p=top_p,
44
- ):
45
  token = msg.choices[0].delta.content
46
  if token:
47
  response += token
48
  yield response
49
  except Exception as e:
50
- yield f"⚠️ DIXARS OS ERROR: {str(e)}"
51
 
52
- # --- UI DESIGN ---
53
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="Dixars Tech") as dixars:
54
- with gr.Row(variant="compact", elem_id="nav"):
55
- gr.Markdown("# πŸ’Ž DIXARS TECHNOLOGY AI")
 
 
 
 
 
56
 
57
- with gr.Tab("AI Workspace"):
58
  with gr.Row():
59
- with gr.Column(scale=1):
60
- model_choice = gr.Dropdown(choices=MODELS, value=MODELS[0], label="Select Intelligence")
61
- with gr.Accordion("Parameters", open=False):
62
- sys_msg = gr.Textbox(value="You are an assistant for Dixars Technology.", label="System Prompt")
63
- tokens = gr.Slider(1, 4096, 1024, label="Max Tokens")
64
- temp = gr.Slider(0.1, 2.0, 0.7, label="Temp")
65
- top_p = gr.Slider(0.1, 1.0, 0.9, label="Top-P")
66
-
67
- with gr.Column(scale=3):
68
- gr.ChatInterface(
69
- respond,
70
- additional_inputs=[sys_msg, tokens, temp, top_p, model_choice]
71
- )
72
 
73
- # --- LAUNCH ---
74
- if __name__ == "__main__":
75
- # Launch with the login gate
76
- dixars.launch(
77
- auth=dixars_auth,
78
- auth_message="DIXARS TECHNOLOGY SECURE ACCESS: Enter credentials."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  )
 
 
 
 
 
 
 
 
 
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()