Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- app/main.py +17 -41
app/main.py
CHANGED
|
@@ -1,52 +1,28 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp"
|
| 4 |
-
os.environ["HF_HUB_CACHE"] = "/tmp"
|
| 5 |
-
|
| 6 |
-
from fastapi import FastAPI, HTTPException
|
| 7 |
-
from pydantic import BaseModel
|
| 8 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 9 |
import torch
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
-
|
| 16 |
device_map="auto",
|
| 17 |
-
torch_dtype=torch.float16
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
model=model,
|
| 23 |
-
tokenizer=tokenizer,
|
| 24 |
-
device=0 if torch.cuda.is_available() else -1
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
app = FastAPI()
|
| 28 |
-
|
| 29 |
-
class PromptRequest(BaseModel):
|
| 30 |
-
prompt: str
|
| 31 |
-
max_tokens: int = 512
|
| 32 |
-
temperature: float = 0.7
|
| 33 |
-
top_p: float = 0.95
|
| 34 |
|
| 35 |
@app.get("/")
|
| 36 |
def read_root():
|
| 37 |
-
return {"message": "
|
| 38 |
|
| 39 |
-
@app.
|
| 40 |
-
def generate(
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
request.prompt,
|
| 44 |
-
max_new_tokens=request.max_tokens,
|
| 45 |
-
temperature=request.temperature,
|
| 46 |
-
top_p=request.top_p,
|
| 47 |
-
do_sample=True,
|
| 48 |
-
pad_token_id=tokenizer.eos_token_id
|
| 49 |
-
)
|
| 50 |
-
return {"response": result[0]["generated_text"]}
|
| 51 |
-
except Exception as e:
|
| 52 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Load tokenizer and model
|
| 8 |
+
model_name = "ayureasehealthcare/llama3-ayurveda-text-v4" # replace with your actual model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
|
|
|
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
model_name,
|
| 13 |
device_map="auto",
|
| 14 |
+
torch_dtype=torch.float16,
|
| 15 |
+
trust_remote_code=True # necessary for mllama / Unsloth models
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# Create pipeline
|
| 19 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.get("/")
|
| 22 |
def read_root():
|
| 23 |
+
return {"message": "Ayurveda LLM is running!"}
|
| 24 |
|
| 25 |
+
@app.get("/generate/")
|
| 26 |
+
def generate(text: str):
|
| 27 |
+
output = pipe(text, max_new_tokens=100, do_sample=True, temperature=0.7)
|
| 28 |
+
return {"output": output[0]["generated_text"]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|