Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,12 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
"meta-llama/Llama-3.1-8B-Instruct",
|
| 8 |
-
"openai/gpt-oss-20b",
|
| 9 |
-
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 10 |
-
]
|
| 11 |
|
| 12 |
-
DEFAULT_MODEL = AVAILABLE_MODELS[0]
|
| 13 |
-
DEFAULT_SYSTEM_MESSAGE = "You are a helpful, concise AI assistant."
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"""Use Gradio OAuth token first, then fall back to HF_TOKEN for local runs."""
|
| 18 |
if hf_token is not None and getattr(hf_token, "token", None):
|
| 19 |
return hf_token.token
|
| 20 |
env_token = os.getenv("HF_TOKEN", "").strip()
|
|
@@ -22,7 +15,7 @@ def resolve_token(hf_token: gr.OAuthToken | None) -> str | None:
|
|
| 22 |
|
| 23 |
|
| 24 |
def _normalize_content(content: object) -> str:
|
| 25 |
-
"""
|
| 26 |
if isinstance(content, str):
|
| 27 |
return content
|
| 28 |
if isinstance(content, list):
|
|
@@ -39,16 +32,18 @@ def _normalize_content(content: object) -> str:
|
|
| 39 |
def respond(
|
| 40 |
message: str,
|
| 41 |
history: list[dict],
|
| 42 |
-
model_id: str,
|
| 43 |
system_message: str,
|
| 44 |
max_tokens: int,
|
| 45 |
temperature: float,
|
| 46 |
top_p: float,
|
| 47 |
hf_token: gr.OAuthToken | None = None,
|
| 48 |
):
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
if not token:
|
| 51 |
-
yield "π
|
| 52 |
return
|
| 53 |
|
| 54 |
if not message or not message.strip():
|
|
@@ -69,11 +64,11 @@ def respond(
|
|
| 69 |
messages.append({"role": "user", "content": message.strip()})
|
| 70 |
|
| 71 |
try:
|
| 72 |
-
client = InferenceClient(token=token, model=
|
| 73 |
response = ""
|
| 74 |
|
| 75 |
for chunk in client.chat_completion(
|
| 76 |
-
messages
|
| 77 |
max_tokens=int(max_tokens),
|
| 78 |
stream=True,
|
| 79 |
temperature=float(temperature),
|
|
@@ -85,35 +80,28 @@ def respond(
|
|
| 85 |
yield response
|
| 86 |
|
| 87 |
except Exception as exc:
|
| 88 |
-
yield f"β **
|
| 89 |
|
| 90 |
|
| 91 |
chatbot = gr.ChatInterface(
|
| 92 |
-
|
| 93 |
additional_inputs=[
|
| 94 |
-
gr.
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
label="
|
| 103 |
-
lines=3,
|
| 104 |
),
|
| 105 |
-
gr.Slider(minimum=64, maximum=2048, value=512, step=32, label="Max new tokens"),
|
| 106 |
-
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 107 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 108 |
],
|
| 109 |
)
|
| 110 |
|
| 111 |
-
with gr.Blocks(
|
| 112 |
with gr.Sidebar():
|
| 113 |
-
gr.Markdown("## π Hugging Face Auth")
|
| 114 |
gr.LoginButton()
|
| 115 |
-
gr.LogoutButton()
|
| 116 |
-
|
| 117 |
chatbot.render()
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
DEFAULT_MODEL = "openai/gpt-oss-20b"
|
| 6 |
+
DEFAULT_SYSTEM_MESSAGE = "You are a friendly Chatbot."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def get_token(hf_token: gr.OAuthToken | None) -> str | None:
|
| 10 |
+
"""Resolve token from Gradio OAuth first, then HF_TOKEN for local runs."""
|
|
|
|
| 11 |
if hf_token is not None and getattr(hf_token, "token", None):
|
| 12 |
return hf_token.token
|
| 13 |
env_token = os.getenv("HF_TOKEN", "").strip()
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def _normalize_content(content: object) -> str:
|
| 18 |
+
"""Convert Gradio 6 content blocks or strings into plain text."""
|
| 19 |
if isinstance(content, str):
|
| 20 |
return content
|
| 21 |
if isinstance(content, list):
|
|
|
|
| 32 |
def respond(
|
| 33 |
message: str,
|
| 34 |
history: list[dict],
|
|
|
|
| 35 |
system_message: str,
|
| 36 |
max_tokens: int,
|
| 37 |
temperature: float,
|
| 38 |
top_p: float,
|
| 39 |
hf_token: gr.OAuthToken | None = None,
|
| 40 |
):
|
| 41 |
+
"""
|
| 42 |
+
Stream chat completions via Hugging Face InferenceClient.
|
| 43 |
+
"""
|
| 44 |
+
token = get_token(hf_token)
|
| 45 |
if not token:
|
| 46 |
+
yield "π Please log in using the sidebar button (or set `HF_TOKEN` locally)."
|
| 47 |
return
|
| 48 |
|
| 49 |
if not message or not message.strip():
|
|
|
|
| 64 |
messages.append({"role": "user", "content": message.strip()})
|
| 65 |
|
| 66 |
try:
|
| 67 |
+
client = InferenceClient(token=token, model=DEFAULT_MODEL)
|
| 68 |
response = ""
|
| 69 |
|
| 70 |
for chunk in client.chat_completion(
|
| 71 |
+
messages,
|
| 72 |
max_tokens=int(max_tokens),
|
| 73 |
stream=True,
|
| 74 |
temperature=float(temperature),
|
|
|
|
| 80 |
yield response
|
| 81 |
|
| 82 |
except Exception as exc:
|
| 83 |
+
yield f"β **Error:** `{type(exc).__name__}: {exc}`"
|
| 84 |
|
| 85 |
|
| 86 |
chatbot = gr.ChatInterface(
|
| 87 |
+
respond,
|
| 88 |
additional_inputs=[
|
| 89 |
+
gr.Textbox(value=DEFAULT_SYSTEM_MESSAGE, label="System message"),
|
| 90 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 91 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 92 |
+
gr.Slider(
|
| 93 |
+
minimum=0.1,
|
| 94 |
+
maximum=1.0,
|
| 95 |
+
value=0.95,
|
| 96 |
+
step=0.05,
|
| 97 |
+
label="Top-p (nucleus sampling)",
|
|
|
|
| 98 |
),
|
|
|
|
|
|
|
|
|
|
| 99 |
],
|
| 100 |
)
|
| 101 |
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
with gr.Sidebar():
|
|
|
|
| 104 |
gr.LoginButton()
|
|
|
|
|
|
|
| 105 |
chatbot.render()
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|