File size: 3,320 Bytes
5e5fd54
72398d0
 
 
f94dc93
72398d0
f94dc93
 
c95b55d
f94dc93
 
 
febdcdd
f94dc93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c95b55d
72398d0
c95b55d
72398d0
 
 
 
 
 
 
febdcdd
c95b55d
f94dc93
 
febdcdd
f94dc93
 
 
 
 
 
 
 
 
febdcdd
f94dc93
 
 
febdcdd
f94dc93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c95b55d
 
 
 
 
f94dc93
c95b55d
f94dc93
c95b55d
f94dc93
72398d0
5e5fd54
f94dc93
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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()