Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
-
# Load the model and tokenizer
|
6 |
model_name = "meta-llama/Llama-3.1-8B-Instruct"
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
# Initialize FastAPI
|
11 |
app = FastAPI()
|
@@ -16,7 +17,10 @@ class Prompt(BaseModel):
|
|
16 |
|
17 |
@app.post("/generate")
|
18 |
def generate_text(prompt: Prompt):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
|
|
5 |
model_name = "meta-llama/Llama-3.1-8B-Instruct"
|
6 |
+
|
7 |
+
# Use the Hugging Face token
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=True)
|
10 |
|
11 |
# Initialize FastAPI
|
12 |
app = FastAPI()
|
|
|
17 |
|
18 |
@app.post("/generate")
|
19 |
def generate_text(prompt: Prompt):
|
20 |
+
try:
|
21 |
+
inputs = tokenizer(prompt.text, return_tensors="pt")
|
22 |
+
outputs = model.generate(**inputs, max_length=100)
|
23 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
+
return {"generated_text": generated_text}
|
25 |
+
except Exception as e:
|
26 |
+
raise HTTPException(status_code=500, detail=f"Error generating text: {str(e)}")
|