Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,59 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
#
|
4 |
-
user_db = {
|
5 |
|
6 |
-
#
|
7 |
-
def
|
8 |
-
return request.headers.get("user")
|
9 |
-
|
10 |
-
# Funci贸n de saludo
|
11 |
-
def greet(name):
|
12 |
-
return f"Hello {name}!"
|
13 |
-
|
14 |
-
# Definimos una interfaz simple de Gradio
|
15 |
-
def demo_function(username):
|
16 |
if username in user_db:
|
17 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
else:
|
19 |
-
return "
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Lanzamos la interfaz
|
28 |
-
demo.launch()
|
29 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import bcrypt
|
3 |
|
4 |
+
# Simulate a database with a dictionary
|
5 |
+
user_db = {}
|
6 |
|
7 |
+
# Function to register a new user
|
8 |
+
def register(username, password):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
if username in user_db:
|
10 |
+
return "Username already exists!"
|
11 |
+
|
12 |
+
# Hash the password using bcrypt
|
13 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
14 |
+
|
15 |
+
# Store username and hashed password
|
16 |
+
user_db[username] = hashed_password
|
17 |
+
|
18 |
+
return "Registration successful!"
|
19 |
+
|
20 |
+
# Function to login with a username and password
|
21 |
+
def login(username, password):
|
22 |
+
if username not in user_db:
|
23 |
+
return "Username not found!"
|
24 |
+
|
25 |
+
hashed_password = user_db[username]
|
26 |
+
|
27 |
+
# Check if the entered password matches the stored hashed password
|
28 |
+
if bcrypt.checkpw(password.encode('utf-8'), hashed_password):
|
29 |
+
return f"Welcome back, {username}!"
|
30 |
else:
|
31 |
+
return "Incorrect password!"
|
32 |
+
|
33 |
+
# Create Gradio interfaces
|
34 |
+
def create_gradio_ui():
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
with gr.Tab("Register"):
|
37 |
+
gr.Markdown("### User Registration")
|
38 |
+
username = gr.Textbox(label="Username")
|
39 |
+
password = gr.Password(label="Password")
|
40 |
+
register_button = gr.Button("Register")
|
41 |
+
register_output = gr.Textbox(label="Registration Status")
|
42 |
+
register_button.click(register, inputs=[username, password], outputs=register_output)
|
43 |
+
|
44 |
+
with gr.Tab("Login"):
|
45 |
+
gr.Markdown("### User Login")
|
46 |
+
login_username = gr.Textbox(label="Username")
|
47 |
+
login_password = gr.Password(label="Password")
|
48 |
+
login_button = gr.Button("Login")
|
49 |
+
login_output = gr.Textbox(label="Login Status")
|
50 |
+
login_button.click(login, inputs=[login_username, login_password], outputs=login_output)
|
51 |
+
|
52 |
+
return demo
|
53 |
+
|
54 |
+
# Launch Gradio app
|
55 |
+
if __name__ == "__main__":
|
56 |
+
demo = create_gradio_ui()
|
57 |
+
demo.launch()
|
58 |
|
|
|
|
|
59 |
|