Spaces:
Runtime error

Dominic2000 commited on
Commit
f38c903
Β·
verified Β·
1 Parent(s): 008d1e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -44
app.py CHANGED
@@ -1,112 +1,136 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # --- NEXT-GEN THEME CONFIG ---
5
- theme = gr.themes.Soft(
6
- primary_hue="cyan",
7
- secondary_hue="blue",
8
  neutral_hue="slate",
9
  font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"],
 
 
 
 
10
  )
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def respond(
13
  message,
14
- history: list,
15
  system_message,
16
  max_tokens,
17
  temperature,
18
  top_p,
19
- model_id,
20
- hf_token: gr.OAuthToken | None,
21
  ):
22
- # Safety check for token
23
- token = hf_token.token if hf_token else None
24
- client = InferenceClient(token=token, model=model_id)
25
 
26
  messages = [{"role": "system", "content": system_message}]
27
 
28
- # Format history for the API
29
- for val in history:
30
- if val[0]: messages.append({"role": "user", "content": val[0]})
31
- if val[1]: messages.append({"role": "assistant", "content": val[1]})
32
 
33
  messages.append({"role": "user", "content": message})
34
 
35
  response = ""
36
  try:
37
- for message in client.chat_completion(
 
38
  messages,
39
  max_tokens=max_tokens,
40
  stream=True,
41
  temperature=temperature,
42
  top_p=top_p,
43
  ):
44
- token_text = message.choices[0].delta.content
45
  if token_text:
46
  response += token_text
47
  yield response
48
  except Exception as e:
49
- yield f"⚠️ Error: {str(e)}. Please ensure you are logged in or the model is available."
50
 
51
  # --- UI CONSTRUCTION ---
52
- with gr.Blocks(theme=theme, title="Nexus AI Terminal") as demo:
53
- with gr.Row():
54
- gr.Markdown("# 🌌 Nexus AI Terminal")
 
 
 
 
 
 
55
  gr.LoginButton(scale=0)
56
 
57
  with gr.Tabs():
58
- with gr.Tab("πŸ’¬ Chat Interface"):
59
  with gr.Row():
60
  # LEFT COLUMN: Controls
61
  with gr.Column(scale=1):
62
- gr.Markdown("### πŸ› οΈ Model Configuration")
63
  model_id = gr.Dropdown(
64
  choices=[
65
  "mistralai/Mistral-7B-Instruct-v0.3",
66
  "meta-llama/Meta-Llama-3-8B-Instruct",
67
  "microsoft/Phi-3-mini-4k-instruct",
68
- "openai/gpt-oss-20b"
69
  ],
70
  value="mistralai/Mistral-7B-Instruct-v0.3",
71
- label="Brain Module (Model)"
72
  )
73
 
74
- with gr.Accordion("βš™οΈ Brain Parameters", open=False):
75
  system_message = gr.Textbox(
76
- value="You are a helpful, high-performance AI assistant.",
77
  label="System Persona"
78
  )
79
- max_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max Response length")
80
- temp = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Creativity (Temperature)")
81
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Nucleus Sampling")
82
 
83
  gr.Markdown("---")
84
- gr.HTML("<center>🟒 System Status: Ready</center>")
85
 
86
  # RIGHT COLUMN: The Chat
87
  with gr.Column(scale=3):
88
- chat_interface = gr.ChatInterface(
89
  respond,
90
  additional_inputs=[system_message, max_tokens, temp, top_p, model_id],
91
  examples=[
92
- ["Explain quantum entanglement in simple terms."],
93
- ["Write a Python script to scrape a website."],
94
- ["Give me a 3-day travel itinerary for Tokyo."]
95
  ],
96
- cache_examples=False,
97
  )
98
 
99
- with gr.Tab("πŸ“Š Analytics & Info"):
100
  with gr.Row():
101
- gr.Label("Current Session Active")
102
- gr.Label("Hugging Face API Tier: Free")
103
  gr.Markdown("""
104
- ### About Nexus AI
105
- This is a next-generation Gradio interface designed for seamless interaction with Large Language Models.
106
- - **Secure:** Uses OAuth for safe Hugging Face Hub access.
107
- - **Adaptive:** Switch models on the fly without refreshing.
108
- - **Modern:** Built with a responsive 'Soft' UI design.
109
  """)
110
 
111
  if __name__ == "__main__":
112
- demo.launch()
 
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,
60
  stream=True,
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()