Spaces:
Sleeping
Sleeping
Update gradio_ui.py
Browse files- gradio_ui.py +25 -37
gradio_ui.py
CHANGED
|
@@ -1,31 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
|
| 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:
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error: {str(e)}"
|
| 31 |
|
|
@@ -35,33 +24,32 @@ def get_user_info(token):
|
|
| 35 |
f"{API_URL}/users/me",
|
| 36 |
headers={"Authorization": f"Bearer {token}"}
|
| 37 |
)
|
| 38 |
-
return
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error: {str(e)}"
|
| 41 |
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
-
gr.Markdown("
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
fetch_button.click(get_user_info, inputs=token_input, outputs=user_info_output)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
API_URL = os.getenv("API_URL", "http://localhost:8000")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def login(username, password):
|
| 8 |
try:
|
| 9 |
response = requests.post(
|
| 10 |
f"{API_URL}/token",
|
| 11 |
+
data={"username": username, "password": password, "grant_type": "password"},
|
| 12 |
headers={"Content-Type": "application/x-www-form-urlencoded"}
|
| 13 |
)
|
| 14 |
if response.status_code == 200:
|
| 15 |
token = response.json().get("access_token")
|
| 16 |
return f"Login successful! Token: {token[:15]}... (truncated)"
|
| 17 |
+
return f"Login failed: {response.json().get('detail', 'Unknown error')}"
|
| 18 |
except Exception as e:
|
| 19 |
return f"Error: {str(e)}"
|
| 20 |
|
|
|
|
| 24 |
f"{API_URL}/users/me",
|
| 25 |
headers={"Authorization": f"Bearer {token}"}
|
| 26 |
)
|
| 27 |
+
return str(response.json())
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error: {str(e)}"
|
| 30 |
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("""
|
| 33 |
+
# Secure JWT Authentication
|
| 34 |
+
Test credentials: username=`testuser`, password=`secret`
|
| 35 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column():
|
| 39 |
+
gr.Markdown("## Login")
|
| 40 |
+
username = gr.Textbox(label="Username")
|
| 41 |
+
password = gr.Textbox(label="Password", type="password")
|
| 42 |
+
login_btn = gr.Button("Login")
|
| 43 |
+
login_output = gr.Textbox(label="Login Result")
|
| 44 |
+
|
| 45 |
+
with gr.Column():
|
| 46 |
+
gr.Markdown("## Get User Info")
|
| 47 |
+
token = gr.Textbox(label="Your JWT Token")
|
| 48 |
+
user_btn = gr.Button("Get User Info")
|
| 49 |
+
user_output = gr.Textbox(label="User Info")
|
| 50 |
|
| 51 |
+
login_btn.click(login, inputs=[username, password], outputs=login_output)
|
| 52 |
+
user_btn.click(get_user_info, inputs=token, outputs=user_output)
|
|
|
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|