Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,18 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
6 |
def respond(
|
7 |
message: str,
|
8 |
history: list[dict[str, str]],
|
@@ -14,31 +25,21 @@ def respond(
|
|
14 |
"""
|
15 |
Generate a response using the HuggingFace Inference API.
|
16 |
|
17 |
-
The system prompt is taken from the secret **prec_chat
|
18 |
-
|
19 |
"""
|
20 |
-
#
|
21 |
-
|
22 |
-
# ----------------------------------------------------------------------
|
23 |
-
# If the secret is missing we fall back to a generic prompt so the app
|
24 |
-
# still works locally.
|
25 |
-
system_message = os.getenv("prec_chat", "You are a helpful assistant.")
|
26 |
|
27 |
-
# ----------------------------------------------------------------------
|
28 |
# 2️⃣ Initialise the HF inference client.
|
29 |
-
# ----------------------------------------------------------------------
|
30 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
31 |
|
32 |
-
# ----------------------------------------------------------------------
|
33 |
# 3️⃣ Build the message list for the chat completion endpoint.
|
34 |
-
# ----------------------------------------------------------------------
|
35 |
messages = [{"role": "system", "content": system_message}]
|
36 |
messages.extend(history) # previous conversation
|
37 |
messages.append({"role": "user", "content": message}) # current query
|
38 |
|
39 |
-
# ----------------------------------------------------------------------
|
40 |
# 4️⃣ Stream the response back to the UI.
|
41 |
-
# ----------------------------------------------------------------------
|
42 |
response = ""
|
43 |
for chunk in client.chat_completion(
|
44 |
messages,
|
@@ -47,25 +48,22 @@ def respond(
|
|
47 |
temperature=temperature,
|
48 |
top_p=top_p,
|
49 |
):
|
50 |
-
# The API returns a list of choices – we only care about the first one.
|
51 |
choices = chunk.choices
|
52 |
token = ""
|
53 |
if choices and choices[0].delta.content:
|
54 |
token = choices[0].delta.content
|
55 |
-
|
56 |
response += token
|
57 |
yield response
|
58 |
|
59 |
|
60 |
-
#
|
61 |
# UI definition – the system‑prompt textbox has been removed.
|
62 |
-
#
|
63 |
chatbot = gr.ChatInterface(
|
64 |
respond,
|
65 |
type="messages",
|
66 |
additional_inputs=[
|
67 |
-
#
|
68 |
-
# generation parameters.
|
69 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
70 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
71 |
gr.Slider(
|
@@ -78,11 +76,31 @@ chatbot = gr.ChatInterface(
|
|
78 |
],
|
79 |
)
|
80 |
|
|
|
|
|
|
|
81 |
with gr.Blocks() as demo:
|
82 |
-
with gr.Sidebar():
|
83 |
-
gr.LoginButton()
|
84 |
chatbot.render()
|
85 |
|
86 |
|
|
|
|
|
|
|
87 |
if __name__ == "__main__":
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
+
# ----------------------------------------------------------------------
|
6 |
+
# Helper: read a secret with a safe fallback (useful when you run the
|
7 |
+
# script locally without a secrets file).
|
8 |
+
# ----------------------------------------------------------------------
|
9 |
+
def _secret(key: str, fallback: str) -> str:
|
10 |
+
"""Return the value of a secret or the supplied fallback."""
|
11 |
+
return os.getenv(key, fallback)
|
12 |
|
13 |
+
|
14 |
+
# ----------------------------------------------------------------------
|
15 |
+
# Core chat logic – the system prompt now comes from the secret `prec_chat`.
|
16 |
+
# ----------------------------------------------------------------------
|
17 |
def respond(
|
18 |
message: str,
|
19 |
history: list[dict[str, str]],
|
|
|
25 |
"""
|
26 |
Generate a response using the HuggingFace Inference API.
|
27 |
|
28 |
+
The system prompt is taken from the secret **prec_chat**.
|
29 |
+
Users cannot edit it from the UI.
|
30 |
"""
|
31 |
+
# 1️⃣ Load the system prompt (fallback = generic assistant)
|
32 |
+
system_message = _secret("prec_chat", "You are a helpful assistant.")
|
|
|
|
|
|
|
|
|
33 |
|
|
|
34 |
# 2️⃣ Initialise the HF inference client.
|
|
|
35 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
36 |
|
|
|
37 |
# 3️⃣ Build the message list for the chat completion endpoint.
|
|
|
38 |
messages = [{"role": "system", "content": system_message}]
|
39 |
messages.extend(history) # previous conversation
|
40 |
messages.append({"role": "user", "content": message}) # current query
|
41 |
|
|
|
42 |
# 4️⃣ Stream the response back to the UI.
|
|
|
43 |
response = ""
|
44 |
for chunk in client.chat_completion(
|
45 |
messages,
|
|
|
48 |
temperature=temperature,
|
49 |
top_p=top_p,
|
50 |
):
|
|
|
51 |
choices = chunk.choices
|
52 |
token = ""
|
53 |
if choices and choices[0].delta.content:
|
54 |
token = choices[0].delta.content
|
|
|
55 |
response += token
|
56 |
yield response
|
57 |
|
58 |
|
59 |
+
# ----------------------------------------------------------------------
|
60 |
# UI definition – the system‑prompt textbox has been removed.
|
61 |
+
# ----------------------------------------------------------------------
|
62 |
chatbot = gr.ChatInterface(
|
63 |
respond,
|
64 |
type="messages",
|
65 |
additional_inputs=[
|
66 |
+
# Only generation parameters are exposed now.
|
|
|
67 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
68 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
69 |
gr.Slider(
|
|
|
76 |
],
|
77 |
)
|
78 |
|
79 |
+
# ----------------------------------------------------------------------
|
80 |
+
# Build the Blocks layout (no LoginButton – we use our own auth).
|
81 |
+
# ----------------------------------------------------------------------
|
82 |
with gr.Blocks() as demo:
|
|
|
|
|
83 |
chatbot.render()
|
84 |
|
85 |
|
86 |
+
# ----------------------------------------------------------------------
|
87 |
+
# Launch with **basic authentication**.
|
88 |
+
# ----------------------------------------------------------------------
|
89 |
if __name__ == "__main__":
|
90 |
+
# Pull the allowed credentials from secrets (fallback = no access)
|
91 |
+
allowed_user = _secret("CHAT_USER", "")
|
92 |
+
allowed_pass = _secret("CHAT_PASS", "")
|
93 |
+
|
94 |
+
# If either is missing we refuse to start – this prevents an accidental
|
95 |
+
# open‑access deployment.
|
96 |
+
if not allowed_user or not allowed_pass:
|
97 |
+
raise RuntimeError(
|
98 |
+
"Authentication credentials not found in secrets. "
|
99 |
+
"Add CHAT_USER and CHAT_PASS to secrets.toml."
|
100 |
+
)
|
101 |
+
|
102 |
+
demo.launch(
|
103 |
+
auth=(allowed_user, allowed_pass), # <-- Gradio's built‑in basic auth
|
104 |
+
# optional: you can also set `auth_message="Please log in"` or
|
105 |
+
# `prevent_thread_lock=True` depending on your deployment.
|
106 |
+
)
|