Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,99 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
messages.extend(history)
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
response = ""
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
response += token
|
| 40 |
yield response
|
| 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 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly
|
| 50 |
-
gr.Slider(minimum=1, maximum=
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
-
with gr.Sidebar():
|
| 64 |
-
gr.LoginButton()
|
| 65 |
-
chatbot.render()
|
| 66 |
-
|
| 67 |
-
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
|
| 7 |
+
MODEL_ID = "HuggingFaceTB/nanowhale-100m"
|
| 8 |
|
| 9 |
+
print(f"Loading model {MODEL_ID} ...")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_ID,
|
| 13 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
+
device_map="auto",
|
| 15 |
+
)
|
| 16 |
+
model.eval()
|
| 17 |
+
print("Model loaded.")
|
| 18 |
+
|
| 19 |
+
DEVICE = next(model.parameters()).device
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def build_prompt(system_message: str, history: list[dict[str, str]], user_message: str) -> str:
|
| 23 |
"""
|
| 24 |
+
Try to use the tokenizer's built-in chat template.
|
| 25 |
+
Fall back to a simple newline-delimited format if none exists.
|
| 26 |
"""
|
|
|
|
|
|
|
| 27 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 28 |
messages.extend(history)
|
| 29 |
+
messages.append({"role": "user", "content": user_message})
|
| 30 |
|
| 31 |
+
if tokenizer.chat_template is not None:
|
| 32 |
+
return tokenizer.apply_chat_template(
|
| 33 |
+
messages,
|
| 34 |
+
tokenize=False,
|
| 35 |
+
add_generation_prompt=True,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Fallback format
|
| 39 |
+
parts = [f"System: {system_message}\n"]
|
| 40 |
+
for msg in history:
|
| 41 |
+
role = "User" if msg["role"] == "user" else "Assistant"
|
| 42 |
+
parts.append(f"{role}: {msg['content']}\n")
|
| 43 |
+
parts.append(f"User: {user_message}\nAssistant:")
|
| 44 |
+
return "".join(parts)
|
| 45 |
|
|
|
|
| 46 |
|
| 47 |
+
def respond(
|
| 48 |
+
message: str,
|
| 49 |
+
history: list[dict[str, str]],
|
| 50 |
+
system_message: str,
|
| 51 |
+
max_new_tokens: int,
|
| 52 |
+
temperature: float,
|
| 53 |
+
top_p: float,
|
| 54 |
+
):
|
| 55 |
+
prompt = build_prompt(system_message, history, message)
|
| 56 |
+
|
| 57 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
| 58 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 59 |
+
|
| 60 |
+
streamer = TextIteratorStreamer(
|
| 61 |
+
tokenizer,
|
| 62 |
+
skip_prompt=True,
|
| 63 |
+
skip_special_tokens=True,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
generation_kwargs = dict(
|
| 67 |
+
**inputs,
|
| 68 |
+
max_new_tokens=max_new_tokens,
|
| 69 |
temperature=temperature,
|
| 70 |
top_p=top_p,
|
| 71 |
+
do_sample=temperature > 0.0,
|
| 72 |
+
streamer=streamer,
|
| 73 |
+
)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 76 |
+
thread.start()
|
| 77 |
+
|
| 78 |
+
response = ""
|
| 79 |
+
for token in streamer:
|
| 80 |
response += token
|
| 81 |
yield response
|
| 82 |
|
| 83 |
+
thread.join()
|
| 84 |
+
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
chatbot = gr.ChatInterface(
|
| 87 |
respond,
|
| 88 |
+
type="messages",
|
| 89 |
+
title="Timmy — powered by nanowhale-100m",
|
| 90 |
additional_inputs=[
|
| 91 |
+
gr.Textbox(value="You are a friendly chatbot.", label="System message"),
|
| 92 |
+
gr.Slider(minimum=1, maximum=512, value=128, step=1, label="Max new tokens"),
|
| 93 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 94 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
],
|
| 96 |
)
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
if __name__ == "__main__":
|
| 99 |
+
chatbot.launch()
|