Tiago Caldeira
commited on
Commit
·
6b5a511
1
Parent(s):
7c94b16
different approach using unsloth model
Browse files
__app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
| 4 |
+
import textwrap
|
| 5 |
+
|
| 6 |
+
model_id = "unsloth/gemma-3n-E2B-it-unsloth-bnb-4bit"
|
| 7 |
+
|
| 8 |
+
# Load tokenizer
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
|
| 11 |
+
# Load model in full precision on CPU — no bitsandbytes
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
model_id,
|
| 14 |
+
device_map="cpu", # Force CPU
|
| 15 |
+
torch_dtype=torch.float32, # Use FP32 to ensure CPU compatibility
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
model.eval()
|
| 19 |
+
|
| 20 |
+
# Helper to format response nicely
|
| 21 |
+
def print_response(text: str) -> str:
|
| 22 |
+
return "\n".join(textwrap.fill(line, 100) for line in text.split("\n"))
|
| 23 |
+
|
| 24 |
+
# Inference function for Gradio
|
| 25 |
+
def predict_text(system_prompt: str, user_prompt: str) -> str:
|
| 26 |
+
messages = [
|
| 27 |
+
{"role": "system", "content": [{"type": "text", "text": system_prompt.strip()}]},
|
| 28 |
+
{"role": "user", "content": [{"type": "text", "text": user_prompt.strip()}]},
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
inputs = tokenizer.apply_chat_template(
|
| 32 |
+
messages,
|
| 33 |
+
add_generation_prompt=True,
|
| 34 |
+
tokenize=True,
|
| 35 |
+
return_dict=True,
|
| 36 |
+
return_tensors="pt"
|
| 37 |
+
).to("cpu")
|
| 38 |
+
|
| 39 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 40 |
+
|
| 41 |
+
with torch.inference_mode():
|
| 42 |
+
output = model.generate(
|
| 43 |
+
**inputs,
|
| 44 |
+
max_new_tokens=300,
|
| 45 |
+
do_sample=False,
|
| 46 |
+
use_cache=False # Important for CPU compatibility
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
generated = output[0][input_len:]
|
| 50 |
+
decoded = tokenizer.decode(generated, skip_special_tokens=True)
|
| 51 |
+
return print_response(decoded)
|
| 52 |
+
|
| 53 |
+
# Gradio UI
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn=predict_text,
|
| 56 |
+
inputs=[
|
| 57 |
+
gr.Textbox(lines=2, label="System Prompt", value="You are a helpful assistant."),
|
| 58 |
+
gr.Textbox(lines=4, label="User Prompt", placeholder="Ask something..."),
|
| 59 |
+
],
|
| 60 |
+
outputs=gr.Textbox(label="Gemma 3n Response"),
|
| 61 |
+
title="Gemma 3n Chat (CPU-friendly)",
|
| 62 |
+
description="Lightweight CPU-only chatbot using a quantized Gemma 3n model.",
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|
app.py
CHANGED
|
@@ -1,66 +1,63 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
-
import textwrap
|
| 5 |
|
| 6 |
model_id = "unsloth/gemma-3n-E2B-it-unsloth-bnb-4bit"
|
| 7 |
|
| 8 |
# Load tokenizer
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
|
| 11 |
-
# Load model
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_id,
|
| 14 |
-
|
| 15 |
-
torch_dtype=torch.float32, # Use FP32 to ensure CPU compatibility
|
| 16 |
)
|
| 17 |
-
|
| 18 |
model.eval()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def
|
| 22 |
-
return "\n".join(textwrap.fill(line, 100) for line in text.split("\n"))
|
| 23 |
-
|
| 24 |
-
# Inference function for Gradio
|
| 25 |
-
def predict_text(system_prompt: str, user_prompt: str) -> str:
|
| 26 |
messages = [
|
| 27 |
-
{
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
]
|
| 30 |
|
| 31 |
inputs = tokenizer.apply_chat_template(
|
| 32 |
messages,
|
| 33 |
add_generation_prompt=True,
|
| 34 |
-
tokenize=True,
|
| 35 |
-
return_dict=True,
|
| 36 |
return_tensors="pt"
|
| 37 |
).to("cpu")
|
| 38 |
|
| 39 |
input_len = inputs["input_ids"].shape[-1]
|
| 40 |
|
| 41 |
with torch.inference_mode():
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
do_sample=False,
|
| 46 |
-
use_cache=False
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
decoded = tokenizer.decode(
|
| 51 |
-
return
|
| 52 |
|
| 53 |
# Gradio UI
|
| 54 |
demo = gr.Interface(
|
| 55 |
-
fn=
|
| 56 |
-
inputs=
|
| 57 |
-
gr.Textbox(lines=2, label="System Prompt", value="You are a helpful assistant."),
|
| 58 |
-
gr.Textbox(lines=4, label="User Prompt", placeholder="Ask something..."),
|
| 59 |
-
],
|
| 60 |
outputs=gr.Textbox(label="Gemma 3n Response"),
|
| 61 |
-
title="Gemma 3n
|
| 62 |
-
description="
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 4 |
|
| 5 |
model_id = "unsloth/gemma-3n-E2B-it-unsloth-bnb-4bit"
|
| 6 |
|
| 7 |
# Load tokenizer
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
|
| 10 |
+
# Load model on CPU
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
model_id,
|
| 13 |
+
torch_dtype=torch.float32,
|
|
|
|
| 14 |
)
|
| 15 |
+
model.to("cpu")
|
| 16 |
model.eval()
|
| 17 |
|
| 18 |
+
# Minimal generation: single user prompt, static system
|
| 19 |
+
def generate_response(user_prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
messages = [
|
| 21 |
+
{
|
| 22 |
+
"role": "system",
|
| 23 |
+
"content": [{"type": "text", "text": "You are a helpful assistant."}]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"role": "user",
|
| 27 |
+
"content": [{"type": "text", "text": user_prompt.strip()}]
|
| 28 |
+
}
|
| 29 |
]
|
| 30 |
|
| 31 |
inputs = tokenizer.apply_chat_template(
|
| 32 |
messages,
|
| 33 |
add_generation_prompt=True,
|
|
|
|
|
|
|
| 34 |
return_tensors="pt"
|
| 35 |
).to("cpu")
|
| 36 |
|
| 37 |
input_len = inputs["input_ids"].shape[-1]
|
| 38 |
|
| 39 |
with torch.inference_mode():
|
| 40 |
+
outputs = model.generate(
|
| 41 |
+
input_ids=inputs["input_ids"],
|
| 42 |
+
attention_mask=inputs["attention_mask"],
|
| 43 |
+
max_new_tokens=100,
|
| 44 |
do_sample=False,
|
| 45 |
+
use_cache=False
|
| 46 |
)
|
| 47 |
|
| 48 |
+
generated_tokens = outputs[0][input_len:]
|
| 49 |
+
decoded = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 50 |
+
return decoded.strip()
|
| 51 |
|
| 52 |
# Gradio UI
|
| 53 |
demo = gr.Interface(
|
| 54 |
+
fn=generate_response,
|
| 55 |
+
inputs=gr.Textbox(lines=3, label="Enter your question"),
|
|
|
|
|
|
|
|
|
|
| 56 |
outputs=gr.Textbox(label="Gemma 3n Response"),
|
| 57 |
+
title="🧪 Simple Gemma 3n Demo (CPU)",
|
| 58 |
+
description="Test the Gemma 3n model with minimal output. Max 100 tokens.",
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch()
|
| 63 |
+
|