File size: 6,164 Bytes
8cd48c4
 
 
 
f26ceea
8cd48c4
 
f26ceea
8cd48c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cb05ef
8cd48c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gradio as gr
import requests
from PIL import Image
import io
import os

BASE_URL = "https://api.jigsawstack.com/v1"
headers = {"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")}

# ----------------- JigsawStack API Wrappers ------------------


def translate_text(text_input, target_lang):
    # Validate required inputs
    if not text_input or not text_input.strip():
        return {"error": "Text input is required"}
    
    if not target_lang or not target_lang.strip():
        return {"error": "Target language code is required"}
    
    # Handle both single string and comma-separated multiple phrases
    if "," in text_input and not text_input.strip().startswith("["):
        text_payload = [t.strip() for t in text_input.split(",") if t.strip()]
    else:
        text_payload = text_input.strip()

    payload = {
        "text": text_payload,
        "target_language": target_lang.strip()
    }

    try:
        r = requests.post(f"{BASE_URL}/ai/translate", headers=headers, json=payload)
        r.raise_for_status()
        data = r.json()

        if not data.get("success"):
            return {"error": "Translation failed", "response": data}

        # Return a more comprehensive result matching the API response
        result = {
            "success": True,
            "translated_text": data.get("translated_text")
        }
        
        return result

    except requests.exceptions.RequestException as req_err:
        return {"error": "Request failed", "message": str(req_err)}
    except Exception as e:
        return {"error": "Unexpected error", "message": str(e)}

# ----------------- Gradio UI ------------------

with gr.Blocks() as demo:
    gr.Markdown("""
    <div style='text-align: center; margin-bottom: 24px;'>
        <h1 style='font-size:2.2em; margin-bottom: 0.2em;'>๐Ÿงฉ Text Translation</h1>
        <p style='font-size:1.2em; margin-top: 0;'>Translate text from one language to another with support for multiple text formats.</p>
        <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/translate' target='_blank'>documentation</a>.</p>
    </div>
    """)

    with gr.Row():
        # Left Column: Inputs and Examples
        with gr.Column(scale=2):
            gr.Markdown("#### Input")
            text_input = gr.Textbox(
                label="Text to Translate", 
                lines=4, 
                placeholder="Enter text to translate (use commas to separate multiple phrases for batch translation)"
            )
            gr.Markdown("#### Translation Options")
            target_lang = gr.Textbox(
                label="Target Language Code", 
                placeholder="es (Spanish), fr (French), de (German), etc.",
                info="Common codes: es, fr, de, hi, ja, ko, zh, ar, ru, pt"
            )
            gr.Markdown("#### Quick Examples")
            gr.Markdown("Click any example below to auto-fill the fields:")
            with gr.Row():
                example_btn1 = gr.Button("๐Ÿ‡ช๐Ÿ‡ธ Hello World โ†’ Spanish", size="sm")
                example_btn2 = gr.Button("๐Ÿ‡ซ๐Ÿ‡ท Good morning โ†’ French", size="sm")
                example_btn3 = gr.Button("๐Ÿ‡ฉ๐Ÿ‡ช Thank you โ†’ German", size="sm")
            with gr.Row():
                example_btn4 = gr.Button("๐Ÿ‡ฏ๐Ÿ‡ต How are you? โ†’ Japanese", size="sm")
                example_btn5 = gr.Button("๐Ÿ‡จ๐Ÿ‡ณ Welcome โ†’ Chinese", size="sm")
                example_btn6 = gr.Button("๐Ÿ‡ฐ๐Ÿ‡ท Goodbye โ†’ Korean", size="sm")
            with gr.Row():
                example_btn7 = gr.Button("๐Ÿ‡ฎ๐Ÿ‡ณ Batch: Hello, Goodbye, Thank you โ†’ Hindi", size="sm")
                example_btn8 = gr.Button("๐Ÿ‡ท๐Ÿ‡บ Business: Meeting, Project, Deadline โ†’ Russian", size="sm")
            gr.Markdown("")  # Spacer
            translate_btn = gr.Button("Translate", variant="primary")

        # Middle Column: Supported Languages
        with gr.Column(scale=1):
            gr.Markdown("#### Supported Languages")
            gr.Markdown("""
            **Common Language Codes:**
            - `es` - Spanish
            - `fr` - French  
            - `de` - German
            - `hi` - Hindi
            - `ja` - Japanese
            - `ko` - Korean
            - `zh` - Chinese
            - `ar` - Arabic
            - `ru` - Russian
            - `pt` - Portuguese
            - `tr` - Turkish
            - `bn` - Bengali
            - `fi` - Finnish
            - `sw` - Swahili
            
            *For a complete list of 162+ supported languages, visit the [Language Codes Reference](https://jigsawstack.com/docs/additional-resources/languages)*
            """)

    # Example functions to auto-fill fields
    def fill_example_1():
        return "Hello World", "es"
    def fill_example_2():
        return "Good morning", "fr"
    def fill_example_3():
        return "Thank you", "de"
    def fill_example_4():
        return "How are you?", "ja"
    def fill_example_5():
        return "Welcome", "zh"
    def fill_example_6():
        return "Goodbye", "ko"
    def fill_example_7():
        return "Hello, Goodbye, Thank you", "hi"
    def fill_example_8():
        return "Meeting, Project, Deadline", "ru"

    # Connect example buttons to auto-fill functions
    example_btn1.click(fill_example_1, outputs=[text_input, target_lang])
    example_btn2.click(fill_example_2, outputs=[text_input, target_lang])
    example_btn3.click(fill_example_3, outputs=[text_input, target_lang])
    example_btn4.click(fill_example_4, outputs=[text_input, target_lang])
    example_btn5.click(fill_example_5, outputs=[text_input, target_lang])
    example_btn6.click(fill_example_6, outputs=[text_input, target_lang])
    example_btn7.click(fill_example_7, outputs=[text_input, target_lang])
    example_btn8.click(fill_example_8, outputs=[text_input, target_lang])

    gr.Markdown("#### Translation Result")
    translate_result = gr.JSON()

    translate_btn.click(
        translate_text,
        inputs=[text_input, target_lang],
        outputs=translate_result
    )



demo.launch()