usag1e commited on
Commit
6cfe633
·
verified ·
1 Parent(s): 07e7927

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -1,26 +1,23 @@
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()
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
- 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)}")
 
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}