Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,31 +2,25 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from io import BytesIO
|
4 |
from PIL import Image
|
5 |
-
import
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
def register_user(username, password):
|
16 |
-
if username in registered_users:
|
17 |
-
return f"Username '{username}' is already taken!"
|
18 |
-
registered_users[username] = hash_password(password)
|
19 |
-
return "Registration successful! You can now log in."
|
20 |
-
|
21 |
-
# Utility: Handle user login
|
22 |
-
def login_user(username, password):
|
23 |
-
hashed_pw = hash_password(password)
|
24 |
-
if username in registered_users and registered_users[username] == hashed_pw:
|
25 |
-
return True, f"Welcome back, {username}!"
|
26 |
-
return False, "Invalid username or password!"
|
27 |
|
28 |
-
# Generate image function (unchanged)
|
29 |
def get_image_from_url(url):
|
|
|
|
|
|
|
30 |
try:
|
31 |
response = requests.get(url, stream=True)
|
32 |
response.raise_for_status()
|
@@ -38,61 +32,54 @@ def get_image_from_url(url):
|
|
38 |
return f"Error processing image: {e}"
|
39 |
|
40 |
def generate_image(prompt, aspect_ratio, realism):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
def login_or_register(mode, username, password):
|
46 |
-
if mode == "Register":
|
47 |
-
return register_user(username, password)
|
48 |
-
elif mode == "Login":
|
49 |
-
success, message = login_user(username, password)
|
50 |
-
if success:
|
51 |
-
return gr.update(visible=True), message # Show the main app if login successful
|
52 |
-
return gr.update(visible=False), message # Hide the main app if login failed
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
fn=
|
75 |
inputs=[
|
76 |
gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate"),
|
77 |
gr.Radio(
|
78 |
choices=["1:1", "3:4", "4:3", "9:16", "16:9", "9:21", "21:9"],
|
79 |
label="Aspect Ratio",
|
80 |
-
value="16:9"
|
81 |
),
|
82 |
-
gr.Checkbox(label="Realism", value=True),
|
83 |
],
|
84 |
-
outputs=gr.Image(type="pil"),
|
|
|
|
|
85 |
)
|
86 |
|
87 |
-
#
|
88 |
-
|
89 |
-
state = gr.State(visible=False)
|
90 |
-
with gr.Row():
|
91 |
-
gr.Column(login_interface)
|
92 |
-
gr.Column(main_interface, visible=False) # Initially hide the main app
|
93 |
-
|
94 |
-
# Logic to handle visibility
|
95 |
-
def toggle_interface(visibility):
|
96 |
-
main_interface.visible = visibility
|
97 |
-
|
98 |
-
app.launch()
|
|
|
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_TOKEN1")
|
9 |
+
GLIF_API_URL = "https://simple-api.glif.app"
|
10 |
+
glif_token_id = 1
|
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)
|
26 |
response.raise_for_status()
|
|
|
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,
|
70 |
inputs=[
|
71 |
gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate"),
|
72 |
gr.Radio(
|
73 |
choices=["1:1", "3:4", "4:3", "9:16", "16:9", "9:21", "21:9"],
|
74 |
label="Aspect Ratio",
|
75 |
+
value="16:9" # Default value
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|