Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,10 +11,15 @@ glif_token_id = 1
|
|
11 |
glif_tokens_tried = 0
|
12 |
no_of_accounts = 3
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def get_image_from_url(url):
|
15 |
"""
|
16 |
-
|
17 |
-
The prompt is ignored in this version.
|
18 |
"""
|
19 |
try:
|
20 |
response = requests.get(url, stream=True)
|
@@ -25,40 +30,40 @@ def get_image_from_url(url):
|
|
25 |
return f"Error fetching image: {e}"
|
26 |
except Exception as e:
|
27 |
return f"Error processing image: {e}"
|
28 |
-
|
29 |
def generate_image(prompt, aspect_ratio, realism):
|
30 |
global glif_token_id
|
31 |
global GLIF_API_TOKEN
|
32 |
global glif_tokens_tried
|
33 |
global no_of_accounts
|
34 |
-
|
35 |
payload = {
|
36 |
"id": "cm3ugmzv2002gnckiosrwk6xi", # Replace with appropriate ID if necessary
|
37 |
"inputs": [prompt, aspect_ratio, str(realism).lower()],
|
38 |
}
|
39 |
headers = {"Authorization": f"Bearer {GLIF_API_TOKEN}"}
|
40 |
-
|
41 |
try:
|
42 |
response_data = requests.post(GLIF_API_URL, json=payload, headers=headers).json()
|
43 |
if "error" in response_data:
|
44 |
if 'error 429' in response_data['error']:
|
45 |
-
if glif_tokens_tried<no_of_accounts:
|
46 |
-
glif_token_id = (glif_token_id+1)%no_of_accounts
|
47 |
-
glif_tokens_tried+=1
|
48 |
GLIF_API_TOKEN = os.getenv(f"GLIF_API_TOKEN{glif_token_id}")
|
49 |
response_data = generate_image(prompt, aspect_ratio, realism)
|
50 |
glif_tokens_tried = 0
|
51 |
return response_data
|
52 |
-
|
53 |
return response_data
|
54 |
elif "output" in response_data:
|
55 |
url = response_data['output']
|
56 |
-
|
57 |
-
return image
|
58 |
else:
|
59 |
return "Error: Unexpected response from server"
|
60 |
except Exception as e:
|
61 |
-
return f"
|
|
|
62 |
# Define the Gradio interface
|
63 |
interface = gr.Interface(
|
64 |
fn=generate_image,
|
@@ -71,12 +76,10 @@ interface = gr.Interface(
|
|
71 |
),
|
72 |
gr.Checkbox(label="Realism", value=True), # Checkbox for realism (True/False)
|
73 |
],
|
74 |
-
outputs=
|
75 |
-
gr.Textbox(label="Result"),
|
76 |
-
],
|
77 |
title="Image Generator",
|
78 |
description="Provide a prompt, select an aspect ratio, and set realism to generate an image.",
|
79 |
)
|
80 |
|
81 |
# Launch the interface
|
82 |
-
interface.launch()
|
|
|
11 |
glif_tokens_tried = 0
|
12 |
no_of_accounts = 3
|
13 |
|
14 |
+
import gradio as gr
|
15 |
+
from PIL import Image
|
16 |
+
from io import BytesIO
|
17 |
+
import requests
|
18 |
+
import os
|
19 |
+
|
20 |
def get_image_from_url(url):
|
21 |
"""
|
22 |
+
Fetches and returns an image from a given URL.
|
|
|
23 |
"""
|
24 |
try:
|
25 |
response = requests.get(url, stream=True)
|
|
|
30 |
return f"Error fetching image: {e}"
|
31 |
except Exception as e:
|
32 |
return f"Error processing image: {e}"
|
33 |
+
|
34 |
def generate_image(prompt, aspect_ratio, realism):
|
35 |
global glif_token_id
|
36 |
global GLIF_API_TOKEN
|
37 |
global glif_tokens_tried
|
38 |
global no_of_accounts
|
39 |
+
|
40 |
payload = {
|
41 |
"id": "cm3ugmzv2002gnckiosrwk6xi", # Replace with appropriate ID if necessary
|
42 |
"inputs": [prompt, aspect_ratio, str(realism).lower()],
|
43 |
}
|
44 |
headers = {"Authorization": f"Bearer {GLIF_API_TOKEN}"}
|
45 |
+
|
46 |
try:
|
47 |
response_data = requests.post(GLIF_API_URL, json=payload, headers=headers).json()
|
48 |
if "error" in response_data:
|
49 |
if 'error 429' in response_data['error']:
|
50 |
+
if glif_tokens_tried < no_of_accounts:
|
51 |
+
glif_token_id = (glif_token_id + 1) % no_of_accounts
|
52 |
+
glif_tokens_tried += 1
|
53 |
GLIF_API_TOKEN = os.getenv(f"GLIF_API_TOKEN{glif_token_id}")
|
54 |
response_data = generate_image(prompt, aspect_ratio, realism)
|
55 |
glif_tokens_tried = 0
|
56 |
return response_data
|
57 |
+
return "No credits available"
|
58 |
return response_data
|
59 |
elif "output" in response_data:
|
60 |
url = response_data['output']
|
61 |
+
return get_image_from_url(url) # Return the image directly
|
|
|
62 |
else:
|
63 |
return "Error: Unexpected response from server"
|
64 |
except Exception as e:
|
65 |
+
return f"Error: {e}"
|
66 |
+
|
67 |
# Define the Gradio interface
|
68 |
interface = gr.Interface(
|
69 |
fn=generate_image,
|
|
|
76 |
),
|
77 |
gr.Checkbox(label="Realism", value=True), # Checkbox for realism (True/False)
|
78 |
],
|
79 |
+
outputs=gr.Image(type="pil"), # Output image
|
|
|
|
|
80 |
title="Image Generator",
|
81 |
description="Provide a prompt, select an aspect ratio, and set realism to generate an image.",
|
82 |
)
|
83 |
|
84 |
# Launch the interface
|
85 |
+
interface.launch()
|