Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,112 +1,136 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# ---
|
| 5 |
-
|
| 6 |
-
primary_hue="
|
| 7 |
-
secondary_hue="
|
| 8 |
neutral_hue="slate",
|
| 9 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def respond(
|
| 13 |
message,
|
| 14 |
-
history
|
| 15 |
system_message,
|
| 16 |
max_tokens,
|
| 17 |
temperature,
|
| 18 |
top_p,
|
| 19 |
-
model_id
|
| 20 |
-
hf_token: gr.OAuthToken | None,
|
| 21 |
):
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
client = InferenceClient(
|
| 25 |
|
| 26 |
messages = [{"role": "system", "content": system_message}]
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if
|
| 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 |
-
|
|
|
|
| 38 |
messages,
|
| 39 |
max_tokens=max_tokens,
|
| 40 |
stream=True,
|
| 41 |
temperature=temperature,
|
| 42 |
top_p=top_p,
|
| 43 |
):
|
| 44 |
-
token_text =
|
| 45 |
if token_text:
|
| 46 |
response += token_text
|
| 47 |
yield response
|
| 48 |
except Exception as e:
|
| 49 |
-
yield f"β οΈ
|
| 50 |
|
| 51 |
# --- UI CONSTRUCTION ---
|
| 52 |
-
with gr.Blocks(theme=
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
gr.LoginButton(scale=0)
|
| 56 |
|
| 57 |
with gr.Tabs():
|
| 58 |
-
with gr.Tab("π¬
|
| 59 |
with gr.Row():
|
| 60 |
# LEFT COLUMN: Controls
|
| 61 |
with gr.Column(scale=1):
|
| 62 |
-
gr.Markdown("### π οΈ
|
| 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="
|
| 72 |
)
|
| 73 |
|
| 74 |
-
with gr.Accordion("βοΈ
|
| 75 |
system_message = gr.Textbox(
|
| 76 |
-
value="You are a
|
| 77 |
label="System Persona"
|
| 78 |
)
|
| 79 |
-
max_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max
|
| 80 |
-
temp = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="
|
| 81 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="
|
| 82 |
|
| 83 |
gr.Markdown("---")
|
| 84 |
-
gr.HTML("<center>π’
|
| 85 |
|
| 86 |
# RIGHT COLUMN: The Chat
|
| 87 |
with gr.Column(scale=3):
|
| 88 |
-
|
| 89 |
respond,
|
| 90 |
additional_inputs=[system_message, max_tokens, temp, top_p, model_id],
|
| 91 |
examples=[
|
| 92 |
-
["
|
| 93 |
-
["
|
| 94 |
-
["
|
| 95 |
],
|
| 96 |
-
cache_examples=False,
|
| 97 |
)
|
| 98 |
|
| 99 |
-
with gr.Tab("π
|
| 100 |
with gr.Row():
|
| 101 |
-
gr.Label("
|
| 102 |
-
gr.Label("
|
| 103 |
gr.Markdown("""
|
| 104 |
-
###
|
| 105 |
-
|
| 106 |
-
- **
|
| 107 |
-
- **
|
| 108 |
-
- **
|
| 109 |
""")
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
-
|
|
|
|
| 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()
|