Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
|
@@ -11,60 +82,51 @@ def respond(
|
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
],
|
| 61 |
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
with gr.Sidebar():
|
|
|
|
| 65 |
gr.LoginButton()
|
|
|
|
| 66 |
chatbot.render()
|
| 67 |
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import threading
|
| 3 |
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import (
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
AutoModelForCausalLM,
|
| 8 |
+
TextIteratorStreamer,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
MODEL_ID = "Amey9766/qwen-0.6b-hospitality-housekeeping"
|
| 12 |
+
|
| 13 |
+
# --- Load once (global) so it doesn't reload every message ---
|
| 14 |
+
tokenizer = None
|
| 15 |
+
model = None
|
| 16 |
+
device = None
|
| 17 |
+
|
| 18 |
+
def load_model(hf_token: str | None = None):
|
| 19 |
+
global tokenizer, model, device
|
| 20 |
+
|
| 21 |
+
if model is not None and tokenizer is not None:
|
| 22 |
+
return
|
| 23 |
+
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 26 |
+
|
| 27 |
+
# If your repo is private/gated, you must provide a token.
|
| 28 |
+
# Priority: Gradio OAuth token -> Space secret HF_TOKEN -> None
|
| 29 |
+
use_token = hf_token or os.getenv("HF_TOKEN")
|
| 30 |
+
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 32 |
+
MODEL_ID,
|
| 33 |
+
token=use_token,
|
| 34 |
+
trust_remote_code=True,
|
| 35 |
+
use_fast=True,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 39 |
+
MODEL_ID,
|
| 40 |
+
token=use_token,
|
| 41 |
+
torch_dtype=dtype,
|
| 42 |
+
device_map="auto" if device == "cuda" else None,
|
| 43 |
+
trust_remote_code=True,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if device == "cpu":
|
| 47 |
+
model.to(device)
|
| 48 |
+
|
| 49 |
+
def build_prompt(system_message: str, history: list[dict[str, str]], user_message: str) -> str:
|
| 50 |
+
"""
|
| 51 |
+
Universal prompt builder.
|
| 52 |
+
Works even if the model doesn't have a strict chat template.
|
| 53 |
+
If your tokenizer supports apply_chat_template, we use it.
|
| 54 |
+
"""
|
| 55 |
+
messages = [{"role": "system", "content": system_message}]
|
| 56 |
+
messages.extend(history)
|
| 57 |
+
messages.append({"role": "user", "content": user_message})
|
| 58 |
+
|
| 59 |
+
if hasattr(tokenizer, "apply_chat_template"):
|
| 60 |
+
try:
|
| 61 |
+
return tokenizer.apply_chat_template(
|
| 62 |
+
messages,
|
| 63 |
+
tokenize=False,
|
| 64 |
+
add_generation_prompt=True
|
| 65 |
+
)
|
| 66 |
+
except Exception:
|
| 67 |
+
pass
|
| 68 |
|
| 69 |
+
# Fallback plain prompt
|
| 70 |
+
prompt = f"System: {system_message}\n"
|
| 71 |
+
for m in history:
|
| 72 |
+
prompt += f"{m['role'].capitalize()}: {m['content']}\n"
|
| 73 |
+
prompt += f"User: {user_message}\nAssistant:"
|
| 74 |
+
return prompt
|
| 75 |
|
| 76 |
def respond(
|
| 77 |
message,
|
|
|
|
| 82 |
top_p,
|
| 83 |
hf_token: gr.OAuthToken,
|
| 84 |
):
|
| 85 |
+
# Load model (once)
|
| 86 |
+
load_model(hf_token.token if hf_token else None)
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
prompt = build_prompt(system_message, history, message)
|
| 89 |
|
| 90 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 91 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 92 |
|
| 93 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 94 |
|
| 95 |
+
gen_kwargs = dict(
|
| 96 |
+
**inputs,
|
| 97 |
+
max_new_tokens=int(max_tokens),
|
| 98 |
+
do_sample=True,
|
| 99 |
+
temperature=float(temperature),
|
| 100 |
+
top_p=float(top_p),
|
| 101 |
+
streamer=streamer,
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
# Run generation in background thread so streamer can yield tokens
|
| 105 |
+
thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)
|
| 106 |
+
thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
partial = ""
|
| 109 |
+
for token in streamer:
|
| 110 |
+
partial += token
|
| 111 |
+
yield partial
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
chatbot = gr.ChatInterface(
|
| 114 |
respond,
|
| 115 |
type="messages",
|
| 116 |
additional_inputs=[
|
| 117 |
+
gr.Textbox(value="You are a helpful housekeeping assistant for hotels.", label="System message"),
|
| 118 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 119 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 120 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
],
|
| 122 |
)
|
| 123 |
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
with gr.Sidebar():
|
| 126 |
+
gr.Markdown("### Login (only needed if the model repo is private/gated)")
|
| 127 |
gr.LoginButton()
|
| 128 |
+
gr.Markdown(f"**Model:** `{MODEL_ID}`")
|
| 129 |
chatbot.render()
|
| 130 |
|
|
|
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|