vineet124jig commited on
Commit
8cd48c4
ยท
verified ยท
1 Parent(s): 4a23b00

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from PIL import Image
4
+ import io
5
+
6
+ BASE_URL = "https://api.jigsawstack.com/v1"
7
+ headers = {"x-api-key": ""}
8
+
9
+ # ----------------- JigsawStack API Wrappers ------------------
10
+
11
+
12
+ def translate_text(text_input, target_lang):
13
+ # Validate required inputs
14
+ if not text_input or not text_input.strip():
15
+ return {"error": "Text input is required"}
16
+
17
+ if not target_lang or not target_lang.strip():
18
+ return {"error": "Target language code is required"}
19
+
20
+ # Handle both single string and comma-separated multiple phrases
21
+ if "," in text_input and not text_input.strip().startswith("["):
22
+ text_payload = [t.strip() for t in text_input.split(",") if t.strip()]
23
+ else:
24
+ text_payload = text_input.strip()
25
+
26
+ payload = {
27
+ "text": text_payload,
28
+ "target_language": target_lang.strip()
29
+ }
30
+
31
+ try:
32
+ r = requests.post(f"{BASE_URL}/ai/translate", headers=headers, json=payload)
33
+ r.raise_for_status()
34
+ data = r.json()
35
+
36
+ if not data.get("success"):
37
+ return {"error": "Translation failed", "response": data}
38
+
39
+ # Return a more comprehensive result matching the API response
40
+ result = {
41
+ "success": True,
42
+ "translated_text": data.get("translated_text")
43
+ }
44
+
45
+ return result
46
+
47
+ except requests.exceptions.RequestException as req_err:
48
+ return {"error": "Request failed", "message": str(req_err)}
49
+ except Exception as e:
50
+ return {"error": "Unexpected error", "message": str(e)}
51
+
52
+ # ----------------- Gradio UI ------------------
53
+
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown("""
56
+ <div style='text-align: center; margin-bottom: 24px;'>
57
+ <h1 style='font-size:2.2em; margin-bottom: 0.2em;'>๐Ÿงฉ JigsawStack Text Translation</h1>
58
+ <p style='font-size:1.2em; margin-top: 0;'>Translate text from one language to another with support for multiple text formats.</p>
59
+ <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>
60
+ </div>
61
+ """)
62
+
63
+ with gr.Row():
64
+ # Left Column: Inputs and Examples
65
+ with gr.Column(scale=2):
66
+ gr.Markdown("#### Input")
67
+ text_input = gr.Textbox(
68
+ label="Text to Translate",
69
+ lines=4,
70
+ placeholder="Enter text to translate (use commas to separate multiple phrases for batch translation)"
71
+ )
72
+ gr.Markdown("#### Translation Options")
73
+ target_lang = gr.Textbox(
74
+ label="Target Language Code",
75
+ placeholder="es (Spanish), fr (French), de (German), etc.",
76
+ info="Common codes: es, fr, de, hi, ja, ko, zh, ar, ru, pt"
77
+ )
78
+ gr.Markdown("#### Quick Examples")
79
+ gr.Markdown("Click any example below to auto-fill the fields:")
80
+ with gr.Row():
81
+ example_btn1 = gr.Button("๐Ÿ‡ช๐Ÿ‡ธ Hello World โ†’ Spanish", size="sm")
82
+ example_btn2 = gr.Button("๐Ÿ‡ซ๐Ÿ‡ท Good morning โ†’ French", size="sm")
83
+ example_btn3 = gr.Button("๐Ÿ‡ฉ๐Ÿ‡ช Thank you โ†’ German", size="sm")
84
+ with gr.Row():
85
+ example_btn4 = gr.Button("๐Ÿ‡ฏ๐Ÿ‡ต How are you? โ†’ Japanese", size="sm")
86
+ example_btn5 = gr.Button("๐Ÿ‡จ๐Ÿ‡ณ Welcome โ†’ Chinese", size="sm")
87
+ example_btn6 = gr.Button("๐Ÿ‡ฐ๐Ÿ‡ท Goodbye โ†’ Korean", size="sm")
88
+ with gr.Row():
89
+ example_btn7 = gr.Button("๐Ÿ‡ฎ๐Ÿ‡ณ Batch: Hello, Goodbye, Thank you โ†’ Hindi", size="sm")
90
+ example_btn8 = gr.Button("๐Ÿ‡ท๐Ÿ‡บ Business: Meeting, Project, Deadline โ†’ Russian", size="sm")
91
+ gr.Markdown("") # Spacer
92
+ translate_btn = gr.Button("Translate", variant="primary")
93
+
94
+ # Middle Column: Supported Languages
95
+ with gr.Column(scale=1):
96
+ gr.Markdown("#### Supported Languages")
97
+ gr.Markdown("""
98
+ **Common Language Codes:**
99
+ - `es` - Spanish
100
+ - `fr` - French
101
+ - `de` - German
102
+ - `hi` - Hindi
103
+ - `ja` - Japanese
104
+ - `ko` - Korean
105
+ - `zh` - Chinese
106
+ - `ar` - Arabic
107
+ - `ru` - Russian
108
+ - `pt` - Portuguese
109
+ - `tr` - Turkish
110
+ - `bn` - Bengali
111
+ - `fi` - Finnish
112
+ - `sw` - Swahili
113
+
114
+ *For a complete list of 162+ supported languages, visit the [Language Codes Reference](https://jigsawstack.com/docs/additional-resources/languages)*
115
+ """)
116
+
117
+ # Example functions to auto-fill fields
118
+ def fill_example_1():
119
+ return "Hello World", "es"
120
+ def fill_example_2():
121
+ return "Good morning", "fr"
122
+ def fill_example_3():
123
+ return "Thank you", "de"
124
+ def fill_example_4():
125
+ return "How are you?", "ja"
126
+ def fill_example_5():
127
+ return "Welcome", "zh"
128
+ def fill_example_6():
129
+ return "Goodbye", "ko"
130
+ def fill_example_7():
131
+ return "Hello, Goodbye, Thank you", "hi"
132
+ def fill_example_8():
133
+ return "Meeting, Project, Deadline", "ru"
134
+
135
+ # Connect example buttons to auto-fill functions
136
+ example_btn1.click(fill_example_1, outputs=[text_input, target_lang])
137
+ example_btn2.click(fill_example_2, outputs=[text_input, target_lang])
138
+ example_btn3.click(fill_example_3, outputs=[text_input, target_lang])
139
+ example_btn4.click(fill_example_4, outputs=[text_input, target_lang])
140
+ example_btn5.click(fill_example_5, outputs=[text_input, target_lang])
141
+ example_btn6.click(fill_example_6, outputs=[text_input, target_lang])
142
+ example_btn7.click(fill_example_7, outputs=[text_input, target_lang])
143
+ example_btn8.click(fill_example_8, outputs=[text_input, target_lang])
144
+
145
+ gr.Markdown("#### Translation Result")
146
+ translate_result = gr.JSON()
147
+
148
+ translate_btn.click(
149
+ translate_text,
150
+ inputs=[text_input, target_lang],
151
+ outputs=translate_result
152
+ )
153
+
154
+
155
+
156
+ demo.launch()