Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,12 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Load model and tokenizer
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained("unsloth/Llama-3.2-1B-Instruct", cache_dir="./hf_cache")
|
| 13 |
-
model = AutoModelForCausalLM.from_pretrained("unsloth/Llama-3.2-1B-Instruct", cache_dir="./hf_cache").to("cpu")
|
| 14 |
|
| 15 |
@app.get("/")
|
| 16 |
def home():
|
|
@@ -18,7 +14,7 @@ def home():
|
|
| 18 |
|
| 19 |
@app.post("/generate")
|
| 20 |
def generate_text(prompt: str):
|
| 21 |
-
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 22 |
-
output = model.generate(**inputs, max_length=
|
| 23 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 24 |
return {"generated_text": generated_text}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# Load model and tokenizer tanpa mendefinisikan cache_dir
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("unsloth/Llama-3.2-1B-Instruct")
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained("unsloth/Llama-3.2-1B-Instruct").to("cpu")
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
def home():
|
|
|
|
| 14 |
|
| 15 |
@app.post("/generate")
|
| 16 |
def generate_text(prompt: str):
|
| 17 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 18 |
+
output = model.generate(**inputs, max_length=300)
|
| 19 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 20 |
return {"generated_text": generated_text}
|