broadfield-dev commited on
Commit
e623db2
Β·
verified Β·
1 Parent(s): 8f76580

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os; os.system("pip install git+https://github.com/broadfield-dev/gradio-keylock.git")
2
+ import gradio as gr
3
+ from gradio_keylock.component import KeylockDecoderComponent, AppServerLogic
4
+ import json
5
+
6
+ server_logic = AppServerLogic()
7
+
8
+ example_db = {"USER":"TestUser","PASS":"TestPass"}
9
+
10
+ def login_fn(username,password):
11
+ if username==example_db.get("USER") and password==example_db.get("PASS"):
12
+ return "### βœ… Logged in"
13
+ else:
14
+ return "### πŸ›‘ Incorrect Username or Password"
15
+
16
+ def process_image_and_display_payload(image):
17
+ if image is None:
18
+ return "Upload a KeyLock image to auto-fill credentials.", {}
19
+
20
+ result_dict = server_logic.decode_payload(image)
21
+
22
+ if result_dict.get("status") == "Success":
23
+ user = result_dict.get("payload", {}).get("USER", "")
24
+ user_pass = result_dict.get("payload", {}).get("PASS", "")
25
+
26
+ GROQ_API_KEY = result_dict.get("payload", {}).get("GROQ_API_KEY", "")
27
+ HF_TOKEN = result_dict.get("payload", {}).get("HF_TOKEN", "")
28
+ OPENAI_API_KEY = result_dict.get("payload", {}).get("OPENAI_API_KEY", "")
29
+ OPENROUTER_API_KEY = result_dict.get("payload", {}).get("OPENROUTER_API_KEY", "")
30
+
31
+ status_message = f"<p style='color:green; font-weight:bold;'>βœ… Success! Decoded credentials for '{user}'.</p>"
32
+ payload = result_dict.get("payload", {})
33
+ else:
34
+ message = result_dict.get("message", "An unknown error occurred.")
35
+ status_message = f"<p style='color:red; font-weight:bold;'>❌ Error: {message}</p>"
36
+ payload = {}
37
+
38
+ return status_message, payload, user, user_pass, GROQ_API_KEY, HF_TOKEN, OPENAI_API_KEY, OPENROUTER_API_KEY
39
+
40
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
41
+ gr.Markdown("# Gradio KeyLock Component Demo")
42
+ gr.Markdown(
43
+ "**Instructions:**\n"
44
+ "1. Use the 'Generate Encrypted Image' section to create a new secure image.\n"
45
+ "2. Download the generated image or drag it to the 'KeyLock Image' upload area.\n"
46
+ "3. The decoded data will **automatically** appear below."
47
+ )
48
+ with gr.Row():
49
+ with gr.Column():
50
+ gr.Markdown("## Login")
51
+ user_name=gr.Textbox(label='User Name')
52
+ user_pass=gr.Textbox(label='Password', type='password')
53
+ login_btn=gr.Button("Login")
54
+ login_msg=gr.Markdown("### Enter user name and password")
55
+ gr.Markdown("## API Keys (Demo)")
56
+ user_GROQ=gr.Textbox(label='GROQ_API_KEY')
57
+ user_HF=gr.Textbox(label='HF_TOKEN')
58
+ user_OPENAI=gr.Textbox(label='OPENAI_API_KEY')
59
+ user_OPENROUTER=gr.Textbox(label='OPENROUTER_API_KEY')
60
+
61
+ with gr.Column():
62
+ keylock_builder = KeylockDecoderComponent(server_logic)
63
+ image_input, status_display = keylock_builder.build_ui()
64
+
65
+ output_json = gr.JSON(label="Decoded Payload")
66
+
67
+ login_btn.click(login_fn,[user_name,user_pass], login_msg)
68
+ image_input.upload(
69
+ fn=process_image_and_display_payload,
70
+ inputs=image_input,
71
+ outputs=[status_display, output_json, user_name, user_pass, user_GROQ, user_HF, user_OPENAI, user_OPENROUTER]
72
+ )
73
+
74
+ demo.launch()