Spaces:
Runtime error

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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -116
app.py CHANGED
@@ -1,59 +1,40 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
4
- # --- DIXARS TECHNOLOGY THEME ---
5
- dixars_theme = gr.themes.Soft(
6
- primary_hue="blue", # Shifted to Blue for professional branding
7
- secondary_hue="indigo",
8
- neutral_hue="slate",
9
- font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"],
10
- ).set(
11
- body_background_fill="*neutral_50",
12
- block_title_text_weight="600",
13
- endpoint_radius="8px",
14
- )
15
 
16
- # Custom CSS for the "Menu Bar" and Login styling
17
- custom_css = """
18
- #dixars-header {
19
- background: linear_gradient(90deg, #1e40af, #3b82f6);
20
- padding: 10px;
21
- border-radius: 8px;
22
- color: white;
23
- margin-bottom: 20px;
24
- }
25
- .menu-btn {
26
- border: none !important;
27
- background: transparent !important;
28
- color: white !important;
29
- font-weight: bold;
30
- }
31
- """
32
 
33
- def respond(
34
- message,
35
- history, # Gradio ChatInterface passes history here
36
- system_message,
37
- max_tokens,
38
- temperature,
39
- top_p,
40
- model_id
41
- ):
42
- # Using the InferenceClient without needing an explicit OAuth token
43
- # if the Space is public/using its own Secret.
44
- client = InferenceClient(model=model_id)
45
-
46
- messages = [{"role": "system", "content": system_message}]
47
-
48
- for user_msg, assistant_msg in history:
49
- if user_msg: messages.append({"role": "user", "content": user_msg})
50
- if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg})
51
-
52
- messages.append({"role": "user", "content": message})
53
 
54
- response = ""
55
  try:
56
- # Mistral-7B-Instruct-v0.3 works best with chat_completion
 
 
 
 
 
 
 
 
 
57
  for msg in client.chat_completion(
58
  messages,
59
  max_tokens=max_tokens,
@@ -61,76 +42,38 @@ def respond(
61
  temperature=temperature,
62
  top_p=top_p,
63
  ):
64
- token_text = msg.choices[0].delta.content
65
- if token_text:
66
- response += token_text
67
  yield response
68
  except Exception as e:
69
- yield f"⚠️ Dixars System Note: {str(e)}. Ensure model availability or HF_TOKEN setup."
70
 
71
- # --- UI CONSTRUCTION ---
72
- with gr.Blocks(theme=dixars_theme, title="Dixars Technology AI", css=custom_css) as dixars:
73
-
74
- # Custom Menu Bar Header
75
- with gr.Row(elem_id="dixars-header"):
76
- gr.Markdown("## 💎 DIXARS TECHNOLOGY")
77
  with gr.Row():
78
- gr.Button("Terminal", variant="secondary", size="sm", elem_classes="menu-btn")
79
- gr.Button("Cloud Storage", variant="secondary", size="sm", elem_classes="menu-btn")
80
- gr.Button("API Access", variant="secondary", size="sm", elem_classes="menu-btn")
81
- gr.LoginButton(scale=0)
82
-
83
- with gr.Tabs():
84
- with gr.Tab("💬 AI Workspace"):
85
- with gr.Row():
86
- # LEFT COLUMN: Controls
87
- with gr.Column(scale=1):
88
- gr.Markdown("### 🛠️ Controller")
89
- model_id = gr.Dropdown(
90
- choices=[
91
- "mistralai/Mistral-7B-Instruct-v0.3",
92
- "meta-llama/Meta-Llama-3-8B-Instruct",
93
- "microsoft/Phi-3-mini-4k-instruct",
94
- ],
95
- value="mistralai/Mistral-7B-Instruct-v0.3",
96
- label="Core Engine"
97
- )
98
-
99
- with gr.Accordion("⚙️ Advanced Parameters", open=False):
100
- system_message = gr.Textbox(
101
- value="You are a high-performance AI developed by Dixars Technology.",
102
- label="System Persona"
103
- )
104
- max_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max Tokens")
105
- temp = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
106
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p")
107
-
108
- gr.Markdown("---")
109
- gr.HTML("<center>🟢 DIXARS OS: ONLINE</center>")
110
-
111
- # RIGHT COLUMN: The Chat
112
- with gr.Column(scale=3):
113
- gr.ChatInterface(
114
- respond,
115
- additional_inputs=[system_message, max_tokens, temp, top_p, model_id],
116
- examples=[
117
- ["Generate a business plan for a tech startup."],
118
- ["Optimize this Python code for speed."],
119
- ["Draft a professional email for client onboarding."]
120
- ],
121
- )
122
-
123
- with gr.Tab("📊 System Analytics"):
124
- with gr.Row():
125
- gr.Label("Dixars Infrastructure: Active")
126
- gr.Label("Latency: 42ms")
127
- gr.Markdown("""
128
- ### Dixars Technology Intelligence
129
- Customized LLM interface for enterprise-grade interactions.
130
- - **Optimized:** Specifically tuned for Mistral & Llama architectures.
131
- - **Branded:** Custom CSS injection for Dixars Corporate Identity.
132
- - **Secure:** Integrated login and encrypted token handling.
133
- """)
134
 
 
135
  if __name__ == "__main__":
136
- dixars.launch()
 
 
 
 
 
1
  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,
 
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
+ )