Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,34 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from contextlib import asynccontextmanager
|
| 4 |
import base64
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
@asynccontextmanager
|
| 9 |
async def lifespan(app: FastAPI):
|
| 10 |
-
|
| 11 |
lora_path = "./lora_file.pth"
|
| 12 |
if os.path.exists(lora_path):
|
| 13 |
with open(lora_path, "rb") as f:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
print("LoRA weights loaded and preprocessed successfully.")
|
| 17 |
else:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Yield control to the application
|
| 22 |
yield
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
print("Application shutting down.")
|
| 26 |
-
|
| 27 |
app = FastAPI(lifespan=lifespan)
|
| 28 |
|
| 29 |
-
# Middleware to handle CORS (optional, but useful for cross-origin requests)
|
| 30 |
-
app.add_middleware(
|
| 31 |
-
CORSMiddleware,
|
| 32 |
-
allow_origins=["*"], # Adjust for security
|
| 33 |
-
allow_credentials=True,
|
| 34 |
-
allow_methods=["*"],
|
| 35 |
-
allow_headers=["*"],
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
@app.post("/modify-prompt")
|
| 39 |
async def modify_prompt(prompt: str):
|
| 40 |
-
|
| 41 |
if lora_weights is None:
|
| 42 |
raise HTTPException(status_code=500, detail="LoRA weights not loaded.")
|
| 43 |
-
|
| 44 |
# Combine prompt with preprocessed LoRA data
|
| 45 |
extended_prompt = {
|
| 46 |
"prompt": prompt,
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
|
|
|
|
|
|
| 2 |
import base64
|
| 3 |
import os
|
| 4 |
+
from contextlib import asynccontextmanager
|
| 5 |
|
| 6 |
+
# Define the lifespan context manager
|
| 7 |
@asynccontextmanager
|
| 8 |
async def lifespan(app: FastAPI):
|
| 9 |
+
lora_weights = None
|
| 10 |
lora_path = "./lora_file.pth"
|
| 11 |
if os.path.exists(lora_path):
|
| 12 |
with open(lora_path, "rb") as f:
|
| 13 |
+
lora_weights = base64.b64encode(f.read()).decode("utf-8")
|
| 14 |
+
print("LoRA weights loaded and preprocessed successfully.")
|
|
|
|
| 15 |
else:
|
| 16 |
+
raise HTTPException(status_code=500, detail="LoRA file not found.")
|
| 17 |
+
|
| 18 |
+
# Yield control back to the FastAPI app during runtime
|
|
|
|
| 19 |
yield
|
| 20 |
+
|
| 21 |
+
# Cleanup or shutdown logic here if necessary
|
| 22 |
+
print("App is shutting down.")
|
| 23 |
|
| 24 |
+
# Create the FastAPI app with the lifespan context manager
|
|
|
|
|
|
|
| 25 |
app = FastAPI(lifespan=lifespan)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
@app.post("/modify-prompt")
|
| 28 |
async def modify_prompt(prompt: str):
|
| 29 |
+
global lora_weights
|
| 30 |
if lora_weights is None:
|
| 31 |
raise HTTPException(status_code=500, detail="LoRA weights not loaded.")
|
|
|
|
| 32 |
# Combine prompt with preprocessed LoRA data
|
| 33 |
extended_prompt = {
|
| 34 |
"prompt": prompt,
|