Testing3 / app.py
DonImages's picture
Update app.py
0ab8732 verified
raw
history blame
1.55 kB
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import base64
import os
# Create the FastAPI app
@asynccontextmanager
async def lifespan(app: FastAPI):
# Load LoRA weights during app startup
lora_path = "./lora_file.pth"
if os.path.exists(lora_path):
with open(lora_path, "rb") as f:
# Base64 encode the LoRA weights for easy JSON transmission
app.state.lora_weights = base64.b64encode(f.read()).decode("utf-8")
print("LoRA weights loaded and preprocessed successfully.")
else:
print("LoRA file not found during startup.")
raise RuntimeError("LoRA file not found.")
# Yield control to the application
yield
# Perform any cleanup (if needed) here
print("Application shutting down.")
app = FastAPI(lifespan=lifespan)
# Middleware to handle CORS (optional, but useful for cross-origin requests)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust for security
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/modify-prompt")
async def modify_prompt(prompt: str):
lora_weights = getattr(app.state, "lora_weights", None)
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