Testing3 / app.py
DonImages's picture
Update app.py
5dbbf26 verified
raw
history blame
1.34 kB
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import base64
import os
app = FastAPI()
# Middleware to handle CORS (optional, but useful if Testing4 calls this API)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust as needed for security
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Load LoRA weights globally
lora_weights = None
@app.on_event("startup")
async def startup_event():
global lora_weights
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
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 HTTPException(status_code=500, detail="LoRA file not found.")
@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