Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,25 +2,31 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from io import BytesIO
|
4 |
from PIL import Image
|
5 |
-
import
|
6 |
-
# HARDCODED_URL = "" # Replace with your desired URL
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
glif_token_id = 1
|
11 |
-
glif_tokens_tried = 0
|
12 |
-
no_of_accounts = 3
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
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,54 +38,61 @@ def get_image_from_url(url):
|
|
32 |
return f"Error processing image: {e}"
|
33 |
|
34 |
def generate_image(prompt, aspect_ratio, realism):
|
35 |
-
|
36 |
-
|
37 |
-
global glif_tokens_tried
|
38 |
-
global no_of_accounts
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
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 |
-
#
|
68 |
-
|
69 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
76 |
),
|
77 |
-
gr.Checkbox(label="Realism", value=True),
|
78 |
],
|
79 |
-
outputs=gr.Image(type="pil"),
|
80 |
-
title="Image Generator",
|
81 |
-
description="Provide a prompt, select an aspect ratio, and set realism to generate an image.",
|
82 |
)
|
83 |
|
84 |
-
#
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
from io import BytesIO
|
4 |
from PIL import Image
|
5 |
+
import hashlib
|
|
|
6 |
|
7 |
+
# Store registered users in a dictionary (you can use a file or database for persistence)
|
8 |
+
registered_users = {}
|
|
|
|
|
|
|
9 |
|
10 |
+
# Utility: Hash passwords using SHA-256
|
11 |
+
def hash_password(password):
|
12 |
+
return hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
|
13 |
|
14 |
+
# Utility: Handle user registration
|
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 |
return f"Error processing image: {e}"
|
39 |
|
40 |
def generate_image(prompt, aspect_ratio, realism):
|
41 |
+
# Dummy implementation to show the flow (replace with your GLIF API logic)
|
42 |
+
return f"Generated image for prompt '{prompt}' with aspect ratio {aspect_ratio}."
|
|
|
|
|
43 |
|
44 |
+
# Gradio Interface for Login/Register
|
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 |
+
# Gradio Interface for Main App
|
55 |
+
def main_app(prompt, aspect_ratio, realism):
|
56 |
+
return generate_image(prompt, aspect_ratio, realism)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
# Register/Login Interface
|
59 |
+
login_interface = gr.Interface(
|
60 |
+
fn=login_or_register,
|
61 |
+
inputs=[
|
62 |
+
gr.Radio(["Login", "Register"], label="Mode"),
|
63 |
+
gr.Textbox(label="Username", placeholder="Enter your username"),
|
64 |
+
gr.Textbox(label="Password", type="password", placeholder="Enter your password"),
|
65 |
+
],
|
66 |
+
outputs=[
|
67 |
+
gr.State(), # Pass visibility state
|
68 |
+
gr.Textbox(label="Status"),
|
69 |
+
],
|
70 |
+
)
|
71 |
+
|
72 |
+
# Main Interface
|
73 |
+
main_interface = gr.Interface(
|
74 |
+
fn=main_app,
|
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 |
+
# Combine Interfaces
|
88 |
+
with gr.Blocks() as app:
|
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()
|