Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!pip install -q gradio_client
|
| 2 |
+
!pip install -q gradio
|
| 3 |
+
import warnings
|
| 4 |
+
warnings.filterwarnings("ignore")
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gradio_client import Client, handle_file
|
| 7 |
+
|
| 8 |
+
def generate_image(prompt, image_url=None, image_file=None):
|
| 9 |
+
# Initialize the client
|
| 10 |
+
client = Client("yanze/PuLID-FLUX")
|
| 11 |
+
|
| 12 |
+
# Determine input image
|
| 13 |
+
if image_url:
|
| 14 |
+
id_image = handle_file(image_url)
|
| 15 |
+
elif image_file:
|
| 16 |
+
id_image = handle_file(image_file.name)
|
| 17 |
+
else:
|
| 18 |
+
return "Error: Please provide an image URL or upload an image file."
|
| 19 |
+
|
| 20 |
+
# Predict
|
| 21 |
+
try:
|
| 22 |
+
result = client.predict(
|
| 23 |
+
prompt=prompt,
|
| 24 |
+
id_image=id_image,
|
| 25 |
+
start_step=0,
|
| 26 |
+
guidance=2,
|
| 27 |
+
seed="-1",
|
| 28 |
+
true_cfg=1,
|
| 29 |
+
width=896,
|
| 30 |
+
height=1152,
|
| 31 |
+
num_steps=20,
|
| 32 |
+
id_weight=1,
|
| 33 |
+
neg_prompt="bad quality, worst quality, text, signature, watermark, extra limbs",
|
| 34 |
+
timestep_to_start_cfg=1,
|
| 35 |
+
max_sequence_length=128,
|
| 36 |
+
api_name="/generate_image"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Extract the base URL and file path
|
| 40 |
+
base_url = "https://yanze-pulid-flux.hf.space/file="
|
| 41 |
+
file_path = result[0] # The first element contains the file path of the primary result
|
| 42 |
+
full_url = f"{base_url}{file_path}"
|
| 43 |
+
|
| 44 |
+
return full_url
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error during prediction: {str(e)}"
|
| 47 |
+
|
| 48 |
+
# Gradio interface
|
| 49 |
+
def gradio_interface():
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("# Image Generation App\nUpload an image or provide an image URL, and enter a prompt to generate a new image.")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt, e.g., portrait, color, cinematic")
|
| 55 |
+
image_url = gr.Textbox(label="Image URL", placeholder="Enter the image URL (optional)")
|
| 56 |
+
|
| 57 |
+
image_file = gr.File(label="Upload Image", file_types=["image"])
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
submit_button = gr.Button("Generate Image")
|
| 61 |
+
|
| 62 |
+
output = gr.Textbox(label="Generated Image URL")
|
| 63 |
+
output_image = gr.Image(label="Generated Image")
|
| 64 |
+
|
| 65 |
+
def process(prompt, image_url, image_file):
|
| 66 |
+
result_url = generate_image(prompt, image_url, image_file)
|
| 67 |
+
if result_url.startswith("http"):
|
| 68 |
+
return result_url, result_url
|
| 69 |
+
else:
|
| 70 |
+
return result_url, None
|
| 71 |
+
|
| 72 |
+
submit_button.click(
|
| 73 |
+
fn=process,
|
| 74 |
+
inputs=[prompt, image_url, image_file],
|
| 75 |
+
outputs=[output, output_image]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return demo
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo = gradio_interface()
|
| 82 |
+
demo.launch()
|