Spaces:
Sleeping
Sleeping
Create gradio_ui.py
Browse files- gradio_ui.py +67 -0
gradio_ui.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Update this if deploying elsewhere
|
| 6 |
+
API_URL = "http://localhost:8000" # For HuggingFace Spaces, use your Space URL
|
| 7 |
+
|
| 8 |
+
def register(username, password):
|
| 9 |
+
try:
|
| 10 |
+
response = requests.post(
|
| 11 |
+
f"{API_URL}/register",
|
| 12 |
+
json={"username": username, "password": password}
|
| 13 |
+
)
|
| 14 |
+
return response.json().get("message", "Registration successful!")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return f"Error: {str(e)}"
|
| 17 |
+
|
| 18 |
+
def login(username, password):
|
| 19 |
+
try:
|
| 20 |
+
response = requests.post(
|
| 21 |
+
f"{API_URL}/token",
|
| 22 |
+
data={"username": username, "password": password},
|
| 23 |
+
headers={"Content-Type": "application/x-www-form-urlencoded"}
|
| 24 |
+
)
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
token = response.json().get("access_token")
|
| 27 |
+
return f"Login successful! Token: {token[:15]}... (truncated)"
|
| 28 |
+
return "Login failed: Invalid credentials"
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error: {str(e)}"
|
| 31 |
+
|
| 32 |
+
def get_user_info(token):
|
| 33 |
+
try:
|
| 34 |
+
response = requests.get(
|
| 35 |
+
f"{API_URL}/users/me",
|
| 36 |
+
headers={"Authorization": f"Bearer {token}"}
|
| 37 |
+
)
|
| 38 |
+
return json.dumps(response.json(), indent=2)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error: {str(e)}"
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("# Secure Authentication System")
|
| 44 |
+
|
| 45 |
+
with gr.Tab("Register"):
|
| 46 |
+
reg_username = gr.Textbox(label="Username")
|
| 47 |
+
reg_password = gr.Textbox(label="Password", type="password")
|
| 48 |
+
reg_output = gr.Textbox(label="Output")
|
| 49 |
+
reg_button = gr.Button("Register")
|
| 50 |
+
|
| 51 |
+
with gr.Tab("Login"):
|
| 52 |
+
login_username = gr.Textbox(label="Username")
|
| 53 |
+
login_password = gr.Textbox(label="Password", type="password")
|
| 54 |
+
login_output = gr.Textbox(label="Output")
|
| 55 |
+
login_button = gr.Button("Login")
|
| 56 |
+
|
| 57 |
+
with gr.Tab("User Info"):
|
| 58 |
+
token_input = gr.Textbox(label="Your JWT Token")
|
| 59 |
+
user_info_output = gr.Textbox(label="User Info", interactive=False)
|
| 60 |
+
fetch_button = gr.Button("Get User Info")
|
| 61 |
+
|
| 62 |
+
reg_button.click(register, inputs=[reg_username, reg_password], outputs=reg_output)
|
| 63 |
+
login_button.click(login, inputs=[login_username, login_password], outputs=login_output)
|
| 64 |
+
fetch_button.click(get_user_info, inputs=token_input, outputs=user_info_output)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|