Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
message,
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 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 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import warnings
|
| 3 |
+
|
| 4 |
+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
| 5 |
+
warnings.filterwarnings("ignore")
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
import gradio as gr
|
| 9 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, logging as hf_logging
|
| 10 |
|
| 11 |
+
hf_logging.set_verbosity_error()
|
| 12 |
|
| 13 |
+
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
MODEL_ID = "SupraLabs/Supra-50M-Instruct"
|
| 16 |
+
DEVICE = "cpu"
|
| 17 |
|
| 18 |
+
# ββ Load model ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
|
| 20 |
+
print(f"[*] Loading {MODEL_ID} on CPU...")
|
| 21 |
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, clean_up_tokenization_spaces=False)
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float32)
|
| 24 |
+
model.eval()
|
| 25 |
|
| 26 |
+
print("[+] Model ready.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# ββ Prompt builder (Alpaca format) ββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 29 |
|
| 30 |
+
def build_prompt(history: list[dict], system: str) -> str:
|
| 31 |
+
"""Convert chat history into the Alpaca instruct format the model expects."""
|
| 32 |
+
parts = []
|
| 33 |
|
| 34 |
+
if system.strip():
|
| 35 |
+
parts.append(
|
| 36 |
+
"Below is an instruction that describes a task. "
|
| 37 |
+
"Write a response that appropriately completes the request.\n\n"
|
| 38 |
+
f"### Instruction:\n{system}\n\n### Response:\nUnderstood.\n"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
for msg in history:
|
| 42 |
+
role, content = msg["role"], msg["content"]
|
| 43 |
+
if role == "user":
|
| 44 |
+
parts.append(
|
| 45 |
+
"Below is an instruction that describes a task. "
|
| 46 |
+
"Write a response that appropriately completes the request.\n\n"
|
| 47 |
+
f"### Instruction:\n{content}\n\n### Response:\n"
|
| 48 |
+
)
|
| 49 |
+
elif role == "assistant":
|
| 50 |
+
parts.append(content + "\n")
|
| 51 |
+
|
| 52 |
+
return "".join(parts)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# ββ Generation ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
+
|
| 57 |
+
def generate_response(
|
| 58 |
+
message: str,
|
| 59 |
+
history: list[dict],
|
| 60 |
+
system_prompt: str,
|
| 61 |
+
max_new_tokens: int,
|
| 62 |
+
temperature: float,
|
| 63 |
+
top_p: float,
|
| 64 |
+
repetition_penalty: float,
|
| 65 |
+
) -> str:
|
| 66 |
+
history = history + [{"role": "user", "content": message}]
|
| 67 |
+
prompt = build_prompt(history, system_prompt)
|
| 68 |
+
|
| 69 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
| 70 |
+
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
output_ids = model.generate(
|
| 73 |
+
**inputs,
|
| 74 |
+
max_new_tokens=max_new_tokens,
|
| 75 |
+
do_sample=temperature > 0,
|
| 76 |
+
temperature=temperature if temperature > 0 else 1.0,
|
| 77 |
+
top_p=top_p,
|
| 78 |
+
top_k=50,
|
| 79 |
+
repetition_penalty=repetition_penalty,
|
| 80 |
+
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 81 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
new_tokens = output_ids[0][inputs["input_ids"].shape[-1]:]
|
| 85 |
+
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 89 |
+
|
| 90 |
+
DESCRIPTION = """
|
| 91 |
+
<div style="text-align:center; padding: 8px 0 4px;">
|
| 92 |
+
<h1 style="font-size:2rem; margin:0;">π¦
Supra-50M Instruct</h1>
|
| 93 |
+
<p style="color:#888; margin:4px 0 0;">50M-parameter chat model by <a href="https://huggingface.co/SupraLabs" target="_blank">SupraLabs</a> β running on CPU</p>
|
| 94 |
+
</div>
|
| 95 |
"""
|
| 96 |
+
|
| 97 |
+
with gr.Blocks(title="Supra-50M Instruct", theme=gr.themes.Soft()) as demo:
|
| 98 |
+
gr.HTML(DESCRIPTION)
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column(scale=3):
|
| 102 |
+
chatbot = gr.Chatbot(
|
| 103 |
+
label="Chat",
|
| 104 |
+
type="messages",
|
| 105 |
+
height=480,
|
| 106 |
+
show_copy_button=True,
|
| 107 |
+
)
|
| 108 |
+
with gr.Row():
|
| 109 |
+
msg_box = gr.Textbox(
|
| 110 |
+
placeholder="Type your messageβ¦",
|
| 111 |
+
show_label=False,
|
| 112 |
+
scale=5,
|
| 113 |
+
lines=1,
|
| 114 |
+
max_lines=4,
|
| 115 |
+
submit_btn=True,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
with gr.Column(scale=1, min_width=220):
|
| 119 |
+
gr.Markdown("### βοΈ Parameters")
|
| 120 |
+
system_prompt = gr.Textbox(
|
| 121 |
+
label="System prompt",
|
| 122 |
+
value="You are a helpful and concise assistant.",
|
| 123 |
+
lines=3,
|
| 124 |
+
)
|
| 125 |
+
max_new_tokens = gr.Slider(
|
| 126 |
+
label="Max new tokens", minimum=32, maximum=512, value=256, step=32
|
| 127 |
+
)
|
| 128 |
+
temperature = gr.Slider(
|
| 129 |
+
label="Temperature", minimum=0.1, maximum=1.5, value=0.7, step=0.05
|
| 130 |
+
)
|
| 131 |
+
top_p = gr.Slider(
|
| 132 |
+
label="Top-p", minimum=0.1, maximum=1.0, value=0.9, step=0.05
|
| 133 |
+
)
|
| 134 |
+
repetition_penalty = gr.Slider(
|
| 135 |
+
label="Repetition penalty", minimum=1.0, maximum=1.5, value=1.15, step=0.05
|
| 136 |
+
)
|
| 137 |
+
clear_btn = gr.Button("ποΈ Clear chat", variant="secondary")
|
| 138 |
+
|
| 139 |
+
# ββ State & wiring ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 140 |
+
|
| 141 |
+
chat_history = gr.State([])
|
| 142 |
+
|
| 143 |
+
def on_submit(message, history, system, max_tok, temp, top_p_val, rep_pen):
|
| 144 |
+
if not message.strip():
|
| 145 |
+
return history, history, ""
|
| 146 |
+
|
| 147 |
+
response = generate_response(
|
| 148 |
+
message, history, system, max_tok, temp, top_p_val, rep_pen
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
history = history + [
|
| 152 |
+
{"role": "user", "content": message},
|
| 153 |
+
{"role": "assistant", "content": response},
|
| 154 |
+
]
|
| 155 |
+
return history, history, ""
|
| 156 |
+
|
| 157 |
+
msg_box.submit(
|
| 158 |
+
fn=on_submit,
|
| 159 |
+
inputs=[msg_box, chat_history, system_prompt, max_new_tokens, temperature, top_p, repetition_penalty],
|
| 160 |
+
outputs=[chatbot, chat_history, msg_box],
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
clear_btn.click(
|
| 164 |
+
fn=lambda: ([], [], ""),
|
| 165 |
+
outputs=[chatbot, chat_history, msg_box],
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
gr.Markdown(
|
| 169 |
+
"<p style='text-align:center; color:#aaa; font-size:0.8rem; margin-top:12px;'>"
|
| 170 |
+
"Model: <a href='https://huggingface.co/SupraLabs/Supra-50M-Instruct' target='_blank'>SupraLabs/Supra-50M-Instruct</a> β "
|
| 171 |
+
"Apache 2.0 License β Β© SupraLabs 2026</p>"
|
| 172 |
+
)
|
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
+
demo.launch()
|