Testing3 / app.py
DonImages's picture
Update app.py
cec3202 verified
raw
history blame
1.18 kB
from fastapi import FastAPI, HTTPException
import base64
import os
from contextlib import asynccontextmanager
# Define the lifespan context manager
@asynccontextmanager
async def lifespan(app: FastAPI):
lora_weights = None
lora_path = "./lora_file.pth"
if os.path.exists(lora_path):
with open(lora_path, "rb") as f:
lora_weights = base64.b64encode(f.read()).decode("utf-8")
print("LoRA weights loaded and preprocessed successfully.")
else:
raise HTTPException(status_code=500, detail="LoRA file not found.")
# Yield control back to the FastAPI app during runtime
yield
# Cleanup or shutdown logic here if necessary
print("App is shutting down.")
# Create the FastAPI app with the lifespan context manager
app = FastAPI(lifespan=lifespan)
@app.post("/modify-prompt")
async def modify_prompt(prompt: str):
global lora_weights
if lora_weights is None:
raise HTTPException(status_code=500, detail="LoRA weights not loaded.")
# Combine prompt with preprocessed LoRA data
extended_prompt = {
"prompt": prompt,
"lora": lora_weights
}
return extended_prompt