Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
async with aiohttp.ClientSession() as session:
|
33 |
-
try:
|
34 |
-
async with session.post(
|
35 |
-
GLIF_API_URL, json=payload, headers=headers, timeout=15
|
36 |
-
) as response:
|
37 |
-
response.raise_for_status()
|
38 |
-
response_data = await response.json()
|
39 |
-
if "error" in response_data:
|
40 |
-
if 'error 429' in response_data['error']:
|
41 |
-
if glif_tokens_tried<no_of_accounts:
|
42 |
-
glif_token_id = (glif_token_id+1)%no_of_accounts
|
43 |
-
glif_tokens_tried+=1
|
44 |
-
GLIF_API_TOKEN = os.getenv(f"GLIF_API_TOKEN{glif_token_id}")
|
45 |
-
response_data = await generate_image_async(prompt, aspect_ratio, style)
|
46 |
-
glif_tokens_tried = 0
|
47 |
-
return response_data
|
48 |
-
response_data = "No credits available"
|
49 |
-
return response_data
|
50 |
-
elif "output" in response_data:
|
51 |
-
image_url = response_data["output"]
|
52 |
-
async with session.get(image_url) as image_response:
|
53 |
-
image_response.raise_for_status()
|
54 |
-
image_bytes = await image_response.read()
|
55 |
-
return image_bytes
|
56 |
-
else:
|
57 |
-
return "Error: Unexpected response from server"
|
58 |
-
|
59 |
-
except asyncio.TimeoutError:
|
60 |
-
return "Error: API request timed out."
|
61 |
-
except aiohttp.ClientError as e:
|
62 |
-
return f"API request failed: {e}"
|
63 |
-
except Exception as e:
|
64 |
-
return f"Exception:{e}"
|
65 |
-
|
66 |
-
# --- Gradio Interface Function ---
|
67 |
-
async def infer(
|
68 |
-
prompt,
|
69 |
-
aspect_ratio,
|
70 |
-
style,
|
71 |
-
progress=gr.Progress(track_tqdm=True)
|
72 |
-
):
|
73 |
-
|
74 |
-
image_bytes_or_error = await generate_image_async(prompt, aspect_ratio, style)
|
75 |
-
|
76 |
-
return image_bytes_or_error
|
77 |
-
|
78 |
-
# --- Gradio UI Setup ---
|
79 |
-
examples = [
|
80 |
-
["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", "1:1", "anime"],
|
81 |
-
["An astronaut riding a green horse", "16:9", "realistic"],
|
82 |
-
["A delicious ceviche cheesecake slice", "4:3", "anime"],
|
83 |
-
]
|
84 |
-
|
85 |
-
css = """
|
86 |
-
#col-container {
|
87 |
-
margin: 0 auto;
|
88 |
-
max-width: 640px;
|
89 |
-
}
|
90 |
-
"""
|
91 |
-
|
92 |
-
with gr.Blocks(css=css) as demo:
|
93 |
-
with gr.Column(elem_id="col-container"):
|
94 |
-
gr.Markdown(" # GLIF Text-to-Image")
|
95 |
-
|
96 |
-
with gr.Row():
|
97 |
-
prompt = gr.Text(
|
98 |
-
label="Prompt",
|
99 |
-
show_label=False,
|
100 |
-
max_lines=1,
|
101 |
-
placeholder="Enter your prompt",
|
102 |
-
container=False,
|
103 |
-
)
|
104 |
-
|
105 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
106 |
-
|
107 |
-
result = gr.Image(label="Result", show_label=False)
|
108 |
-
|
109 |
-
with gr.Accordion("Settings", open=True):
|
110 |
-
aspect_ratio = gr.Dropdown(
|
111 |
-
label="Aspect Ratio",
|
112 |
-
choices=[
|
113 |
-
"1:1",
|
114 |
-
"3:4",
|
115 |
-
"4:3",
|
116 |
-
"9:16",
|
117 |
-
"16:9",
|
118 |
-
"9:21",
|
119 |
-
"21:9",
|
120 |
-
],
|
121 |
-
value="1:1",
|
122 |
-
)
|
123 |
-
style = gr.Dropdown(
|
124 |
-
label = "Style",
|
125 |
-
choices = [
|
126 |
-
"anime",
|
127 |
-
"realistic"
|
128 |
-
],
|
129 |
-
value = "anime"
|
130 |
-
)
|
131 |
-
|
132 |
-
gr.Examples(examples=examples, inputs=[prompt, aspect_ratio, style])
|
133 |
-
|
134 |
-
demo.on(
|
135 |
-
triggers=[run_button.click, prompt.submit],
|
136 |
-
fn=infer,
|
137 |
-
inputs=[
|
138 |
-
prompt,
|
139 |
-
aspect_ratio,
|
140 |
-
style,
|
141 |
-
],
|
142 |
-
outputs=[result],
|
143 |
-
)
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from io import BytesIO
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
HARDCODED_URL = "https://images.unsplash.com/photo-1682687220204-276355c19923?ixlib=rb-4.0.3&ixid=M3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2940&q=80" # Replace with your desired URL
|
7 |
+
|
8 |
+
def get_image_from_hardcoded_url(prompt):
|
9 |
+
"""
|
10 |
+
Loads and returns an image from a hardcoded URL.
|
11 |
+
The prompt is ignored in this version.
|
12 |
+
"""
|
13 |
+
try:
|
14 |
+
response = requests.get(HARDCODED_URL, stream=True)
|
15 |
+
response.raise_for_status()
|
16 |
+
image = Image.open(BytesIO(response.content))
|
17 |
+
return image
|
18 |
+
except requests.exceptions.RequestException as e:
|
19 |
+
return f"Error fetching image: {e}"
|
20 |
+
except Exception as e:
|
21 |
+
return f"Error processing image: {e}"
|
22 |
+
|
23 |
+
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=get_image_from_hardcoded_url,
|
26 |
+
inputs=gr.Textbox(lines=2, label="Enter a Prompt (Ignored)"),
|
27 |
+
outputs=gr.Image(label="Fetched Image"),
|
28 |
+
title="Image Fetcher (Hardcoded URL)",
|
29 |
+
description="Enter a prompt which will be ignored. The app will then fetch the image from a hardcoded URL and display it."
|
30 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
if __name__ == "__main__":
|
33 |
+
iface.launch()
|