Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files- README.md +11 -7
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +133 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.19.0
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: fable-traces
|
| 3 |
+
emoji: 📖
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.19.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
+
short_description: Chat with fable-traces, a Qwen3-4B-Instruct finetune
|
| 10 |
+
python_version: "3.12"
|
| 11 |
+
startup_duration_timeout: 30m
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# fable-traces
|
| 15 |
+
|
| 16 |
+
A streaming chat demo for [`AliesTaha/fable-traces`](https://huggingface.co/AliesTaha/fable-traces),
|
| 17 |
+
a compact instruction-tuned model built on **Qwen3-4B-Instruct-2507**. Runs on ZeroGPU.
|
__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (6.11 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from threading import Thread
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "AliesTaha/fable-traces"
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_ID,
|
| 12 |
+
torch_dtype=torch.bfloat16,
|
| 13 |
+
attn_implementation="sdpa",
|
| 14 |
+
).to("cuda")
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
DEFAULT_SYSTEM = "You are a helpful, concise assistant."
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@spaces.GPU(duration=90)
|
| 21 |
+
def chat(
|
| 22 |
+
message: str,
|
| 23 |
+
history: list,
|
| 24 |
+
system_prompt: str = DEFAULT_SYSTEM,
|
| 25 |
+
max_new_tokens: int = 512,
|
| 26 |
+
temperature: float = 0.7,
|
| 27 |
+
top_p: float = 0.9,
|
| 28 |
+
):
|
| 29 |
+
"""Chat with the fable-traces (Qwen3-4B-Instruct finetune) model.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
message: the user's latest message.
|
| 33 |
+
history: prior conversation turns (managed by Gradio ChatInterface).
|
| 34 |
+
system_prompt: instruction that steers the assistant's behaviour.
|
| 35 |
+
max_new_tokens: maximum number of tokens to generate in the reply.
|
| 36 |
+
temperature: sampling temperature; higher is more random.
|
| 37 |
+
top_p: nucleus sampling probability mass.
|
| 38 |
+
"""
|
| 39 |
+
messages = []
|
| 40 |
+
if system_prompt and system_prompt.strip():
|
| 41 |
+
messages.append({"role": "system", "content": system_prompt.strip()})
|
| 42 |
+
for turn in history:
|
| 43 |
+
if isinstance(turn, dict):
|
| 44 |
+
messages.append({"role": turn["role"], "content": turn["content"]})
|
| 45 |
+
else:
|
| 46 |
+
user_msg, assistant_msg = turn
|
| 47 |
+
if user_msg:
|
| 48 |
+
messages.append({"role": "user", "content": user_msg})
|
| 49 |
+
if assistant_msg:
|
| 50 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 51 |
+
messages.append({"role": "user", "content": message})
|
| 52 |
+
|
| 53 |
+
inputs = tokenizer.apply_chat_template(
|
| 54 |
+
messages,
|
| 55 |
+
add_generation_prompt=True,
|
| 56 |
+
return_tensors="pt",
|
| 57 |
+
).to(model.device)
|
| 58 |
+
|
| 59 |
+
streamer = TextIteratorStreamer(
|
| 60 |
+
tokenizer, skip_prompt=True, skip_special_tokens=True
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
do_sample = temperature > 0
|
| 64 |
+
gen_kwargs = dict(
|
| 65 |
+
input_ids=inputs,
|
| 66 |
+
streamer=streamer,
|
| 67 |
+
max_new_tokens=int(max_new_tokens),
|
| 68 |
+
do_sample=do_sample,
|
| 69 |
+
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 70 |
+
)
|
| 71 |
+
if do_sample:
|
| 72 |
+
gen_kwargs["temperature"] = float(temperature)
|
| 73 |
+
gen_kwargs["top_p"] = float(top_p)
|
| 74 |
+
|
| 75 |
+
thread = Thread(target=model.generate, kwargs=gen_kwargs)
|
| 76 |
+
thread.start()
|
| 77 |
+
|
| 78 |
+
partial = ""
|
| 79 |
+
for token in streamer:
|
| 80 |
+
partial += token
|
| 81 |
+
yield partial
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
CSS = """
|
| 85 |
+
#col-container { max-width: 900px; margin: 0 auto; }
|
| 86 |
+
.dark .gradio-container { color: var(--body-text-color); }
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
with gr.Blocks(theme=gr.themes.Citrus(), css=CSS) as demo:
|
| 90 |
+
with gr.Column(elem_id="col-container"):
|
| 91 |
+
gr.Markdown(
|
| 92 |
+
"""
|
| 93 |
+
# 📖 fable-traces
|
| 94 |
+
Chat with [`AliesTaha/fable-traces`](https://huggingface.co/AliesTaha/fable-traces),
|
| 95 |
+
a compact instruction-tuned model built on **Qwen3-4B-Instruct-2507**.
|
| 96 |
+
Tuned for short, conversational replies.
|
| 97 |
+
"""
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
with gr.Accordion("Advanced settings", open=False):
|
| 101 |
+
system_prompt = gr.Textbox(
|
| 102 |
+
label="System prompt",
|
| 103 |
+
value=DEFAULT_SYSTEM,
|
| 104 |
+
lines=2,
|
| 105 |
+
)
|
| 106 |
+
max_new_tokens = gr.Slider(
|
| 107 |
+
minimum=16, maximum=2048, value=512, step=16,
|
| 108 |
+
label="Max new tokens",
|
| 109 |
+
)
|
| 110 |
+
temperature = gr.Slider(
|
| 111 |
+
minimum=0.0, maximum=1.5, value=0.7, step=0.05,
|
| 112 |
+
label="Temperature (0 = greedy)",
|
| 113 |
+
)
|
| 114 |
+
top_p = gr.Slider(
|
| 115 |
+
minimum=0.1, maximum=1.0, value=0.9, step=0.05,
|
| 116 |
+
label="Top-p",
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
gr.ChatInterface(
|
| 120 |
+
fn=chat,
|
| 121 |
+
type="messages",
|
| 122 |
+
additional_inputs=[system_prompt, max_new_tokens, temperature, top_p],
|
| 123 |
+
examples=[
|
| 124 |
+
["Tell me something interesting."],
|
| 125 |
+
["Write a two-line poem about the desert at night."],
|
| 126 |
+
["Explain what a large language model is in one sentence."],
|
| 127 |
+
["Give me three tips for staying focused while studying."],
|
| 128 |
+
],
|
| 129 |
+
cache_examples=False,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
accelerate
|