Spaces:
Sleeping
Sleeping
from fastapi import FastAPI | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
import torch | |
app = FastAPI() | |
# Load model and tokenizer | |
tokenizer = AutoTokenizer.from_pretrained("unsloth/Llama-3.2-1B-Instruct") | |
model = AutoModelForCausalLM.from_pretrained("unsloth/Llama-3.2-1B-Instruct").to("cpu") # Gunakan CPU karena HF Spaces gratis hanya menyediakan CPU | |
def home(): | |
return {"message": "FastAPI running with Llama-3.2-1B-Instruct"} | |
def generate_text(prompt: str): | |
inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Gunakan CPU | |
output = model.generate(**inputs, max_length=200) | |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True) | |
return {"generated_text": generated_text} | |