Upload 4 files
Browse files- .gitignore +3 -0
- README.md +21 -9
- app.py +143 -0
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
.gradio/
|
README.md
CHANGED
|
@@ -1,15 +1,27 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.27.0
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
short_description:
|
|
|
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: GPT-OSS 20B Heretic Chat
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.27.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
+
python_version: "3.12"
|
| 10 |
+
startup_duration_timeout: 1h
|
| 11 |
+
short_description: Chat demo for GPT-OSS 20B Heretic
|
| 12 |
+
models:
|
| 13 |
+
- p-e-w/gpt-oss-20b-heretic
|
| 14 |
---
|
| 15 |
|
| 16 |
+
# GPT-OSS 20B Heretic Chat
|
| 17 |
+
|
| 18 |
+
A ZeroGPU Gradio demo for [`p-e-w/gpt-oss-20b-heretic`](https://huggingface.co/p-e-w/gpt-oss-20b-heretic), a community-modified version of OpenAI's `gpt-oss-20b`.
|
| 19 |
+
|
| 20 |
+
The interface supports multi-turn chat, configurable reasoning effort, temperature, and output length. It follows the model's Harmony chat template and displays only the user-facing final channel.
|
| 21 |
+
|
| 22 |
+
## Notes
|
| 23 |
+
|
| 24 |
+
- This model is a third-party modification and is not published or endorsed by OpenAI.
|
| 25 |
+
- Model output can be inaccurate, biased, or objectionable.
|
| 26 |
+
- The Space uses ZeroGPU; visitors consume their own GPU quota when generating.
|
| 27 |
+
- The model is licensed under Apache 2.0. See the source model repository for details.
|
app.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
MODEL_ID = "p-e-w/gpt-oss-20b-heretic"
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
torch_dtype="auto",
|
| 16 |
+
low_cpu_mem_usage=True,
|
| 17 |
+
attn_implementation="sdpa",
|
| 18 |
+
).eval().to("cuda")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _final_channel(text: str) -> str:
|
| 22 |
+
"""Return only the user-facing Harmony final channel when present."""
|
| 23 |
+
matches = re.findall(
|
| 24 |
+
r"<\|channel\|>final<\|message\|>(.*?)(?=<\|end\|>|<\|return\|>|$)",
|
| 25 |
+
text,
|
| 26 |
+
flags=re.DOTALL,
|
| 27 |
+
)
|
| 28 |
+
if matches:
|
| 29 |
+
return matches[-1].strip()
|
| 30 |
+
|
| 31 |
+
cleaned = re.sub(r"<\|[^|]+\|>", "", text).strip()
|
| 32 |
+
return cleaned or "The model returned no user-facing text."
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@spaces.GPU(duration=180)
|
| 36 |
+
def chat(
|
| 37 |
+
message: str,
|
| 38 |
+
history: list[dict],
|
| 39 |
+
system_prompt: str,
|
| 40 |
+
reasoning_effort: str,
|
| 41 |
+
max_new_tokens: int,
|
| 42 |
+
temperature: float,
|
| 43 |
+
) -> str:
|
| 44 |
+
"""Chat with p-e-w/gpt-oss-20b-heretic.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
message: The newest user message.
|
| 48 |
+
history: Prior Gradio chat messages.
|
| 49 |
+
system_prompt: Optional behavior instructions for the model.
|
| 50 |
+
reasoning_effort: Low, medium, or high reasoning effort.
|
| 51 |
+
max_new_tokens: Maximum number of tokens to generate.
|
| 52 |
+
temperature: Sampling temperature; zero uses greedy decoding.
|
| 53 |
+
"""
|
| 54 |
+
messages = [
|
| 55 |
+
{
|
| 56 |
+
"role": "system",
|
| 57 |
+
"content": f"Reasoning: {reasoning_effort.lower()}\n{system_prompt.strip()}",
|
| 58 |
+
}
|
| 59 |
+
]
|
| 60 |
+
for item in history[-12:]:
|
| 61 |
+
role = item.get("role")
|
| 62 |
+
content = item.get("content")
|
| 63 |
+
if role in {"user", "assistant"} and isinstance(content, str):
|
| 64 |
+
messages.append({"role": role, "content": content})
|
| 65 |
+
messages.append({"role": "user", "content": message})
|
| 66 |
+
|
| 67 |
+
encoded = tokenizer.apply_chat_template(
|
| 68 |
+
messages,
|
| 69 |
+
add_generation_prompt=True,
|
| 70 |
+
return_tensors="pt",
|
| 71 |
+
return_dict=True,
|
| 72 |
+
).to("cuda")
|
| 73 |
+
|
| 74 |
+
sampling = float(temperature) > 0
|
| 75 |
+
with torch.inference_mode():
|
| 76 |
+
output = model.generate(
|
| 77 |
+
**encoded,
|
| 78 |
+
max_new_tokens=int(max_new_tokens),
|
| 79 |
+
do_sample=sampling,
|
| 80 |
+
temperature=float(temperature) if sampling else None,
|
| 81 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
generated = output[0, encoded["input_ids"].shape[1] :]
|
| 85 |
+
raw_text = tokenizer.decode(generated, skip_special_tokens=False)
|
| 86 |
+
return _final_channel(raw_text)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
CSS = """
|
| 90 |
+
#app { max-width: 1050px; margin: 0 auto; }
|
| 91 |
+
.dark .gradio-container { color: var(--body-text-color); }
|
| 92 |
+
.model-note { opacity: .78; font-size: .92rem; }
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
with gr.Blocks(theme=gr.themes.Citrus(), css=CSS) as demo:
|
| 96 |
+
with gr.Column(elem_id="app"):
|
| 97 |
+
gr.Markdown(
|
| 98 |
+
"""
|
| 99 |
+
# GPT‑OSS 20B Heretic
|
| 100 |
+
Chat with the community-modified `p-e-w/gpt-oss-20b-heretic` model.
|
| 101 |
+
|
| 102 |
+
<span class="model-note">This demo returns only the model's final answer, not its private reasoning channel. Outputs may be inaccurate or objectionable.</span>
|
| 103 |
+
"""
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
with gr.Accordion("Generation settings", open=False):
|
| 107 |
+
system_prompt = gr.Textbox(
|
| 108 |
+
label="System prompt",
|
| 109 |
+
value="You are a direct, helpful assistant.",
|
| 110 |
+
lines=3,
|
| 111 |
+
)
|
| 112 |
+
with gr.Row():
|
| 113 |
+
reasoning_effort = gr.Radio(
|
| 114 |
+
["Low", "Medium", "High"], value="Medium", label="Reasoning"
|
| 115 |
+
)
|
| 116 |
+
max_new_tokens = gr.Slider(
|
| 117 |
+
64, 1024, value=512, step=64, label="Max new tokens"
|
| 118 |
+
)
|
| 119 |
+
temperature = gr.Slider(
|
| 120 |
+
0, 1.5, value=0.7, step=0.05, label="Temperature"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
gr.ChatInterface(
|
| 124 |
+
fn=chat,
|
| 125 |
+
additional_inputs=[
|
| 126 |
+
system_prompt,
|
| 127 |
+
reasoning_effort,
|
| 128 |
+
max_new_tokens,
|
| 129 |
+
temperature,
|
| 130 |
+
],
|
| 131 |
+
examples=[
|
| 132 |
+
["Explain quantum mechanics clearly and concisely."],
|
| 133 |
+
["Write a tiny Python function that detects palindromes."],
|
| 134 |
+
["Give me three unusual ideas for a science-fiction short story."],
|
| 135 |
+
],
|
| 136 |
+
cache_examples=True,
|
| 137 |
+
cache_mode="lazy",
|
| 138 |
+
type="messages",
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.55.0
|
| 2 |
+
accelerate>=1.9.0
|
| 3 |
+
kernels>=0.9.0
|
| 4 |
+
safetensors>=0.5.3
|