Last active
December 15, 2023 08:57
-
-
Save truevis/9cd2c976fa1af61973e67beeca938578 to your computer and use it in GitHub Desktop.
This Python script harnesses the power of Gradio's client API to transform text prompts into images. It's designed to connect to a specific Hugging Face model, request image generation based on detailed text and parameter inputs, then neatly organize the resulting files. It's like a tidy butler for your AI-generated art, minus the white gloves.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gradio_client import Client | |
import os | |
import shutil | |
# https://huggingface.co/spaces/JingyeChen22/TextDiffuser-2 | |
destination_directory = 'd:\\downloads\\' | |
client = Client("https://jingyechen22-textdiffuser-2.hf.space/--replicas/snjqb/") | |
result = client.predict( | |
"-1", # str in 'guest_id' Textbox component | |
"<|startoftext|>poster of four players playing beach volleyball and net photorealistic <|endoftext|><|startoftext|> l36 t18 r90 b35 [M] [a] [l] [i] [b] [u] <|endoftext|> l38 t32 r87 b52 [B] [e] [a] [c] [h] <|endoftext|> l21 t51 r105 b76 [V] [o] [l] [l] [e] [y] [b] [a] [l] [l] <|endoftext|><|endoftext|>", # str in 'Prompt. You can let language model automatically identify keywords, or provide them below' Textbox component | |
"Malibu/Beach/Volleyball", # str in '(Optional) Keywords. Should be separated by / (e.g., keyword1/keyword2/...)' Textbox component | |
"Wide-format", # str in '(Optional) Positive prompt' Textbox component | |
"TextDiffuser-2", # Literal[TextDiffuser-2, TextDiffuser-2-LCM] in 'Choice of models' Radio component | |
30, # float (numeric value between 1 and 50) in 'Sampling step' Slider component | |
7, # float (numeric value between 1 and 13) in 'Scale of classifier-free guidance' Slider component | |
8, # float (numeric value between 1 and 6) in 'Batch size' Slider component | |
1.7, # float (numeric value between 0.1 and 2) in 'Temperature' Slider component | |
False, # bool in 'Natural image generation' Checkbox component | |
api_name="/text_to_image" | |
) | |
r = result | |
# Parse the JSON to extract the file paths | |
# Check if the first element of the tuple contains the data | |
if isinstance(result[0], list): | |
# Loop through each entry in the data | |
for item in result[0]: | |
# Extract the image path | |
image_path = item.get('image') | |
if image_path: | |
# Extract the parent folder name | |
parent_folder = os.path.dirname(image_path) | |
parent_folder_name = os.path.basename(parent_folder) | |
# Create a new filename using the parent folder name | |
new_filename = f"{parent_folder_name}.png" | |
# Define the destination path | |
destination_path = os.path.join(destination_directory, new_filename) | |
# Move the file | |
shutil.move(image_path, destination_path) | |
print(f"Moved: {image_path} to {destination_path}") | |
# Check if the parent folder is empty and delete it | |
if not os.listdir(parent_folder): | |
os.rmdir(parent_folder) | |
print(f"Deleted empty folder: {parent_folder}") | |
else: | |
print("The result structure is not as expected.") | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: