Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import snapshot_download
|
| 4 |
+
import torch
|
| 5 |
+
from diffusers import FluxPipeline, FluxTransformer2DModel
|
| 6 |
+
|
| 7 |
+
# Install dependencies
|
| 8 |
+
os.system("pip install huggingface-hub transformers diffusers accelerate safetensors torch gradio")
|
| 9 |
+
|
| 10 |
+
# Download PosterCraft model
|
| 11 |
+
model_dir = "models/postercraft/postercraft_rl"
|
| 12 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 13 |
+
snapshot_download(repo_id="PosterCraft/PosterCraft-v1_RL", local_dir=model_dir, resume_download=True, token=hf_token)
|
| 14 |
+
|
| 15 |
+
# Load model
|
| 16 |
+
pipeline_id = "black-forest-labs/FLUX.1-dev"
|
| 17 |
+
dtype = torch.bfloat16
|
| 18 |
+
pipe = FluxPipeline.from_pretrained(pipeline_id, torch_dtype=dtype, token=hf_token)
|
| 19 |
+
pipe.transformer = FluxTransformer2DModel.from_pretrained(model_dir, torch_dtype=dtype, token=hf_token)
|
| 20 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
|
| 22 |
+
# Generate function
|
| 23 |
+
def generate_poster(prompt):
|
| 24 |
+
try:
|
| 25 |
+
image = pipe(prompt).images[0]
|
| 26 |
+
return image
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
# Gradio UI
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=generate_poster,
|
| 33 |
+
inputs="text",
|
| 34 |
+
outputs="image",
|
| 35 |
+
title="Test Poster Generator"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
iface.launch()
|