Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,112 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
-
message,
|
| 7 |
-
history: list
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
|
|
|
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|
|
|
|
| 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()
|