tt
Browse files
app.py
CHANGED
|
@@ -2,17 +2,22 @@ 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
-
device_map="cpu",
|
| 12 |
-
torch_dtype=torch.float32
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
-
# Create generation pipeline
|
| 16 |
generator = pipeline(
|
| 17 |
"text-generation",
|
| 18 |
model=model,
|
|
@@ -20,7 +25,7 @@ generator = pipeline(
|
|
| 20 |
device=-1 # CPU
|
| 21 |
)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
def chat(prompt, max_new_tokens=256, temperature=0.7):
|
| 25 |
output = generator(
|
| 26 |
prompt,
|
|
@@ -31,7 +36,7 @@ def chat(prompt, max_new_tokens=256, temperature=0.7):
|
|
| 31 |
)
|
| 32 |
return output[0]["generated_text"]
|
| 33 |
|
| 34 |
-
# Launch Gradio
|
| 35 |
gr.Interface(
|
| 36 |
fn=chat,
|
| 37 |
inputs=[
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load model and tokenizer with trust_remote_code=True
|
| 6 |
model_id = "PowerInfer/SmallThinker-21BA3B-Instruct"
|
| 7 |
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
trust_remote_code=True # Required for models with custom code
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
model_id,
|
| 15 |
+
device_map="cpu", # Run on CPU
|
| 16 |
+
torch_dtype=torch.float32, # Use float32 on CPU
|
| 17 |
+
trust_remote_code=True # Allow custom code execution
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Create text generation pipeline
|
| 21 |
generator = pipeline(
|
| 22 |
"text-generation",
|
| 23 |
model=model,
|
|
|
|
| 25 |
device=-1 # CPU
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Define the chat function
|
| 29 |
def chat(prompt, max_new_tokens=256, temperature=0.7):
|
| 30 |
output = generator(
|
| 31 |
prompt,
|
|
|
|
| 36 |
)
|
| 37 |
return output[0]["generated_text"]
|
| 38 |
|
| 39 |
+
# Launch the Gradio interface
|
| 40 |
gr.Interface(
|
| 41 |
fn=chat,
|
| 42 |
inputs=[
|