File size: 1,196 Bytes
37f9623 bb2b425 37f9623 30fa026 dd28fa5 0cefee0 994ab9f 30fa026 0cefee0 37f9623 cec3202 37f9623 cec3202 30fa026 994ab9f 0ab8732 37f9623 cec3202 37f9623 0cefee0 37f9623 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
from fastapi import FastAPI, HTTPException
import base64
import os
from contextlib import asynccontextmanager
# Global variable to hold the LoRA weights
lora_weights = None
# Lifespan context manager to handle the application startup
@asynccontextmanager
async def lifespan(app: FastAPI):
global lora_weights
lora_path = "./lora_file.pth" # Ensure the correct file name
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
# Cleanup if necessary (but not required in this case)
# Initialize FastAPI app with lifespan context
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
|