Spaces:
Running
Running
Upload 2 files
Browse files- app.py +113 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import os
|
6 |
+
BASE_URL = "https://api.jigsawstack.com/v1"
|
7 |
+
|
8 |
+
headers = {"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")}
|
9 |
+
|
10 |
+
|
11 |
+
# ----------------- JigsawStack API Wrappers ------------------
|
12 |
+
|
13 |
+
def image_translate(source_type, image_url, file_store_key, target_language):
|
14 |
+
if not target_language or not target_language.strip():
|
15 |
+
return "Error: Target language is required.", None
|
16 |
+
|
17 |
+
payload = {"target_language": target_language.strip()}
|
18 |
+
|
19 |
+
if source_type == "URL":
|
20 |
+
if not image_url or not image_url.strip():
|
21 |
+
return "Error: Image URL is required.", None
|
22 |
+
payload["url"] = image_url.strip()
|
23 |
+
elif source_type == "File Store Key":
|
24 |
+
if not file_store_key or not file_store_key.strip():
|
25 |
+
return "Error: File Store Key is required.", None
|
26 |
+
payload["file_store_key"] = file_store_key.strip()
|
27 |
+
else:
|
28 |
+
return "Error: Invalid image source selected.", None
|
29 |
+
|
30 |
+
try:
|
31 |
+
response = requests.post(
|
32 |
+
f"{BASE_URL}/ai/translate/image",
|
33 |
+
headers=headers,
|
34 |
+
json=payload
|
35 |
+
)
|
36 |
+
response.raise_for_status()
|
37 |
+
|
38 |
+
if response.headers.get("content-type", "").startswith("image/"):
|
39 |
+
# The API returns the translated image directly
|
40 |
+
from PIL import Image
|
41 |
+
import io
|
42 |
+
image = Image.open(io.BytesIO(response.content))
|
43 |
+
return "✅ Image translated successfully.", image
|
44 |
+
else:
|
45 |
+
try:
|
46 |
+
error_data = response.json()
|
47 |
+
return f"Error: API returned an error - {error_data.get('message', 'Unknown error')}", None
|
48 |
+
except:
|
49 |
+
return "Error: Received an unexpected response from the API.", None
|
50 |
+
|
51 |
+
except requests.exceptions.RequestException as e:
|
52 |
+
return f"Request failed: {str(e)}", None
|
53 |
+
except Exception as e:
|
54 |
+
return f"An unexpected error occurred: {str(e)}", None
|
55 |
+
|
56 |
+
# ----------------- Gradio UI ------------------
|
57 |
+
|
58 |
+
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown("""
|
60 |
+
<div style='text-align: center; margin-bottom: 24px;'>
|
61 |
+
<h1 style='font-size:2.2em; margin-bottom: 0.2em;'>🧩 JigsawStack Image Translation</h1>
|
62 |
+
<p style='font-size:1.2em; margin-top: 0;'>Extract and translate text from images into multiple languages.</p>
|
63 |
+
<p style='font-size:1em; margin-top: 0.5em;'>For more details and API usage, see the <a href='https://jigsawstack.com/docs/api-reference/ai/translate/image-translate' target='_blank'>documentation</a>.</p>
|
64 |
+
</div>
|
65 |
+
""")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column():
|
69 |
+
gr.Markdown("#### Image Source")
|
70 |
+
it_source_type = gr.Radio(
|
71 |
+
choices=["URL", "File Store Key"],
|
72 |
+
label="Choose Image Source",
|
73 |
+
value="URL"
|
74 |
+
)
|
75 |
+
it_image_url = gr.Textbox(
|
76 |
+
label="Image URL",
|
77 |
+
placeholder="Enter the URL of the image..."
|
78 |
+
)
|
79 |
+
it_file_key = gr.Textbox(
|
80 |
+
label="File Store Key",
|
81 |
+
placeholder="your-file-store-key",
|
82 |
+
visible=False
|
83 |
+
)
|
84 |
+
|
85 |
+
gr.Markdown("#### Translation Options")
|
86 |
+
it_target_language = gr.Textbox(
|
87 |
+
label="Target Language Code",
|
88 |
+
placeholder="es, fr, de, ja, etc."
|
89 |
+
)
|
90 |
+
it_btn = gr.Button("Translate Image", variant="primary")
|
91 |
+
|
92 |
+
with gr.Column():
|
93 |
+
gr.Markdown("#### Results")
|
94 |
+
it_status = gr.Textbox(label="Status", interactive=False)
|
95 |
+
it_translated_image = gr.Image(label="Translated Image")
|
96 |
+
|
97 |
+
def update_it_source(source_type):
|
98 |
+
is_url = source_type == "URL"
|
99 |
+
return gr.update(visible=is_url), gr.update(visible=not is_url)
|
100 |
+
|
101 |
+
it_source_type.change(
|
102 |
+
update_it_source,
|
103 |
+
inputs=it_source_type,
|
104 |
+
outputs=[it_image_url, it_file_key]
|
105 |
+
)
|
106 |
+
|
107 |
+
it_btn.click(
|
108 |
+
image_translate,
|
109 |
+
inputs=[it_source_type, it_image_url, it_file_key, it_target_language],
|
110 |
+
outputs=[it_status, it_translated_image]
|
111 |
+
)
|
112 |
+
|
113 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
requests
|
3 |
+
Pillow
|