Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,43 +12,36 @@ logger = logging.getLogger(__name__)
|
|
12 |
# ==============================================================================
|
13 |
# CLIENT-SIDE API CALL LOGIC
|
14 |
# ==============================================================================
|
15 |
-
|
16 |
-
def call_api(server_endpoint_url: str, image: Image.Image) -> dict:
|
17 |
"""
|
18 |
Takes a PIL image, converts it to base64, sends it to the specified server API,
|
19 |
and returns the decrypted JSON data.
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
-
raise gr.Error("Please provide the FULL API endpoint URL, including the /run/mcp_decode path.")
|
24 |
if image is None:
|
25 |
raise gr.Error("Please upload an encrypted image.")
|
26 |
|
27 |
-
|
|
|
28 |
logger.info(f"Client attempting to call API at: {api_endpoint}")
|
29 |
|
30 |
try:
|
31 |
-
# 1. Convert the PIL Image to a base64 string
|
32 |
with io.BytesIO() as buffer:
|
33 |
image.save(buffer, format="PNG")
|
34 |
image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
35 |
|
36 |
-
# 2. Prepare the JSON payload for the Gradio API
|
37 |
payload = {"data": [image_base64]}
|
38 |
-
|
39 |
-
# 3. Make the POST request to the server API
|
40 |
headers = {"Content-Type": "application/json"}
|
41 |
response = requests.post(api_endpoint, headers=headers, json=payload, timeout=30)
|
42 |
|
43 |
-
# 4. Process the response
|
44 |
response_json = response.json()
|
45 |
if response.status_code == 200:
|
46 |
-
if "data" in response_json
|
47 |
return response_json["data"][0]
|
48 |
elif "error" in response_json:
|
49 |
raise gr.Error(f"API returned an error: {response_json['error']}")
|
50 |
else:
|
51 |
-
# Handle non-200 responses with more detail
|
52 |
error_detail = response_json.get("error", "Unknown error.")
|
53 |
raise gr.Error(f"API Error (Status {response.status_code}): {error_detail}")
|
54 |
|
@@ -62,20 +55,17 @@ def call_api(server_endpoint_url: str, image: Image.Image) -> dict:
|
|
62 |
# ==============================================================================
|
63 |
# GRADIO INTERFACE
|
64 |
# ==============================================================================
|
65 |
-
|
66 |
with gr.Blocks(theme=gr.themes.Soft(), title="KeyLock API Client") as demo:
|
67 |
gr.Markdown("# π KeyLock API Client")
|
68 |
-
gr.Markdown(
|
69 |
-
"This UI calls the **Secure Decoder API**. It sends an encrypted image to the server for decryption."
|
70 |
-
)
|
71 |
|
72 |
with gr.Row():
|
73 |
with gr.Column(scale=1):
|
74 |
-
gr.Markdown("### 1. Configure Server
|
75 |
server_url_input = gr.Textbox(
|
76 |
-
label="
|
77 |
-
placeholder="https://your-
|
78 |
-
info="
|
79 |
)
|
80 |
|
81 |
gr.Markdown("### 2. Upload Encrypted Image")
|
|
|
12 |
# ==============================================================================
|
13 |
# CLIENT-SIDE API CALL LOGIC
|
14 |
# ==============================================================================
|
15 |
+
def call_api(server_base_url: str, image: Image.Image) -> dict:
|
|
|
16 |
"""
|
17 |
Takes a PIL image, converts it to base64, sends it to the specified server API,
|
18 |
and returns the decrypted JSON data.
|
19 |
"""
|
20 |
+
if not server_base_url or "hf.space" not in server_base_url:
|
21 |
+
raise gr.Error("Please provide a valid Hugging Face Space URL for the server API.")
|
|
|
22 |
if image is None:
|
23 |
raise gr.Error("Please upload an encrypted image.")
|
24 |
|
25 |
+
# --- CORRECTED API PATH ---
|
26 |
+
api_endpoint = f"{server_base_url.strip('/')}/run/keylock-auth-decoder"
|
27 |
logger.info(f"Client attempting to call API at: {api_endpoint}")
|
28 |
|
29 |
try:
|
|
|
30 |
with io.BytesIO() as buffer:
|
31 |
image.save(buffer, format="PNG")
|
32 |
image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
33 |
|
|
|
34 |
payload = {"data": [image_base64]}
|
|
|
|
|
35 |
headers = {"Content-Type": "application/json"}
|
36 |
response = requests.post(api_endpoint, headers=headers, json=payload, timeout=30)
|
37 |
|
|
|
38 |
response_json = response.json()
|
39 |
if response.status_code == 200:
|
40 |
+
if "data" in response_json:
|
41 |
return response_json["data"][0]
|
42 |
elif "error" in response_json:
|
43 |
raise gr.Error(f"API returned an error: {response_json['error']}")
|
44 |
else:
|
|
|
45 |
error_detail = response_json.get("error", "Unknown error.")
|
46 |
raise gr.Error(f"API Error (Status {response.status_code}): {error_detail}")
|
47 |
|
|
|
55 |
# ==============================================================================
|
56 |
# GRADIO INTERFACE
|
57 |
# ==============================================================================
|
|
|
58 |
with gr.Blocks(theme=gr.themes.Soft(), title="KeyLock API Client") as demo:
|
59 |
gr.Markdown("# π KeyLock API Client")
|
60 |
+
gr.Markdown("This UI calls the **Secure Decoder API**. It sends an encrypted image to the server for decryption.")
|
|
|
|
|
61 |
|
62 |
with gr.Row():
|
63 |
with gr.Column(scale=1):
|
64 |
+
gr.Markdown("### 1. Configure Server URL")
|
65 |
server_url_input = gr.Textbox(
|
66 |
+
label="Decoder API Server Base URL",
|
67 |
+
placeholder="https://your-name-keylock-auth.hf.space",
|
68 |
+
info="This is the direct URL of your server space, without any path."
|
69 |
)
|
70 |
|
71 |
gr.Markdown("### 2. Upload Encrypted Image")
|