vineet124jig commited on
Commit
52caa2a
·
verified ·
1 Parent(s): 507c78b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +148 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
12
+ # ----------------- JigsawStack API Wrasppers ------------------
13
+
14
+ def summarize(text, url, file_store_key, summary_type, max_points, max_characters):
15
+ # Validate that at least one input method is provided
16
+ if not text and not url and not file_store_key:
17
+ return "Error: Please provide either text, URL, or file store key"
18
+
19
+ payload = {}
20
+
21
+ # Add text if provided
22
+ if text and text.strip():
23
+ payload["text"] = text.strip()
24
+
25
+ # Add URL if provided
26
+ if url and url.strip():
27
+ payload["url"] = url.strip()
28
+
29
+ # Add file store key if provided
30
+ if file_store_key and file_store_key.strip():
31
+ payload["file_store_key"] = file_store_key.strip()
32
+
33
+ # Add summary type
34
+ if summary_type:
35
+ payload["type"] = summary_type
36
+
37
+ # Add max points if provided and type is points
38
+ if summary_type == "points" and max_points:
39
+ try:
40
+ max_points_int = int(max_points)
41
+ if 1 <= max_points_int <= 100:
42
+ payload["max_points"] = max_points_int
43
+ else:
44
+ return "Error: max_points must be between 1 and 100"
45
+ except ValueError:
46
+ return "Error: max_points must be a valid number"
47
+
48
+ # Add max characters if provided
49
+ if max_characters:
50
+ try:
51
+ max_chars_int = int(max_characters)
52
+ if max_chars_int > 0:
53
+ payload["max_characters"] = max_chars_int
54
+ else:
55
+ return "Error: max_characters must be greater than 0"
56
+ except ValueError:
57
+ return "Error: max_characters must be a valid number"
58
+
59
+ try:
60
+ print(payload)
61
+ r = requests.post(f"{BASE_URL}/ai/summary", headers=headers, json=payload)
62
+ r.raise_for_status()
63
+ result = r.json()
64
+
65
+ if not result.get("success"):
66
+ return f"Error: {result.get('message', 'Unknown error')}"
67
+
68
+ summary = result.get("summary", "")
69
+
70
+ # Format the output based on type
71
+ if summary_type == "points" and isinstance(summary, list):
72
+ return "\n• " + "\n• ".join(summary)
73
+ else:
74
+ return summary
75
+
76
+ except requests.exceptions.RequestException as req_err:
77
+ return f"Request failed: {str(req_err)}"
78
+ except Exception as e:
79
+ return f"Unexpected error: {str(e)}"
80
+
81
+ # ----------------- Gradio UI ------------------
82
+
83
+ with gr.Blocks() as demo:
84
+ gr.Markdown("""
85
+ <div style='text-align: center; margin-bottom: 24px;'>
86
+ <h1 style='font-size:2.2em; margin-bottom: 0.2em;'>🧩 JigsawStack Image Translation</h1>
87
+ <p style='font-size:1.2em; margin-top: 0;'>Extract and translate text from images into multiple languages.</p>
88
+ <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>
89
+ </div>
90
+ """)
91
+
92
+ with gr.Row():
93
+ with gr.Column():
94
+ gr.Markdown("#### Input Method (select one)")
95
+ input_method = gr.Radio(
96
+ choices=["Text", "URL", "File Store Key"],
97
+ label="Choose Input Method",
98
+ value="Text"
99
+ )
100
+ # Conditional inputs based on selection
101
+ long_text = gr.Textbox(label="Text to Summarize", lines=8, placeholder="Enter your text here...")
102
+ url_input = gr.Textbox(label="Document URL (PDF)", placeholder="https://example.com/document.pdf", visible=False)
103
+ file_key = gr.Textbox(label="File Store Key", placeholder="your-file-store-key", visible=False)
104
+
105
+ with gr.Column():
106
+ gr.Markdown("#### Summary Options")
107
+ summary_type = gr.Radio(
108
+ choices=["text", "points"],
109
+ label="Summary Format",
110
+ value="text",
111
+ info="Text: continuous paragraph | Points: bullet points"
112
+ )
113
+ max_points = gr.Slider(
114
+ label="Max Points (1-100)",
115
+ value=5,
116
+ minimum=1,
117
+ maximum=100,
118
+ step=1,
119
+ info="Only applies when format is 'points'"
120
+ )
121
+ max_characters = gr.Number(
122
+ label="Max Characters",
123
+ value=200,
124
+ minimum=1,
125
+ info="Optional: limit summary length"
126
+ )
127
+
128
+ summary_btn = gr.Button("Generate Summary", variant="primary")
129
+ summary = gr.Textbox(label="Generated Summary", lines=10)
130
+
131
+ # Function to show/hide input groups based on selection
132
+ def update_input_visibility(method):
133
+ if method == "Text":
134
+ return gr.Textbox(visible=True), gr.Textbox(visible=False), gr.Textbox(visible=False)
135
+ elif method == "URL":
136
+ return gr.Textbox(visible=False), gr.Textbox(visible=True), gr.Textbox(visible=False)
137
+ elif method == "File Store Key":
138
+ return gr.Textbox(visible=False), gr.Textbox(visible=False), gr.Textbox(visible=True)
139
+ else:
140
+ return gr.Textbox(visible=True), gr.Textbox(visible=False), gr.Textbox(visible=False)
141
+
142
+ input_method.change(
143
+ update_input_visibility,
144
+ inputs=input_method,
145
+ outputs=[long_text, url_input, file_key]
146
+ )
147
+
148
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ requests
3
+ Pillow