creative / app.py
artintel235's picture
Update app.py
f94dc93 verified
raw
history blame
3.32 kB
import gradio as gr
import requests
from io import BytesIO
from PIL import Image
import hashlib
# Store registered users in a dictionary (you can use a file or database for persistence)
registered_users = {}
# Utility: Hash passwords using SHA-256
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
# Utility: Handle user registration
def register_user(username, password):
if username in registered_users:
return f"Username '{username}' is already taken!"
registered_users[username] = hash_password(password)
return "Registration successful! You can now log in."
# Utility: Handle user login
def login_user(username, password):
hashed_pw = hash_password(password)
if username in registered_users and registered_users[username] == hashed_pw:
return True, f"Welcome back, {username}!"
return False, "Invalid username or password!"
# Generate image function (unchanged)
def get_image_from_url(url):
try:
response = requests.get(url, stream=True)
response.raise_for_status()
image = Image.open(BytesIO(response.content))
return image
except requests.exceptions.RequestException as e:
return f"Error fetching image: {e}"
except Exception as e:
return f"Error processing image: {e}"
def generate_image(prompt, aspect_ratio, realism):
# Dummy implementation to show the flow (replace with your GLIF API logic)
return f"Generated image for prompt '{prompt}' with aspect ratio {aspect_ratio}."
# Gradio Interface for Login/Register
def login_or_register(mode, username, password):
if mode == "Register":
return register_user(username, password)
elif mode == "Login":
success, message = login_user(username, password)
if success:
return gr.update(visible=True), message # Show the main app if login successful
return gr.update(visible=False), message # Hide the main app if login failed
# Gradio Interface for Main App
def main_app(prompt, aspect_ratio, realism):
return generate_image(prompt, aspect_ratio, realism)
# Register/Login Interface
login_interface = gr.Interface(
fn=login_or_register,
inputs=[
gr.Radio(["Login", "Register"], label="Mode"),
gr.Textbox(label="Username", placeholder="Enter your username"),
gr.Textbox(label="Password", type="password", placeholder="Enter your password"),
],
outputs=[
gr.State(), # Pass visibility state
gr.Textbox(label="Status"),
],
)
# Main Interface
main_interface = gr.Interface(
fn=main_app,
inputs=[
gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate"),
gr.Radio(
choices=["1:1", "3:4", "4:3", "9:16", "16:9", "9:21", "21:9"],
label="Aspect Ratio",
value="16:9"
),
gr.Checkbox(label="Realism", value=True),
],
outputs=gr.Image(type="pil"),
)
# Combine Interfaces
with gr.Blocks() as app:
state = gr.State(visible=False)
with gr.Row():
gr.Column(login_interface)
gr.Column(main_interface, visible=False) # Initially hide the main app
# Logic to handle visibility
def toggle_interface(visibility):
main_interface.visible = visibility
app.launch()