Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,73 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
+
from PIL import Image
|
| 4 |
import os
|
| 5 |
+
import time
|
| 6 |
+
import traceback
|
| 7 |
|
| 8 |
+
# Create a Client instance to communicate with the Hugging Face space
|
| 9 |
+
client = Client("https://huggingface.co/spaces/hsuwill000/LCM_SoteMix_OpenVINO_CPU_Space_TAESD")
|
| 10 |
+
|
| 11 |
+
# Counter for image filenames to avoid overwriting
|
| 12 |
+
count = 0
|
| 13 |
+
|
| 14 |
+
# Gradio Interface Function to handle image generation
|
| 15 |
+
def infer_gradio(prompt: str):
|
| 16 |
+
global count
|
| 17 |
+
|
| 18 |
+
# Prepare the inputs for the prediction
|
| 19 |
+
inputs = {
|
| 20 |
+
"prompt": prompt,
|
| 21 |
+
"num_inference_steps": 10 # Number of inference steps for the model
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Send the request to the model and receive the image
|
| 26 |
+
result = client.predict(inputs, api_name="/infer")
|
| 27 |
+
|
| 28 |
+
# Open the resulting image
|
| 29 |
+
image = Image.open(result)
|
| 30 |
+
|
| 31 |
+
# Create a unique filename to save the image
|
| 32 |
+
filename = f"img_{count:08d}.jpg"
|
| 33 |
+
while os.path.exists(filename):
|
| 34 |
+
count += 1
|
| 35 |
+
filename = f"img_{count:08d}.jpg"
|
| 36 |
+
|
| 37 |
+
# Save the image locally
|
| 38 |
+
image.save(filename)
|
| 39 |
+
print(f"Saved image as {filename}")
|
| 40 |
+
|
| 41 |
+
# Return the image to be displayed in Gradio
|
| 42 |
+
return image
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
# Handle any errors that occur
|
| 46 |
+
print(f"An exception occurred: {str(e)}")
|
| 47 |
+
print("Stack trace:")
|
| 48 |
+
traceback.print_exc() # Print stack trace for debugging
|
| 49 |
+
return None # Return nothing if an error occurs
|
| 50 |
+
|
| 51 |
+
# Define Gradio Interface
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
with gr.Column():
|
| 54 |
+
gr.Markdown("# LCMSoteMix Image Generator")
|
| 55 |
+
|
| 56 |
+
# Prompt input field for the user
|
| 57 |
+
prompt_input = gr.Textbox(
|
| 58 |
+
label="Enter Your Prompt",
|
| 59 |
+
placeholder="Type your prompt for image generation here",
|
| 60 |
+
lines=4, # Allow multi-line input for the prompt
|
| 61 |
+
interactive=True # Allow user to interact with the textbox
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Button to trigger the generation
|
| 65 |
+
run_button = gr.Button("Generate Image")
|
| 66 |
+
|
| 67 |
+
# Output image display area
|
| 68 |
+
output_image = gr.Image(label="Generated Image")
|
| 69 |
+
|
| 70 |
+
# Connecting the button click to the image generation function
|
| 71 |
+
run_button.click(infer_gradio, inputs=prompt_input, outputs=output_image)
|
| 72 |
+
|
| 73 |
+
demo.launch()
|