andevs commited on
Commit
29ead4d
Β·
verified Β·
1 Parent(s): d50f723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -175
app.py CHANGED
@@ -1,155 +1,41 @@
1
  import gradio as gr
2
- import os
3
- import io
4
- import requests
5
- from PIL import Image
6
 
7
  # CONVERTLY - The Everything Converter
8
- # Working with Python 3.10
9
 
10
- HF_TOKEN = os.getenv("HF_TOKEN", "")
 
11
 
12
- API_URLS = {
13
- "ocr": "https://api-inference.huggingface.co/models/microsoft/trocr-large-printed",
14
- "remove_bg": "https://api-inference.huggingface.co/models/briaai/RMBG-1.4",
15
- "caption": "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base",
16
- "summarize": "https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
17
- }
18
-
19
- def query_hf(url, data):
20
- try:
21
- headers = {}
22
- if HF_TOKEN:
23
- headers["Authorization"] = f"Bearer {HF_TOKEN}"
24
- response = requests.post(url, headers=headers, data=data, timeout=30)
25
- if response.status_code == 200:
26
- return response.content
27
- return None
28
- except Exception as e:
29
- print(f"Error: {e}")
30
- return None
31
-
32
- def extract_text(image):
33
  if image is None:
34
- return "Please upload an image first"
35
- try:
36
- img_bytes = io.BytesIO()
37
- image.save(img_bytes, format='PNG')
38
- result = query_hf(API_URLS["ocr"], img_bytes.getvalue())
39
- if result:
40
- try:
41
- return result.decode('utf-8')
42
- except:
43
- return "Text extracted successfully!"
44
- return "OCR failed. Please try again."
45
- except Exception as e:
46
- return f"Error: {str(e)}"
47
 
48
- def remove_background(image):
49
  if image is None:
50
  return None
51
- try:
52
- img_bytes = io.BytesIO()
53
- image.save(img_bytes, format='PNG')
54
- result = query_hf(API_URLS["remove_bg"], img_bytes.getvalue())
55
- if result:
56
- try:
57
- return Image.open(io.BytesIO(result))
58
- except:
59
- return image
60
- return image
61
- except:
62
- return image
63
-
64
- def caption_image(image):
65
- if image is None:
66
- return "No image uploaded"
67
- try:
68
- img_bytes = io.BytesIO()
69
- image.save(img_bytes, format='PNG')
70
- result = query_hf(API_URLS["caption"], img_bytes.getvalue())
71
- if result:
72
- try:
73
- return result.decode('utf-8')
74
- except:
75
- return "Caption generated"
76
- return "Caption generation failed"
77
- except Exception as e:
78
- return f"Error: {str(e)}"
79
 
80
- def summarize_text(text):
81
- if not text or len(text.strip()) < 50:
82
- return "Please enter at least 50 characters"
83
- try:
84
- if len(text) > 2000:
85
- text = text[:2000]
86
- result = query_hf(API_URLS["summarize"], text.encode())
87
- if result:
88
- try:
89
- return result.decode('utf-8')
90
- except:
91
- return text[:300] + "..."
92
- return "Summarization failed"
93
- except Exception as e:
94
- return f"Error: {str(e)}"
95
 
96
- def vectorize_logo(image):
97
  if image is None:
98
- return None, "Please upload a logo image"
99
- try:
100
- width, height = image.size
101
- svg_code = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}">
102
- <rect width="{width}" height="{height}" fill="#ffffff"/>
103
- <defs>
104
- <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
105
- <stop offset="0%" stop-color="#667eea"/>
106
- <stop offset="100%" stop-color="#764ba2"/>
107
- </linearGradient>
108
- </defs>
109
- <g transform="translate({width//2}, {height//2})">
110
- <circle cx="0" cy="0" r="{min(width, height)//3}" fill="url(#g)" opacity="0.9"/>
111
- <path d="M -40 -30 L 40 -30 L 0 40 Z" fill="#ffffff" opacity="0.95"/>
112
- <circle cx="0" cy="0" r="{min(width, height)//4}" fill="none" stroke="#ffffff" stroke-width="3"/>
113
- </g>
114
- <text x="{width//2}" y="{height - 20}" text-anchor="middle" font-family="Arial" font-size="14" fill="#667eea">CONVERTLY</text>
115
- </svg>'''
116
- return svg_code, "Logo vectorized successfully!"
117
- except Exception as e:
118
- return None, f"Error: {str(e)}"
119
-
120
- def convert_format(image, target_format):
121
- if image is None:
122
- return None
123
- try:
124
- output = io.BytesIO()
125
- fmt = target_format.upper()
126
- if fmt == "JPG":
127
- fmt = "JPEG"
128
- if image.mode in ('RGBA', 'P'):
129
- image = image.convert('RGB')
130
- image.save(output, format=fmt, quality=95)
131
- output.seek(0)
132
- return Image.open(output)
133
- except:
134
- return image
135
-
136
- def resize_image(image, width, height):
137
- if image is None:
138
- return None
139
- try:
140
- return image.resize((int(width), int(height)), Image.LANCZOS)
141
- except:
142
- return image
143
 
144
  # CSS
145
  css = """
146
  .gradio-container { max-width: 1200px; margin: auto; }
 
147
  """
148
 
149
  with gr.Blocks(css=css, title="CONVERTLY") as demo:
150
  gr.Markdown("""
151
  # πŸš€ CONVERTLY
152
- **Convert Anything. Into Everything.** β€” Powered by Hugging Face AI
153
  """)
154
 
155
  with gr.Tabs():
@@ -159,9 +45,9 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
159
  logo_input = gr.Image(type="pil", label="Upload Logo", height=300)
160
  vector_btn = gr.Button("Vectorize Now", variant="primary")
161
  with gr.Column():
162
- svg_output = gr.Code(label="SVG Code", lines=15)
163
  vector_msg = gr.Textbox(label="Status", lines=2)
164
- vector_btn.click(vectorize_logo, inputs=[logo_input], outputs=[svg_output, vector_msg])
165
 
166
  with gr.TabItem("πŸ“ Extract Text"):
167
  with gr.Row():
@@ -169,74 +55,44 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
169
  ocr_image = gr.Image(type="pil", label="Upload Image", height=300)
170
  ocr_btn = gr.Button("Extract Text", variant="primary")
171
  with gr.Column():
172
- ocr_output = gr.Textbox(label="Extracted Text", lines=12)
173
- ocr_btn.click(extract_text, inputs=[ocr_image], outputs=[ocr_output])
174
 
175
  with gr.TabItem("πŸ–ΌοΈ Remove Background"):
176
  with gr.Row():
177
  with gr.Column():
178
- bg_input = gr.Image(type="pil", label="Original Image", height=300)
179
  bg_btn = gr.Button("Remove Background", variant="primary")
180
  with gr.Column():
181
  bg_output = gr.Image(label="Result", height=300)
182
- bg_btn.click(remove_background, inputs=[bg_input], outputs=[bg_output])
183
-
184
- with gr.TabItem("πŸ’¬ Caption Image"):
185
- with gr.Row():
186
- with gr.Column():
187
- cap_input = gr.Image(type="pil", label="Upload Image", height=300)
188
- cap_btn = gr.Button("Generate Caption", variant="primary")
189
- with gr.Column():
190
- cap_output = gr.Textbox(label="Caption", lines=5)
191
- cap_btn.click(caption_image, inputs=[cap_input], outputs=[cap_output])
192
 
193
  with gr.TabItem("πŸ“„ Summarize"):
194
  with gr.Row():
195
  with gr.Column():
196
- text_input = gr.Textbox(label="Text to Summarize", lines=12)
197
  sum_btn = gr.Button("Summarize", variant="primary")
198
  with gr.Column():
199
- sum_output = gr.Textbox(label="Summary", lines=8)
200
- sum_btn.click(summarize_text, inputs=[text_input], outputs=[sum_output])
201
-
202
- with gr.TabItem("πŸ”„ Image Converter"):
203
- with gr.Row():
204
- with gr.Column():
205
- conv_input = gr.Image(type="pil", label="Original", height=250)
206
- format_radio = gr.Radio(choices=["PNG", "JPG", "WEBP"], label="Format", value="PNG")
207
- conv_btn = gr.Button("Convert", variant="primary")
208
- with gr.Column():
209
- conv_output = gr.Image(label="Converted", height=250)
210
- conv_btn.click(convert_format, inputs=[conv_input, format_radio], outputs=[conv_output])
211
-
212
- with gr.TabItem("πŸ“ Resize"):
213
- with gr.Row():
214
- with gr.Column():
215
- resize_input = gr.Image(type="pil", label="Original", height=250)
216
- width_slider = gr.Slider(50, 1000, value=500, label="Width")
217
- height_slider = gr.Slider(50, 1000, value=500, label="Height")
218
- resize_btn = gr.Button("Resize", variant="primary")
219
- with gr.Column():
220
- resize_output = gr.Image(label="Resized", height=250)
221
- resize_btn.click(resize_image, inputs=[resize_input, width_slider, height_slider], outputs=[resize_output])
222
 
223
  with gr.TabItem("πŸ”§ All Tools"):
224
  gr.Markdown("""
225
- ### πŸ“š 200+ Tools Available
226
 
227
  | Category | Count | Tools |
228
  |----------|-------|-------|
229
  | PDF Tools | 45 | Convert, Merge, Split, Compress |
230
  | Image Tools | 38 | Format, Resize, Crop, Optimize |
231
  | AI Tools | 42 | Vectorizer, Remove BG, Upscale |
232
- | OCR Tools | 18 | Extract Text, Tables, Handwriting |
233
  | Audio Tools | 24 | Conversion, Transcription |
234
- | Video Tools | 31 | Format, Compress, Extract |
235
- | Developer | 29 | JSON, CSV, XML, Base64 |
236
  | Student | 22 | Study Guides, Quizzes |
237
  """)
238
 
239
  gr.Markdown("---\n### πŸš€ Free β€’ No Registration β€’ AI-Powered")
240
 
241
  if __name__ == "__main__":
242
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
 
 
 
 
2
 
3
  # CONVERTLY - The Everything Converter
4
+ # Super simple version that definitely works
5
 
6
+ def greet(name):
7
+ return f"Hello {name}! Welcome to CONVERTLY! πŸš€"
8
 
9
+ def extract_text_demo(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  if image is None:
11
+ return "Please upload an image"
12
+ return "βœ… OCR would extract text from this image!"
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ def remove_bg_demo(image):
15
  if image is None:
16
  return None
17
+ return image # Just return the original for demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ def summarize_demo(text):
20
+ if not text:
21
+ return "Please enter some text"
22
+ return f"βœ… Summary: {text[:100]}..."
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ def vectorize_demo(image):
25
  if image is None:
26
+ return None, "Please upload a logo"
27
+ return "<svg>Vectorized Logo</svg>", "βœ… Demo vectorization complete!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # CSS
30
  css = """
31
  .gradio-container { max-width: 1200px; margin: auto; }
32
+ h1 { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
33
  """
34
 
35
  with gr.Blocks(css=css, title="CONVERTLY") as demo:
36
  gr.Markdown("""
37
  # πŸš€ CONVERTLY
38
+ **Convert Anything. Into Everything.** β€” Coming Soon!
39
  """)
40
 
41
  with gr.Tabs():
 
45
  logo_input = gr.Image(type="pil", label="Upload Logo", height=300)
46
  vector_btn = gr.Button("Vectorize Now", variant="primary")
47
  with gr.Column():
48
+ svg_output = gr.Code(label="SVG Code", lines=10)
49
  vector_msg = gr.Textbox(label="Status", lines=2)
50
+ vector_btn.click(vectorize_demo, inputs=[logo_input], outputs=[svg_output, vector_msg])
51
 
52
  with gr.TabItem("πŸ“ Extract Text"):
53
  with gr.Row():
 
55
  ocr_image = gr.Image(type="pil", label="Upload Image", height=300)
56
  ocr_btn = gr.Button("Extract Text", variant="primary")
57
  with gr.Column():
58
+ ocr_output = gr.Textbox(label="Extracted Text", lines=8)
59
+ ocr_btn.click(extract_text_demo, inputs=[ocr_image], outputs=[ocr_output])
60
 
61
  with gr.TabItem("πŸ–ΌοΈ Remove Background"):
62
  with gr.Row():
63
  with gr.Column():
64
+ bg_input = gr.Image(type="pil", label="Original", height=300)
65
  bg_btn = gr.Button("Remove Background", variant="primary")
66
  with gr.Column():
67
  bg_output = gr.Image(label="Result", height=300)
68
+ bg_btn.click(remove_bg_demo, inputs=[bg_input], outputs=[bg_output])
 
 
 
 
 
 
 
 
 
69
 
70
  with gr.TabItem("πŸ“„ Summarize"):
71
  with gr.Row():
72
  with gr.Column():
73
+ text_input = gr.Textbox(label="Text", lines=8)
74
  sum_btn = gr.Button("Summarize", variant="primary")
75
  with gr.Column():
76
+ sum_output = gr.Textbox(label="Summary", lines=5)
77
+ sum_btn.click(summarize_demo, inputs=[text_input], outputs=[sum_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  with gr.TabItem("πŸ”§ All Tools"):
80
  gr.Markdown("""
81
+ ### πŸ“š 200+ Tools Coming Soon!
82
 
83
  | Category | Count | Tools |
84
  |----------|-------|-------|
85
  | PDF Tools | 45 | Convert, Merge, Split, Compress |
86
  | Image Tools | 38 | Format, Resize, Crop, Optimize |
87
  | AI Tools | 42 | Vectorizer, Remove BG, Upscale |
88
+ | OCR Tools | 18 | Extract Text, Tables |
89
  | Audio Tools | 24 | Conversion, Transcription |
90
+ | Video Tools | 31 | Format, Compress |
91
+ | Developer | 29 | JSON, CSV, XML |
92
  | Student | 22 | Study Guides, Quizzes |
93
  """)
94
 
95
  gr.Markdown("---\n### πŸš€ Free β€’ No Registration β€’ AI-Powered")
96
 
97
  if __name__ == "__main__":
98
+ demo.launch()