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