Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
README.md
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
---
|
| 2 |
title: Concise Reasoning Demo
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
short_description: demo of LLMs fine-tuned for concise reasoning
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
-
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
|
| 1 |
---
|
| 2 |
title: Concise Reasoning Demo
|
| 3 |
+
emoji: 🏍️
|
| 4 |
+
colorFrom: red
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.12.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
short_description: demo of LLMs fine-tuned for concise reasoning
|
| 12 |
+
---
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,64 +1,149 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 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 |
-
|
| 58 |
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from collections.abc import Iterator
|
| 3 |
+
from threading import Thread
|
| 4 |
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 9 |
|
| 10 |
+
# Token limits
|
| 11 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
| 12 |
+
MAX_MAX_NEW_TOKENS = 2048
|
| 13 |
+
DEFAULT_MAX_NEW_TOKENS = 512
|
| 14 |
|
| 15 |
+
# Description
|
| 16 |
+
DESCRIPTION = """\
|
| 17 |
+
# Demo for "Self-Training Elicits Concise Reasoning in Large Language Models"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
This Space showcases the model [tergel/llama-3.2-3b-instruct-gsm8k-fs-gpt4o-bon](https://huggingface.co/tergel/llama-3.2-3b-instruct-gsm8k-fs-gpt4o-bon)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
We provide a simple chat interface allowing you to observe the concise CoT solutions that our model can produce. Feel free to play with it.
|
| 22 |
+
"""
|
| 23 |
|
| 24 |
+
# Decide on device
|
| 25 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
|
| 27 |
+
if not torch.cuda.is_available():
|
| 28 |
+
DESCRIPTION += "\n\n<p>**Warning**: Running on CPU 🥶 – this may be extremely slow. We will upgrade to GPUs soon.</p>"
|
| 29 |
+
|
| 30 |
+
# Load model and tokenizer
|
| 31 |
+
model_id = "tergel/llama-3.2-3b-instruct-gsm8k-fs-gpt4o-bon"
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
+
model_id,
|
| 34 |
+
device_map=None if device == "cpu" else "auto",
|
| 35 |
+
torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32,
|
| 36 |
+
)
|
| 37 |
+
model.to(device)
|
| 38 |
|
| 39 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 40 |
+
tokenizer.use_default_system_prompt = False
|
| 41 |
|
| 42 |
+
@spaces.GPU
|
| 43 |
+
def generate(
|
| 44 |
+
message: str,
|
| 45 |
+
chat_history: list[dict],
|
| 46 |
+
system_prompt: str = "",
|
| 47 |
+
max_new_tokens: int = DEFAULT_MAX_NEW_TOKENS,
|
| 48 |
+
temperature: float = 0.7,
|
| 49 |
+
top_p: float = 0.95,
|
| 50 |
+
top_k: int = 40,
|
| 51 |
+
repetition_penalty: float = 1.2,
|
| 52 |
+
) -> Iterator[str]:
|
| 53 |
+
# Build conversation
|
| 54 |
+
conversation = []
|
| 55 |
+
if system_prompt:
|
| 56 |
+
conversation.append({"role": "system", "content": system_prompt})
|
| 57 |
+
conversation += chat_history
|
| 58 |
+
conversation.append({"role": "user", "content": message})
|
| 59 |
+
|
| 60 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
|
| 61 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 62 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 63 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
| 64 |
+
input_ids = input_ids.to(model.device)
|
| 65 |
+
|
| 66 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 67 |
+
generate_kwargs = dict(
|
| 68 |
+
{"input_ids": input_ids},
|
| 69 |
+
streamer=streamer,
|
| 70 |
+
max_new_tokens=max_new_tokens,
|
| 71 |
+
do_sample=True,
|
| 72 |
+
top_p=top_p,
|
| 73 |
+
top_k=top_k,
|
| 74 |
+
temperature=temperature,
|
| 75 |
+
num_beams=1,
|
| 76 |
+
repetition_penalty=repetition_penalty,
|
| 77 |
+
)
|
| 78 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 79 |
+
t.start()
|
| 80 |
+
|
| 81 |
+
outputs = []
|
| 82 |
+
for text in streamer:
|
| 83 |
+
outputs.append(text)
|
| 84 |
+
yield "".join(outputs)
|
| 85 |
+
|
| 86 |
|
| 87 |
+
chat_interface = gr.ChatInterface(
|
| 88 |
+
fn=generate,
|
|
|
|
|
|
|
|
|
|
| 89 |
additional_inputs=[
|
| 90 |
+
gr.Textbox(label="System prompt", lines=6),
|
|
|
|
|
|
|
| 91 |
gr.Slider(
|
| 92 |
+
label="Max new tokens",
|
| 93 |
+
minimum=1,
|
| 94 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
| 95 |
+
step=1,
|
| 96 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
| 97 |
+
),
|
| 98 |
+
gr.Slider(
|
| 99 |
+
label="Temperature",
|
| 100 |
minimum=0.1,
|
| 101 |
+
maximum=4.0,
|
| 102 |
+
step=0.1,
|
| 103 |
+
value=0.7,
|
| 104 |
+
),
|
| 105 |
+
gr.Slider(
|
| 106 |
+
label="Top-p (nucleus sampling)",
|
| 107 |
+
minimum=0.05,
|
| 108 |
maximum=1.0,
|
| 109 |
+
step=0.05,
|
| 110 |
value=0.95,
|
| 111 |
+
),
|
| 112 |
+
gr.Slider(
|
| 113 |
+
label="Top-k",
|
| 114 |
+
minimum=1,
|
| 115 |
+
maximum=1000,
|
| 116 |
+
step=1,
|
| 117 |
+
value=40,
|
| 118 |
+
),
|
| 119 |
+
gr.Slider(
|
| 120 |
+
label="Repetition penalty",
|
| 121 |
+
minimum=1.0,
|
| 122 |
+
maximum=2.0,
|
| 123 |
step=0.05,
|
| 124 |
+
value=1.2,
|
| 125 |
),
|
| 126 |
],
|
| 127 |
+
stop_btn=None,
|
| 128 |
+
examples=[
|
| 129 |
+
[
|
| 130 |
+
"A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?"
|
| 131 |
+
],
|
| 132 |
+
[
|
| 133 |
+
"Claire makes a 3 egg omelet every morning for breakfast. How many dozens of eggs will she eat in 4 weeks?"
|
| 134 |
+
],
|
| 135 |
+
[
|
| 136 |
+
"James decides to run 3 sprints 3 times a week. He runs 60 meters each sprint. How many total meters does he run a week?"
|
| 137 |
+
],
|
| 138 |
+
],
|
| 139 |
+
cache_examples=False,
|
| 140 |
+
type="messages",
|
| 141 |
)
|
| 142 |
|
| 143 |
+
with gr.Blocks(css_paths="style.css", fill_height=True) as demo:
|
| 144 |
+
gr.Markdown(DESCRIPTION)
|
| 145 |
+
chat_interface.render()
|
| 146 |
+
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
+
demo.queue(max_size=20).launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
style.css
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
h1 {
|
| 2 |
+
text-align: center;
|
| 3 |
+
display: block;
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
#duplicate-button {
|
| 7 |
+
margin: auto;
|
| 8 |
+
color: white;
|
| 9 |
+
background: #1565c0;
|
| 10 |
+
border-radius: 100vh;
|
| 11 |
+
}
|
| 12 |
+
|