Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,32 @@
|
|
| 1 |
-
from
|
| 2 |
-
import os
|
| 3 |
import base64
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
class LoRAInferenceWrapper:
|
| 7 |
-
def __init__(self, model_id, token):
|
| 8 |
-
# Initialize the InferenceClient
|
| 9 |
-
self.client = InferenceClient(model_id, token=token)
|
| 10 |
-
|
| 11 |
-
def load_lora_weights(self):
|
| 12 |
-
# Define the path to the LoRA model
|
| 13 |
-
lora_model_path = "./lora.model.pth" # Update to the actual file name
|
| 14 |
-
|
| 15 |
-
# Check if the file exists at the given path
|
| 16 |
-
if os.path.exists(lora_model_path):
|
| 17 |
-
print(f"Found LoRA model at: {lora_model_path}")
|
| 18 |
-
with open(lora_model_path, 'rb') as f:
|
| 19 |
-
return f.read() # Load the file content
|
| 20 |
-
else:
|
| 21 |
-
raise FileNotFoundError(f"LoRA model not found at path: {lora_model_path}")
|
| 22 |
-
|
| 23 |
-
def preprocess_lora_weights(self, lora_weights):
|
| 24 |
-
# Preprocess the LoRA weights (e.g., Base64 encoding for JSON compatibility)
|
| 25 |
-
return base64.b64encode(lora_weights).decode("utf-8")
|
| 26 |
-
|
| 27 |
-
def generate_with_lora(self, prompt):
|
| 28 |
-
# Load and preprocess the LoRA weights
|
| 29 |
-
lora_weights = self.load_lora_weights()
|
| 30 |
-
processed_lora = self.preprocess_lora_weights(lora_weights)
|
| 31 |
-
|
| 32 |
-
# Combine the prompt and LoRA data as a single input
|
| 33 |
-
extended_prompt = json.dumps({
|
| 34 |
-
"prompt": prompt,
|
| 35 |
-
"lora": processed_lora
|
| 36 |
-
})
|
| 37 |
-
|
| 38 |
-
# Generate the output using the InferenceClient
|
| 39 |
-
result = self.client.text_to_image(prompt=extended_prompt)
|
| 40 |
-
return result
|
| 41 |
-
|
| 42 |
-
# Example usage
|
| 43 |
-
model_id = "stabilityai/stable-diffusion-3.5-large"
|
| 44 |
-
token = "hf_YOUR_HF_API_TOKEN" # Replace with your Hugging Face token
|
| 45 |
-
|
| 46 |
-
# Initialize the wrapper
|
| 47 |
-
lora_client = LoRAInferenceWrapper(model_id, token)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
|
|
|
| 2 |
import base64
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Load LoRA weights on startup
|
| 8 |
+
lora_weights = None
|
| 9 |
+
|
| 10 |
+
@app.on_event("startup")
|
| 11 |
+
def load_lora_weights():
|
| 12 |
+
global lora_weights
|
| 13 |
+
lora_path = "./lora_file.pth"
|
| 14 |
+
if os.path.exists(lora_path):
|
| 15 |
+
with open(lora_path, "rb") as f:
|
| 16 |
+
# Base64 encode the LoRA weights for easy JSON transmission
|
| 17 |
+
lora_weights = base64.b64encode(f.read()).decode("utf-8")
|
| 18 |
+
print("LoRA weights loaded and preprocessed successfully.")
|
| 19 |
+
else:
|
| 20 |
+
raise HTTPException(status_code=500, detail="LoRA file not found.")
|
| 21 |
+
|
| 22 |
+
@app.post("/modify-prompt")
|
| 23 |
+
async def modify_prompt(prompt: str):
|
| 24 |
+
global lora_weights
|
| 25 |
+
if lora_weights is None:
|
| 26 |
+
raise HTTPException(status_code=500, detail="LoRA weights not loaded.")
|
| 27 |
+
# Combine prompt with preprocessed LoRA data
|
| 28 |
+
extended_prompt = {
|
| 29 |
+
"prompt": prompt,
|
| 30 |
+
"lora": lora_weights
|
| 31 |
+
}
|
| 32 |
+
return extended_prompt
|