Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "UniversalComputingResearch/Atom2.7m"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
+
MODEL_ID,
|
| 9 |
+
trust_remote_code=True,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
MODEL_ID,
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
).eval()
|
| 16 |
+
|
| 17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
model.to(device)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def generate(prompt, max_new_tokens, temperature, do_sample):
|
| 22 |
+
if not prompt.strip():
|
| 23 |
+
return "Enter a prompt first."
|
| 24 |
+
|
| 25 |
+
inputs = tokenizer(
|
| 26 |
+
prompt,
|
| 27 |
+
return_tensors="pt",
|
| 28 |
+
add_special_tokens=False,
|
| 29 |
+
).to(device)
|
| 30 |
+
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
output_ids = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_new_tokens=int(max_new_tokens),
|
| 35 |
+
do_sample=bool(do_sample),
|
| 36 |
+
temperature=float(temperature) if do_sample else None,
|
| 37 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
examples = [
|
| 44 |
+
["12 + 34 =", 4, 1.0, False],
|
| 45 |
+
["7 + 8 =", 3, 1.0, False],
|
| 46 |
+
["25 - 9 =", 4, 1.0, False],
|
| 47 |
+
["3 * 6 =", 4, 1.0, False],
|
| 48 |
+
["The capital of France is", 12, 0.8, True],
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
description = """
|
| 52 |
+
Atom2.7m is a tiny causal language model for text continuation, with arithmetic-aware handling for numeric spans.
|
| 53 |
+
|
| 54 |
+
It is not an instruction-tuned chatbot. It works best with short continuation prompts such as `12 + 34 =`.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=generate,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Textbox(
|
| 61 |
+
label="Prompt",
|
| 62 |
+
value="12 + 34 =",
|
| 63 |
+
lines=3,
|
| 64 |
+
),
|
| 65 |
+
gr.Slider(
|
| 66 |
+
minimum=1,
|
| 67 |
+
maximum=64,
|
| 68 |
+
value=8,
|
| 69 |
+
step=1,
|
| 70 |
+
label="Max new tokens",
|
| 71 |
+
),
|
| 72 |
+
gr.Slider(
|
| 73 |
+
minimum=0.1,
|
| 74 |
+
maximum=2.0,
|
| 75 |
+
value=1.0,
|
| 76 |
+
step=0.1,
|
| 77 |
+
label="Temperature",
|
| 78 |
+
),
|
| 79 |
+
gr.Checkbox(
|
| 80 |
+
value=False,
|
| 81 |
+
label="Sample instead of greedy decoding",
|
| 82 |
+
),
|
| 83 |
+
],
|
| 84 |
+
outputs=gr.Textbox(
|
| 85 |
+
label="Model output",
|
| 86 |
+
lines=6,
|
| 87 |
+
),
|
| 88 |
+
title="Atom2.7m Arithmetic Demo",
|
| 89 |
+
description=description,
|
| 90 |
+
examples=examples,
|
| 91 |
+
allow_flagging="never",
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch()
|