Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import base64
|
|
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
import os
|
@@ -9,6 +10,7 @@ API_KEY = os.environ.get("API_KEY")
|
|
9 |
API_URL = "https://api.4llm.com/v1/images/generations"
|
10 |
|
11 |
def generate_image(prompt, size="1024x1024"):
|
|
|
12 |
headers = {
|
13 |
"Authorization": f"Bearer {API_KEY}",
|
14 |
"Content-Type": "application/json"
|
@@ -32,12 +34,20 @@ def generate_image(prompt, size="1024x1024"):
|
|
32 |
|
33 |
# Convert to PIL Image
|
34 |
image = Image.open(io.BytesIO(image_bytes))
|
35 |
-
|
|
|
|
|
36 |
except Exception as e:
|
37 |
-
return None, f"Error: {str(e)}"
|
38 |
|
39 |
# Create the Gradio interface
|
40 |
-
with gr.Blocks(title="4LLM Image Generation", css="
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
gr.Markdown("# 4LLM Image Generation Demo")
|
42 |
gr.Markdown("Generate images using the 4LLM API with no rate limits!")
|
43 |
|
@@ -53,7 +63,7 @@ with gr.Blocks(title="4LLM Image Generation", css="footer { display: none !impor
|
|
53 |
value="1024x1024",
|
54 |
label="Image Size"
|
55 |
)
|
56 |
-
generate_btn = gr.Button("Generate Image")
|
57 |
|
58 |
with gr.Column():
|
59 |
output_image = gr.Image(label="Generated Image")
|
@@ -62,12 +72,29 @@ with gr.Blocks(title="4LLM Image Generation", css="footer { display: none !impor
|
|
62 |
lines=3,
|
63 |
interactive=False
|
64 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
generate_btn.click(
|
67 |
fn=generate_image,
|
68 |
inputs=[prompt, size],
|
69 |
-
outputs=[output_image, revised_prompt]
|
70 |
)
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
if __name__ == "__main__":
|
73 |
demo.launch(share=False)
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import base64
|
4 |
+
import time
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
import os
|
|
|
10 |
API_URL = "https://api.4llm.com/v1/images/generations"
|
11 |
|
12 |
def generate_image(prompt, size="1024x1024"):
|
13 |
+
start_time = time.time() # Start timing
|
14 |
headers = {
|
15 |
"Authorization": f"Bearer {API_KEY}",
|
16 |
"Content-Type": "application/json"
|
|
|
34 |
|
35 |
# Convert to PIL Image
|
36 |
image = Image.open(io.BytesIO(image_bytes))
|
37 |
+
|
38 |
+
generation_time = time.time() - start_time # Calculate generation time
|
39 |
+
return image, result["data"][0]["revised_prompt"], f"{generation_time:.2f} seconds"
|
40 |
except Exception as e:
|
41 |
+
return None, f"Error: {str(e)}", ""
|
42 |
|
43 |
# Create the Gradio interface
|
44 |
+
with gr.Blocks(title="4LLM Image Generation", css="""
|
45 |
+
footer { display: none !important; }
|
46 |
+
.gr-button { background-color: #4CAF50; color: white; border: none; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 5px; }
|
47 |
+
.gr-button:hover { background-color: #45a049; }
|
48 |
+
.gr-textbox { margin-bottom: 10px; }
|
49 |
+
.gr-markdown { margin-bottom: 20px; }
|
50 |
+
""") as demo:
|
51 |
gr.Markdown("# 4LLM Image Generation Demo")
|
52 |
gr.Markdown("Generate images using the 4LLM API with no rate limits!")
|
53 |
|
|
|
63 |
value="1024x1024",
|
64 |
label="Image Size"
|
65 |
)
|
66 |
+
generate_btn = gr.Button("🔄 Generate Image")
|
67 |
|
68 |
with gr.Column():
|
69 |
output_image = gr.Image(label="Generated Image")
|
|
|
72 |
lines=3,
|
73 |
interactive=False
|
74 |
)
|
75 |
+
generation_time_display = gr.Textbox(
|
76 |
+
label="Generation Time",
|
77 |
+
lines=1,
|
78 |
+
interactive=False
|
79 |
+
)
|
80 |
+
copy_btn = gr.Button("📋 Copy Image")
|
81 |
+
download_btn = gr.Button("⬇️ Download Image")
|
82 |
|
83 |
generate_btn.click(
|
84 |
fn=generate_image,
|
85 |
inputs=[prompt, size],
|
86 |
+
outputs=[output_image, revised_prompt, generation_time_display]
|
87 |
)
|
88 |
|
89 |
+
# Add functionality for the Copy and Download buttons
|
90 |
+
def copy_image(image):
|
91 |
+
return image
|
92 |
+
|
93 |
+
def download_image(image):
|
94 |
+
return image
|
95 |
+
|
96 |
+
copy_btn.click(fn=copy_image, inputs=output_image, outputs=None)
|
97 |
+
download_btn.click(fn=download_image, inputs=output_image, outputs=None)
|
98 |
+
|
99 |
if __name__ == "__main__":
|
100 |
demo.launch(share=False)
|