tt
Browse files- app.py +41 -31
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,35 +1,45 @@
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
temperature=temperature,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
do_sample=True
|
| 20 |
)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_id = "PowerInfer/SmallThinker-21BA3B-Instruct"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_id,
|
| 11 |
+
device_map="cpu", # Force CPU
|
| 12 |
+
torch_dtype=torch.float32
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Create generation pipeline
|
| 16 |
+
generator = pipeline(
|
| 17 |
+
"text-generation",
|
| 18 |
+
model=model,
|
| 19 |
+
tokenizer=tokenizer,
|
| 20 |
+
device=-1 # CPU
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Inference function
|
| 24 |
+
def chat(prompt, max_new_tokens=256, temperature=0.7):
|
| 25 |
+
output = generator(
|
| 26 |
+
prompt,
|
| 27 |
+
max_new_tokens=max_new_tokens,
|
| 28 |
temperature=temperature,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
pad_token_id=tokenizer.eos_token_id
|
|
|
|
| 31 |
)
|
| 32 |
+
return output[0]["generated_text"]
|
| 33 |
+
|
| 34 |
+
# Launch Gradio app
|
| 35 |
+
gr.Interface(
|
| 36 |
+
fn=chat,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Textbox(label="Prompt", lines=4, placeholder="Ask anything..."),
|
| 39 |
+
gr.Slider(32, 512, value=256, step=16, label="Max New Tokens"),
|
| 40 |
+
gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
|
| 41 |
+
],
|
| 42 |
+
outputs=gr.Textbox(label="Response"),
|
| 43 |
+
title="💬 SmallThinker-21BA3B-Instruct",
|
| 44 |
+
description="Run PowerInfer/SmallThinker-21BA3B-Instruct locally on CPU using Hugging Face + Gradio"
|
| 45 |
+
).launch()
|
|
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
gradio>=4.44.0
|
| 2 |
-
transformers>=4.40.0
|
| 3 |
torch>=2.1.0
|
|
|
|
| 4 |
accelerate>=0.27.0
|
| 5 |
sentencepiece # required for some tokenizers
|
| 6 |
safetensors # faster & safer model loading
|
| 7 |
-
hf_xet
|
|
|
|
| 1 |
gradio>=4.44.0
|
|
|
|
| 2 |
torch>=2.1.0
|
| 3 |
+
transformers==4.53.3
|
| 4 |
accelerate>=0.27.0
|
| 5 |
sentencepiece # required for some tokenizers
|
| 6 |
safetensors # faster & safer model loading
|
| 7 |
+
hf_xet
|