Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import base64
|
4 |
from PIL import Image
|
|
|
5 |
import io
|
6 |
import logging
|
7 |
|
@@ -10,62 +10,71 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(level
|
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
# ==============================================================================
|
13 |
-
# CLIENT-SIDE API CALL LOGIC
|
14 |
# ==============================================================================
|
15 |
-
|
|
|
16 |
"""
|
17 |
-
|
18 |
-
and returns the decrypted JSON data.
|
19 |
"""
|
20 |
-
|
21 |
-
|
|
|
22 |
if image is None:
|
23 |
raise gr.Error("Please upload an encrypted image.")
|
24 |
|
25 |
-
|
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 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
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 |
|
48 |
-
except
|
49 |
-
|
50 |
-
|
|
|
51 |
except Exception as e:
|
|
|
52 |
logger.error(f"An unexpected client-side error occurred: {e}", exc_info=True)
|
53 |
raise gr.Error(f"An unexpected error occurred: {e}")
|
54 |
|
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(
|
|
|
|
|
|
|
61 |
|
62 |
with gr.Row():
|
63 |
with gr.Column(scale=1):
|
64 |
-
gr.Markdown("### 1. Configure Server
|
65 |
-
|
66 |
-
label="Decoder
|
67 |
-
|
68 |
-
info="This is the
|
69 |
)
|
70 |
|
71 |
gr.Markdown("### 2. Upload Encrypted Image")
|
@@ -78,7 +87,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="KeyLock API Client") as demo:
|
|
78 |
|
79 |
submit_button.click(
|
80 |
fn=call_api,
|
81 |
-
inputs=[
|
82 |
outputs=[json_output]
|
83 |
)
|
84 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio_client import Client, GradioClientError
|
|
|
3 |
from PIL import Image
|
4 |
+
import base64
|
5 |
import io
|
6 |
import logging
|
7 |
|
|
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
# ==============================================================================
|
13 |
+
# CLIENT-SIDE API CALL LOGIC (Using the Official Gradio Client)
|
14 |
# ==============================================================================
|
15 |
+
|
16 |
+
def call_api(server_space_id: str, image: Image.Image) -> dict:
|
17 |
"""
|
18 |
+
Uses the official gradio_client to call the secure decoder API.
|
|
|
19 |
"""
|
20 |
+
# --- Input Validation ---
|
21 |
+
if not server_space_id or "/" not in server_space_id:
|
22 |
+
raise gr.Error("Please provide the Server Space ID in the format 'username/space-name'.")
|
23 |
if image is None:
|
24 |
raise gr.Error("Please upload an encrypted image.")
|
25 |
|
26 |
+
logger.info(f"Initializing client for server space: {server_space_id}")
|
|
|
|
|
27 |
|
28 |
try:
|
29 |
+
# 1. Initialize the Gradio Client
|
30 |
+
# This points the client to your server space.
|
31 |
+
client = Client(src=server_space_id)
|
32 |
+
|
33 |
+
# 2. Convert the PIL Image to a base64 string
|
34 |
with io.BytesIO() as buffer:
|
35 |
image.save(buffer, format="PNG")
|
36 |
image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
37 |
|
38 |
+
# 3. Call the specific API endpoint on the server
|
39 |
+
# This matches the documentation you provided exactly.
|
40 |
+
logger.info(f"Calling api_name='/keylock-auth-decoder' on {server_space_id}")
|
41 |
+
result = client.predict(
|
42 |
+
# The keyword argument MUST match the server function's parameter name.
|
43 |
+
image_base64_string=image_base64,
|
44 |
+
api_name="/keylock-auth-decoder"
|
45 |
+
)
|
46 |
|
47 |
+
logger.info("Successfully received result from API.")
|
48 |
+
# The result from a successful predict call is the data itself.
|
49 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
except GradioClientError as e:
|
52 |
+
# Handle errors from the gradio_client library (e.g., 404, server errors)
|
53 |
+
logger.error(f"Gradio Client Error: {e}")
|
54 |
+
raise gr.Error(f"API Call Failed: {e}. Check if the Server Space ID is correct and the server is running.")
|
55 |
except Exception as e:
|
56 |
+
# Handle other unexpected errors
|
57 |
logger.error(f"An unexpected client-side error occurred: {e}", exc_info=True)
|
58 |
raise gr.Error(f"An unexpected error occurred: {e}")
|
59 |
|
60 |
# ==============================================================================
|
61 |
# GRADIO INTERFACE
|
62 |
# ==============================================================================
|
63 |
+
|
64 |
with gr.Blocks(theme=gr.themes.Soft(), title="KeyLock API Client") as demo:
|
65 |
gr.Markdown("# π KeyLock API Client")
|
66 |
+
gr.Markdown(
|
67 |
+
"This UI calls the **Secure Decoder API**. It sends an encrypted image to the server for decryption "
|
68 |
+
"using the official `gradio_client` library."
|
69 |
+
)
|
70 |
|
71 |
with gr.Row():
|
72 |
with gr.Column(scale=1):
|
73 |
+
gr.Markdown("### 1. Configure Server Space ID")
|
74 |
+
server_id_input = gr.Textbox(
|
75 |
+
label="Decoder Server Space ID",
|
76 |
+
value="broadfield-dev/KeyLock-Auth",
|
77 |
+
info="This is the Hugging Face ID of your server, e.g., 'username/space-name'."
|
78 |
)
|
79 |
|
80 |
gr.Markdown("### 2. Upload Encrypted Image")
|
|
|
87 |
|
88 |
submit_button.click(
|
89 |
fn=call_api,
|
90 |
+
inputs=[server_id_input, image_input],
|
91 |
outputs=[json_output]
|
92 |
)
|
93 |
|