File size: 1,526 Bytes
3cb97b6
 
2ef1e0a
1782685
f12ea3a
3cb97b6
 
f12ea3a
 
 
 
 
3cb97b6
 
f12ea3a
 
 
3cb97b6
 
f12ea3a
3cb97b6
 
 
 
 
 
 
f12ea3a
3cb97b6
 
 
 
0d6a629
3cb97b6
 
0d6a629
3cb97b6
 
f12ea3a
3cb97b6
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch

# Load model and tokenizer with trust_remote_code=True
model_id = "PowerInfer/SmallThinker-21BA3B-Instruct"

tokenizer = AutoTokenizer.from_pretrained(
    model_id,
    trust_remote_code=True  # Required for models with custom code
)

model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="cpu",              # Run on CPU
    torch_dtype=torch.float32,     # Use float32 on CPU
    trust_remote_code=True         # Allow custom code execution
)

# Create text generation pipeline
generator = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    device=-1  # CPU
)

# Define the chat function
def chat(prompt, max_new_tokens=256, temperature=0.7):
    output = generator(
        prompt,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id
    )
    return output[0]["generated_text"]

# Launch the Gradio interface
gr.Interface(
    fn=chat,
    inputs=[
        gr.Textbox(label="Prompt", lines=4, placeholder="Ask anything..."),
        gr.Slider(32, 512, value=256, step=16, label="Max New Tokens"),
        gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
    ],
    outputs=gr.Textbox(label="Response"),
    title="💬 SmallThinker-21BA3B-Instruct",
    description="Run PowerInfer/SmallThinker-21BA3B-Instruct locally on CPU using Hugging Face + Gradio"
).launch()