app.py
CHANGED
|
@@ -1,120 +1,63 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
from typing import List, Dict, Optional
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
history: List[Dict[str, str]],
|
| 10 |
-
system_message: str,
|
| 11 |
-
max_tokens: int,
|
| 12 |
-
temperature: float,
|
| 13 |
-
top_p: float,
|
| 14 |
-
hf_token: Optional[gr.OAuthToken], # from LoginButton (kept)
|
| 15 |
-
pat_override: str, # NEW: user-pasted PAT (password field)
|
| 16 |
-
):
|
| 17 |
-
"""
|
| 18 |
-
Use PAT override if provided; otherwise fall back to LoginButton token.
|
| 19 |
-
NOTE: OAuth token from LoginButton usually lacks `inference.endpoints.infer.write`,
|
| 20 |
-
so for Inference Endpoints you almost always need to paste a PAT here.
|
| 21 |
-
"""
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
temperature=temperature,
|
| 45 |
-
top_p=top_p,
|
| 46 |
-
stream=True,
|
| 47 |
-
):
|
| 48 |
-
tok = ""
|
| 49 |
-
if getattr(chunk, "choices", None) and getattr(chunk.choices[0], "delta", None):
|
| 50 |
-
tok = chunk.choices[0].delta.content or ""
|
| 51 |
-
out += tok
|
| 52 |
-
yield out
|
| 53 |
-
return
|
| 54 |
-
except Exception as e_chat:
|
| 55 |
-
chat_err = str(e_chat)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def to_prompt(msgs: List[Dict[str, str]]) -> str:
|
| 60 |
-
lines = []
|
| 61 |
-
for m in msgs:
|
| 62 |
-
role = m.get("role", "user")
|
| 63 |
-
content = m.get("content", "")
|
| 64 |
-
tag = {"system": "SYSTEM", "user": "USER"}.get(role, "ASSISTANT")
|
| 65 |
-
lines.append(f"[{tag}] {content}")
|
| 66 |
-
lines.append("[ASSISTANT]")
|
| 67 |
-
return "\n".join(lines)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
return_full_text=False,
|
| 78 |
-
):
|
| 79 |
-
piece = getattr(tok, "token", tok)
|
| 80 |
-
if isinstance(piece, dict) and "text" in piece:
|
| 81 |
-
piece = piece["text"]
|
| 82 |
-
out += str(piece)
|
| 83 |
-
yield out
|
| 84 |
-
except Exception as e_gen:
|
| 85 |
-
yield (
|
| 86 |
-
"❗ Endpoint call failed.\n\n"
|
| 87 |
-
f"• Chat API error: {chat_err}\n"
|
| 88 |
-
f"• Text-generation fallback error: {e_gen}\n\n"
|
| 89 |
-
"Most likely cause: the token used does NOT have `inference.endpoints.infer.write`.\n"
|
| 90 |
-
"Paste a PAT with that scope in the sidebar."
|
| 91 |
-
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
gr.Slider(1, 4096, value=512, step=1, label="Max new tokens"),
|
| 100 |
-
gr.Slider(0.0, 4.0, value=0.7, step=0.1, label="Temperature"),
|
| 101 |
-
gr.Slider(0.0, 1.0, value=0.95, step=0.05, label="Top-p"),
|
| 102 |
-
# NEW: secure PAT override
|
| 103 |
-
gr.Textbox(value="", label="HF PAT (with `inference.endpoints.infer.write`)", type="password"),
|
| 104 |
-
],
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
with gr.Blocks() as demo:
|
| 108 |
-
with gr.Sidebar():
|
| 109 |
-
gr.Markdown("### Hugging Face Login (optional)")
|
| 110 |
-
gr.LoginButton()
|
| 111 |
-
gr.Markdown(
|
| 112 |
-
"**Important:** Inference Endpoints require a PAT with\n"
|
| 113 |
-
"`inference.endpoints.infer.write`. The Login token usually does **not** have this.\n"
|
| 114 |
-
"Paste a PAT in the password field if you see 403 errors."
|
| 115 |
-
)
|
| 116 |
-
gr.Markdown(f"**Endpoint**: `{ENDPOINT_URL}`")
|
| 117 |
-
chat.render()
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
|
|
|
| 4 |
|
| 5 |
+
# Pick up secrets from HF Space
|
| 6 |
+
BASE = os.getenv("HF_ENDPOINT_URL", "").rstrip("/")
|
| 7 |
+
API_KEY = os.getenv("HF_TOKEN")
|
| 8 |
+
MODEL_ID = "kaizen9/qsft_30_6000_v2"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
client = OpenAI(
|
| 11 |
+
base_url=f"{BASE}/v1",
|
| 12 |
+
api_key=API_KEY,
|
| 13 |
+
)
|
|
|
|
| 14 |
|
| 15 |
+
def build_messages(history, user_msg, system_msg):
|
| 16 |
+
msgs = []
|
| 17 |
+
if system_msg.strip():
|
| 18 |
+
msgs.append({"role": "system", "content": system_msg.strip()})
|
| 19 |
+
for u, a in history:
|
| 20 |
+
if u: msgs.append({"role": "user", "content": u})
|
| 21 |
+
if a: msgs.append({"role": "assistant", "content": a})
|
| 22 |
+
msgs.append({"role": "user", "content": user_msg})
|
| 23 |
+
return msgs
|
| 24 |
|
| 25 |
+
def chat_fn(message, history, system_message, temperature, top_p, max_tokens):
|
| 26 |
+
msgs = build_messages(history, message, system_message)
|
| 27 |
+
stream = client.chat.completions.create(
|
| 28 |
+
model=MODEL_ID,
|
| 29 |
+
messages=msgs,
|
| 30 |
+
temperature=float(temperature),
|
| 31 |
+
top_p=float(top_p),
|
| 32 |
+
max_tokens=int(max_tokens),
|
| 33 |
+
stream=True,
|
| 34 |
+
)
|
| 35 |
|
| 36 |
+
partial = ""
|
| 37 |
+
for chunk in stream:
|
| 38 |
+
delta = chunk.choices[0].delta
|
| 39 |
+
if delta and delta.content:
|
| 40 |
+
partial += delta.content
|
| 41 |
+
yield partial
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# QSFT Chat UI")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
system_box = gr.Textbox(
|
| 47 |
+
label="System prompt",
|
| 48 |
+
value="You are a helpful assistant.",
|
| 49 |
+
lines=2,
|
| 50 |
+
)
|
| 51 |
+
temp = gr.Slider(0.0, 2.0, 0.7, step=0.1, label="Temperature")
|
| 52 |
+
topp = gr.Slider(0.0, 1.0, 0.95, step=0.01, label="Top-p")
|
| 53 |
+
maxt = gr.Slider(16, 4096, 512, step=16, label="Max tokens")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
gr.ChatInterface(
|
| 56 |
+
fn=chat_fn,
|
| 57 |
+
additional_inputs=[system_box, temp, topp, maxt],
|
| 58 |
+
retry_btn=True,
|
| 59 |
+
undo_btn=True,
|
| 60 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.launch()
|