Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,16 +2,22 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from io import BytesIO
|
4 |
from PIL import Image
|
5 |
-
|
6 |
HARDCODED_URL = "" # Replace with your desired URL
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
15 |
response.raise_for_status()
|
16 |
image = Image.open(BytesIO(response.content))
|
17 |
return image
|
@@ -19,15 +25,58 @@ def get_image_from_hardcoded_url(prompt):
|
|
19 |
return f"Error fetching image: {e}"
|
20 |
except Exception as e:
|
21 |
return f"Error processing image: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
fn=
|
26 |
-
inputs=
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
)
|
31 |
|
32 |
-
|
33 |
-
|
|
|
2 |
import requests
|
3 |
from io import BytesIO
|
4 |
from PIL import Image
|
5 |
+
import os
|
6 |
HARDCODED_URL = "" # Replace with your desired URL
|
7 |
|
8 |
+
GLIF_API_TOKEN = os.getenv("GLIF_API_TOKEN")
|
9 |
+
GLIF_API_URL = "https://simple-api.glif.app"
|
10 |
+
glif_token_id = 0
|
11 |
+
glif_tokens_tried = 0
|
12 |
+
no_of_accounts = 11
|
13 |
+
|
14 |
+
def get_image_from_url(url):
|
15 |
"""
|
16 |
Loads and returns an image from a hardcoded URL.
|
17 |
The prompt is ignored in this version.
|
18 |
"""
|
19 |
try:
|
20 |
+
response = requests.get(url, stream=True)
|
21 |
response.raise_for_status()
|
22 |
image = Image.open(BytesIO(response.content))
|
23 |
return image
|
|
|
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 |
+
# Make the POST request
|
43 |
+
response_data = requests.post(GLIF_API_URL, json=payload, headers=headers).json()
|
44 |
+
if "error" in response_data:
|
45 |
+
if 'error 429' in response_data['error']:
|
46 |
+
if glif_tokens_tried<no_of_accounts:
|
47 |
+
glif_token_id = (glif_token_id+1)%no_of_accounts
|
48 |
+
glif_tokens_tried+=1
|
49 |
+
GLIF_API_TOKEN = os.getenv(f"GLIF_API_TOKEN{glif_token_id}")
|
50 |
+
response_data = await generate_image_async(prompt, aspect_ratio, realism)
|
51 |
+
glif_tokens_tried = 0
|
52 |
+
return response_data
|
53 |
+
response_data = "No credits available"
|
54 |
+
return response_data
|
55 |
+
elif "output" in response_data:
|
56 |
+
url = response_data['output']
|
57 |
+
image = get_image_from_url(url)
|
58 |
+
return f"prompt:{response_data['inputs']['Prompt']}\noutput:{response_data['output']}"
|
59 |
+
else:
|
60 |
+
return "Error: Unexpected response from server"
|
61 |
|
62 |
+
# Define the Gradio interface
|
63 |
+
interface = gr.Interface(
|
64 |
+
fn=gradio_generate_image,
|
65 |
+
inputs=[
|
66 |
+
gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate"),
|
67 |
+
gr.Radio(
|
68 |
+
choices=["1:1", "3:4", "4:3", "9:16", "16:9", "9:21", "21:9"],
|
69 |
+
label="Aspect Ratio",
|
70 |
+
value="16:9" # Default value
|
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()
|