Spaces:
Sleeping
Sleeping
Upload Fooocus-Clean.py
Browse files- Fooocus-Clean.py +82 -0
Fooocus-Clean.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
# π Form-like Configuration Section - Easily customize your script settings here!
|
| 8 |
+
# -----------------------------------------------------------
|
| 9 |
+
# Server Configuration - Change these to match your server!
|
| 10 |
+
fooocus_host = 'http://192.168.50.136:7878' # π Host address for the Fooocus server
|
| 11 |
+
|
| 12 |
+
# π File Paths - Change these paths if your images are stored in a different location!
|
| 13 |
+
imgs_base_path = "./saved_images" # Define the base path for saving images
|
| 14 |
+
|
| 15 |
+
# Headers for HTTP Requests - Modify if needed!
|
| 16 |
+
headers = {
|
| 17 |
+
"accept": "application/json"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
# -----------------------------------------------------------
|
| 21 |
+
# π οΈ Utility Functions - These functions perform the core operations of the script.
|
| 22 |
+
|
| 23 |
+
# Function to encode images to base64 - This helps in preparing images for API requests.
|
| 24 |
+
def encode_image_to_base64(image_path):
|
| 25 |
+
with open(image_path, "rb") as image_file:
|
| 26 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 27 |
+
|
| 28 |
+
# Function to save images from API response - This saves the processed images locally.
|
| 29 |
+
def save_image_from_response(response, base_filename="generated-image"):
|
| 30 |
+
if not os.path.exists(imgs_base_path):
|
| 31 |
+
os.makedirs(imgs_base_path) # Create the directory if it does not exist
|
| 32 |
+
output_path = os.path.join(imgs_base_path, base_filename)
|
| 33 |
+
for i, item in enumerate(response, start=1):
|
| 34 |
+
if item.get('url'):
|
| 35 |
+
modified_url = item['url'].replace('http://127.0.0.1:7878', fooocus_host)
|
| 36 |
+
img_data = requests.get(modified_url).content
|
| 37 |
+
with open(f"{output_path}{i}.png", 'wb') as handler:
|
| 38 |
+
handler.write(img_data)
|
| 39 |
+
print(f"πΌοΈ Image saved as {output_path}{i}.png")
|
| 40 |
+
|
| 41 |
+
# Enhanced Gradio app for text and image prompt generation
|
| 42 |
+
def generate_image_from_prompt(prompt, image_file=None):
|
| 43 |
+
api_url = f"{fooocus_host}/v1/generation/text-to-image"
|
| 44 |
+
request_payload = {"prompt": prompt}
|
| 45 |
+
if image_file:
|
| 46 |
+
image_base64 = encode_image_to_base64(image_file.name)
|
| 47 |
+
request_payload["image_prompt"] = image_base64 # Add image prompt if provided
|
| 48 |
+
response = requests.post(api_url, json=request_payload, headers=headers)
|
| 49 |
+
if response.status_code == 200:
|
| 50 |
+
result = response.json()
|
| 51 |
+
if isinstance(result, list) and len(result) > 0:
|
| 52 |
+
save_image_from_response(result, "generated-image")
|
| 53 |
+
image_url = result[0].get("url") # Assuming the first item in the list contains the URL
|
| 54 |
+
if image_url:
|
| 55 |
+
modified_url = image_url.replace('http://127.0.0.1:7878', fooocus_host)
|
| 56 |
+
return f"<img src='{modified_url}'/>" # Return an HTML image tag with the modified URL
|
| 57 |
+
else:
|
| 58 |
+
return "Error: URL not found in response."
|
| 59 |
+
elif isinstance(result, dict):
|
| 60 |
+
save_image_from_response([result], "generated-image-single") # Save single image response
|
| 61 |
+
image_url = result.get("url")
|
| 62 |
+
if image_url:
|
| 63 |
+
modified_url = image_url.replace('http://127.0.0.1:7878', fooocus_host)
|
| 64 |
+
return f"<img src='{modified_url}'/>" # Return an HTML image tag with the modified URL
|
| 65 |
+
else:
|
| 66 |
+
return "Error: URL not found in response."
|
| 67 |
+
else:
|
| 68 |
+
return "Error: Unexpected response format. Please try again."
|
| 69 |
+
else:
|
| 70 |
+
return f"Error: Could not generate image. Status code: {response.status_code}"
|
| 71 |
+
|
| 72 |
+
iface = gr.Interface(
|
| 73 |
+
fn=generate_image_from_prompt,
|
| 74 |
+
inputs=[gr.Textbox(label="Enter your prompt"), gr.File(label="Upload an image prompt (optional)")],
|
| 75 |
+
outputs=gr.HTML(label="Generated Image"), # Adjusted to handle image URLs via HTML
|
| 76 |
+
title="Enhanced Image Generator",
|
| 77 |
+
description="Enter a prompt and optionally upload an image to generate an image using the API. Images are saved locally."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
iface.launch()
|
| 82 |
+
|