Spaces:
Running
Running
Update app.py with enhanced Hugging Face integration and UI improvements
Browse files
app.py
CHANGED
@@ -1,6 +1,144 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import random
|
4 |
+
import time
|
5 |
|
6 |
+
# Set up the client for the Hugging Face model
|
7 |
+
client = InferenceClient(model="GenAIJake/g3na1j8k3")
|
8 |
+
|
9 |
+
def generate_image(prompt, negative_prompt="", num_inference_steps=30, guidance_scale=7.5):
|
10 |
+
try:
|
11 |
+
# Add a small delay to show the loading indicator
|
12 |
+
time.sleep(0.5)
|
13 |
+
|
14 |
+
# Call the model API
|
15 |
+
image = client.text_to_image(
|
16 |
+
prompt=prompt,
|
17 |
+
negative_prompt=negative_prompt,
|
18 |
+
num_inference_steps=num_inference_steps,
|
19 |
+
guidance_scale=guidance_scale,
|
20 |
+
)
|
21 |
+
|
22 |
+
return image, None
|
23 |
+
except Exception as e:
|
24 |
+
return None, f"Error generating image: {str(e)}"
|
25 |
+
|
26 |
+
# Example prompts to help users get started
|
27 |
+
example_prompts = [
|
28 |
+
["A serene lake surrounded by mountains at sunset"],
|
29 |
+
["A futuristic cityscape with flying cars"],
|
30 |
+
["A photo-realistic cat wearing sunglasses and a hat"],
|
31 |
+
["A fantasy castle with dragons flying around it"],
|
32 |
+
["An astronaut riding a horse on the moon, digital art"]
|
33 |
+
]
|
34 |
+
|
35 |
+
# Custom CSS for styling
|
36 |
+
custom_css = """
|
37 |
+
.gradio-container {
|
38 |
+
font-family: 'Arial', sans-serif;
|
39 |
+
}
|
40 |
+
.main-header {
|
41 |
+
text-align: center;
|
42 |
+
margin-bottom: 2rem;
|
43 |
+
}
|
44 |
+
.example-prompt {
|
45 |
+
cursor: pointer;
|
46 |
+
padding: 0.5rem;
|
47 |
+
border-radius: 0.5rem;
|
48 |
+
background-color: #f7f7f7;
|
49 |
+
margin-bottom: 0.5rem;
|
50 |
+
transition: background-color 0.3s;
|
51 |
+
}
|
52 |
+
.example-prompt:hover {
|
53 |
+
background-color: #e0e0e0;
|
54 |
+
}
|
55 |
+
"""
|
56 |
+
|
57 |
+
# Create the Gradio interface
|
58 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
59 |
+
gr.HTML("""
|
60 |
+
<div class="main-header">
|
61 |
+
<h1>AI Image Generator</h1>
|
62 |
+
<p>Generate amazing images with the power of AI!</p>
|
63 |
+
</div>
|
64 |
+
""")
|
65 |
+
|
66 |
+
with gr.Row():
|
67 |
+
with gr.Column(scale=2):
|
68 |
+
# Input controls
|
69 |
+
with gr.Group():
|
70 |
+
prompt = gr.Textbox(
|
71 |
+
label="Enter your prompt",
|
72 |
+
placeholder="Describe the image you want to generate...",
|
73 |
+
lines=3
|
74 |
+
)
|
75 |
+
|
76 |
+
negative_prompt = gr.Textbox(
|
77 |
+
label="Negative prompt (optional)",
|
78 |
+
placeholder="Elements you want to exclude from the image...",
|
79 |
+
lines=2
|
80 |
+
)
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
steps = gr.Slider(
|
84 |
+
minimum=10,
|
85 |
+
maximum=50,
|
86 |
+
value=30,
|
87 |
+
step=1,
|
88 |
+
label="Number of inference steps"
|
89 |
+
)
|
90 |
+
|
91 |
+
guidance = gr.Slider(
|
92 |
+
minimum=1.0,
|
93 |
+
maximum=15.0,
|
94 |
+
value=7.5,
|
95 |
+
step=0.1,
|
96 |
+
label="Guidance scale"
|
97 |
+
)
|
98 |
+
|
99 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
100 |
+
|
101 |
+
# Example prompts section
|
102 |
+
with gr.Accordion("Example prompts", open=True):
|
103 |
+
gr.HTML("<p>Click on any example to use it as your prompt:</p>")
|
104 |
+
examples = gr.Examples(
|
105 |
+
example_prompts,
|
106 |
+
inputs=[prompt],
|
107 |
+
examples_per_page=5
|
108 |
+
)
|
109 |
+
|
110 |
+
with gr.Column(scale=3):
|
111 |
+
# Output area with error display
|
112 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
113 |
+
error_output = gr.Textbox(label="Error (if any)", visible=False)
|
114 |
+
|
115 |
+
# Instructions section
|
116 |
+
with gr.Accordion("How to use", open=False):
|
117 |
+
gr.HTML("""
|
118 |
+
<h3>Instructions:</h3>
|
119 |
+
<ul>
|
120 |
+
<li><strong>Enter a prompt</strong>: Describe the image you want to generate. Be specific and detailed for better results.</li>
|
121 |
+
<li><strong>Negative prompt</strong>: Describe elements you want to avoid in the image.</li>
|
122 |
+
<li><strong>Inference steps</strong>: More steps often result in higher quality but take longer to generate.</li>
|
123 |
+
<li><strong>Guidance scale</strong>: Higher values make the image adhere more strictly to your prompt.</li>
|
124 |
+
<li><strong>Examples</strong>: Click on any example prompt to use it as a starting point.</li>
|
125 |
+
</ul>
|
126 |
+
""")
|
127 |
+
|
128 |
+
# Set up event handlers
|
129 |
+
generate_btn.click(
|
130 |
+
fn=generate_image,
|
131 |
+
inputs=[prompt, negative_prompt, steps, guidance],
|
132 |
+
outputs=[output_image, error_output],
|
133 |
+
show_progress=True
|
134 |
+
)
|
135 |
+
|
136 |
+
# Handle errors by displaying the error message
|
137 |
+
error_output.change(
|
138 |
+
fn=lambda error: gr.update(visible=error is not None and error != ""),
|
139 |
+
inputs=[error_output],
|
140 |
+
outputs=[error_output]
|
141 |
+
)
|
142 |
+
|
143 |
+
# For Hugging Face Spaces, we need to use a specific launch pattern
|
144 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|