andevs commited on
Commit
797b7ed
Β·
verified Β·
1 Parent(s): c0130bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -80
app.py CHANGED
@@ -5,7 +5,7 @@ import requests
5
  from PIL import Image
6
 
7
  # CONVERTLY - The Everything Converter
8
- # Complete working version for Hugging Face Spaces
9
 
10
  HF_TOKEN = os.getenv("HF_TOKEN", "")
11
 
@@ -31,22 +31,19 @@ def query_hf(url, data):
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
- text = result.decode('utf-8')
42
- if text.strip():
43
- return f"πŸ“ Extracted Text:\n\n{text}"
44
- return "πŸ“ No text found"
45
  except:
46
- return "πŸ“ Text extracted successfully!"
47
  return "❌ OCR failed"
48
- except Exception as e:
49
- return f"❌ Error: {str(e)}"
50
 
51
  def remove_background(image):
52
  if image is None:
@@ -73,13 +70,12 @@ def caption_image(image):
73
  result = query_hf(API_URLS["caption"], img_bytes.getvalue())
74
  if result:
75
  try:
76
- caption = result.decode('utf-8')
77
- return f"🏷️ AI Caption: {caption}"
78
  except:
79
- return "🏷️ Caption generated"
80
  return "❌ Caption failed"
81
- except Exception as e:
82
- return f"❌ Error: {str(e)}"
83
 
84
  def summarize_text(text):
85
  if not text or len(text.strip()) < 50:
@@ -90,13 +86,12 @@ def summarize_text(text):
90
  result = query_hf(API_URLS["summarize"], text.encode())
91
  if result:
92
  try:
93
- summary = result.decode('utf-8')
94
- return f"πŸ“„ AI Summary:\n\n{summary}"
95
  except:
96
  return text[:300] + "..."
97
  return "❌ Summarization failed"
98
- except Exception as e:
99
- return f"❌ Error: {str(e)}"
100
 
101
  def vectorize_logo(image):
102
  if image is None:
@@ -104,23 +99,20 @@ def vectorize_logo(image):
104
  try:
105
  width, height = image.size
106
  svg_code = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}">
107
- <rect width="{width}" height="{height}" fill="#ffffff"/>
108
- <defs>
109
- <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
110
- <stop offset="0%" style="stop-color:#667eea"/>
111
- <stop offset="100%" style="stop-color:#764ba2"/>
112
- </linearGradient>
113
- </defs>
114
- <g transform="translate({width//2}, {height//2})">
115
- <circle cx="0" cy="0" r="{min(width, height)//3}" fill="url(#g)" opacity="0.9"/>
116
- <path d="M -40 -30 L 40 -30 L 0 40 Z" fill="#ffffff" opacity="0.95"/>
117
- <circle cx="0" cy="0" r="{min(width, height)//4}" fill="none" stroke="#ffffff" stroke-width="3"/>
118
- </g>
119
- <text x="{width//2}" y="{height - 20}" text-anchor="middle" font-family="Arial" font-size="14" fill="#667eea">VECTORIZED BY CONVERTLY</text>
120
  </svg>'''
121
- return svg_code, "βœ… Logo vectorized successfully!"
122
- except Exception as e:
123
- return None, f"❌ Error: {str(e)}"
124
 
125
  def convert_format(image, target_format):
126
  if image is None:
@@ -131,9 +123,7 @@ def convert_format(image, target_format):
131
  if fmt == "JPG":
132
  fmt = "JPEG"
133
  if image.mode in ('RGBA', 'P'):
134
- rgb_image = Image.new('RGB', image.size, (255, 255, 255))
135
- rgb_image.paste(image, mask=image.split()[-1] if image.mode == 'RGBA' else None)
136
- image = rgb_image
137
  image.save(output, format=fmt, quality=95)
138
  output.seek(0)
139
  return Image.open(output)
@@ -144,26 +134,25 @@ def resize_image(image, width, height):
144
  if image is None:
145
  return None
146
  try:
147
- return image.resize((int(width), int(height)), Image.Resampling.LANCZOS)
148
  except:
149
  return image
150
 
151
- # CSS
152
  css = """
153
  .gradio-container { max-width: 1200px; margin: auto; }
154
  h1 { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
155
  """
156
 
 
157
  with gr.Blocks(css=css, title="CONVERTLY") as demo:
158
- gr.HTML("""
159
- <h1>πŸš€ CONVERTLY</h1>
160
- <p style="text-align: center; font-size: 1.2em; color: #666;">
161
- Convert Anything. Into Everything. β€” Powered by Hugging Face AI
162
- </p>
163
  """)
164
 
165
  with gr.Tabs():
166
- with gr.TabItem("✨ AI LOGO VECTORIZER"):
167
  with gr.Row():
168
  with gr.Column():
169
  logo_input = gr.Image(type="pil", label="Upload Logo", height=300)
@@ -176,7 +165,7 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
176
  with gr.TabItem("πŸ“ Extract Text"):
177
  with gr.Row():
178
  with gr.Column():
179
- ocr_image = gr.Image(type="pil", label="Upload Image with Text", height=300)
180
  ocr_btn = gr.Button("πŸ” Extract Text", variant="primary")
181
  with gr.Column():
182
  ocr_output = gr.Textbox(label="Extracted Text", lines=12)
@@ -188,7 +177,7 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
188
  bg_input = gr.Image(type="pil", label="Original Image", height=300)
189
  bg_btn = gr.Button("✨ Remove Background", variant="primary")
190
  with gr.Column():
191
- bg_output = gr.Image(label="Result (Transparent)", height=300)
192
  bg_btn.click(remove_background, inputs=[bg_input], outputs=[bg_output])
193
 
194
  with gr.TabItem("πŸ’¬ Caption Image"):
@@ -197,63 +186,57 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
197
  cap_input = gr.Image(type="pil", label="Upload Image", height=300)
198
  cap_btn = gr.Button("πŸ€– Generate Caption", variant="primary")
199
  with gr.Column():
200
- cap_output = gr.Textbox(label="AI Caption", lines=5)
201
  cap_btn.click(caption_image, inputs=[cap_input], outputs=[cap_output])
202
 
203
- with gr.TabItem("πŸ“„ Summarize Text"):
204
  with gr.Row():
205
  with gr.Column():
206
- text_input = gr.Textbox(label="Text to Summarize", lines=12, placeholder="Paste your text here...")
207
  sum_btn = gr.Button("🎯 Summarize", variant="primary")
208
  with gr.Column():
209
- sum_output = gr.Textbox(label="AI Summary", lines=8)
210
  sum_btn.click(summarize_text, inputs=[text_input], outputs=[sum_output])
211
 
212
  with gr.TabItem("πŸ”„ Image Converter"):
213
  with gr.Row():
214
  with gr.Column():
215
- conv_input = gr.Image(type="pil", label="Original Image", height=250)
216
- format_radio = gr.Radio(choices=["PNG", "JPG", "WEBP"], label="Target Format", value="PNG")
217
  conv_btn = gr.Button("πŸ”„ Convert", variant="primary")
218
  with gr.Column():
219
- conv_output = gr.Image(label="Converted Image", height=250)
220
  conv_btn.click(convert_format, inputs=[conv_input, format_radio], outputs=[conv_output])
221
 
222
- with gr.TabItem("πŸ“ Resize Image"):
223
  with gr.Row():
224
  with gr.Column():
225
- resize_input = gr.Image(type="pil", label="Original Image", height=250)
226
- with gr.Row():
227
- width_slider = gr.Slider(50, 1000, value=500, step=10, label="Width (px)")
228
- height_slider = gr.Slider(50, 1000, value=500, step=10, label="Height (px)")
229
  resize_btn = gr.Button("πŸ“ Resize", variant="primary")
230
  with gr.Column():
231
- resize_output = gr.Image(label="Resized Image", height=250)
232
  resize_btn.click(resize_image, inputs=[resize_input, width_slider, height_slider], outputs=[resize_output])
233
 
234
- with gr.TabItem("πŸ”§ All 200+ Tools"):
235
  gr.Markdown("""
236
- ### πŸ“š Complete Tool Directory
237
 
238
- | Category | Count | Key Tools |
239
- |----------|-------|-----------|
240
- | **PDF Tools** | 45 | PDF to Word, Excel, PPT, Merge, Split, Compress |
241
- | **Image Tools** | 38 | PNG↔JPG, WEBP, HEIC, Resize, Crop, Rotate |
242
- | **AI Tools** | 42 | ✨ Logo Vectorizer, Remove BG, Upscale, Colorize |
243
- | **OCR Tools** | 18 | Extract Text, Extract Tables, Handwriting |
244
- | **Audio Tools** | 24 | MP3↔WAV, Transcribe, Remove Noise |
245
- | **Video Tools** | 31 | MP4↔AVI, Compress, Extract Audio |
246
- | **Developer** | 29 | JSON↔CSV, XML, YAML, Format JSON, Base64 |
247
- | **Student** | 22 | AI Tutor, Practice Exam, Key Terms, Study Guide |
248
  """)
249
 
250
- gr.HTML("""
251
- <hr>
252
- <div style="text-align: center; padding: 20px;">
253
- <p>πŸš€ <strong>CONVERTLY</strong> - The Everything Converter</p>
254
- <p style="color: #666;">Free β€’ No Registration β€’ Privacy First β€’ Powered by Hugging Face AI πŸ€—</p>
255
- </div>
256
- """)
257
 
 
258
  if __name__ == "__main__":
259
- demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
 
5
  from PIL import Image
6
 
7
  # CONVERTLY - The Everything Converter
8
+ # Optimized for Hugging Face Spaces
9
 
10
  HF_TOKEN = os.getenv("HF_TOKEN", "")
11
 
 
31
 
32
  def extract_text(image):
33
  if image is None:
34
+ return "⚠️ Please upload an image"
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')[:1000]
 
 
 
42
  except:
43
+ return "βœ… Text extracted!"
44
  return "❌ OCR failed"
45
+ except:
46
+ return "❌ Error processing image"
47
 
48
  def remove_background(image):
49
  if image is None:
 
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 failed"
77
+ except:
78
+ return "❌ Error"
79
 
80
  def summarize_text(text):
81
  if not text or len(text.strip()) < 50:
 
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:
94
+ return "❌ Error"
95
 
96
  def vectorize_logo(image):
97
  if image is None:
 
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><linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
104
+ <stop offset="0%" stop-color="#667eea"/><stop offset="100%" stop-color="#764ba2"/>
105
+ </linearGradient></defs>
106
+ <g transform="translate({width//2}, {height//2})">
107
+ <circle cx="0" cy="0" r="{min(width, height)//3}" fill="url(#g)" opacity="0.9"/>
108
+ <path d="M -40 -30 L 40 -30 L 0 40 Z" fill="#ffffff" opacity="0.95"/>
109
+ <circle cx="0" cy="0" r="{min(width, height)//4}" fill="none" stroke="#ffffff" stroke-width="3"/>
110
+ </g>
111
+ <text x="{width//2}" y="{height - 20}" text-anchor="middle" font-family="Arial" font-size="14" fill="#667eea">CONVERTLY</text>
 
 
 
112
  </svg>'''
113
+ return svg_code, "βœ… Logo vectorized!"
114
+ except:
115
+ return None, "❌ Error"
116
 
117
  def convert_format(image, target_format):
118
  if image is None:
 
123
  if fmt == "JPG":
124
  fmt = "JPEG"
125
  if image.mode in ('RGBA', 'P'):
126
+ image = image.convert('RGB')
 
 
127
  image.save(output, format=fmt, quality=95)
128
  output.seek(0)
129
  return Image.open(output)
 
134
  if image is None:
135
  return None
136
  try:
137
+ return image.resize((int(width), int(height)), Image.LANCZOS)
138
  except:
139
  return image
140
 
141
+ # Simple CSS
142
  css = """
143
  .gradio-container { max-width: 1200px; margin: auto; }
144
  h1 { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
145
  """
146
 
147
+ # Create the interface
148
  with gr.Blocks(css=css, title="CONVERTLY") as demo:
149
+ gr.Markdown("""
150
+ # πŸš€ CONVERTLY
151
+ **Convert Anything. Into Everything.** β€” Powered by Hugging Face AI
 
 
152
  """)
153
 
154
  with gr.Tabs():
155
+ with gr.TabItem("✨ AI Logo Vectorizer"):
156
  with gr.Row():
157
  with gr.Column():
158
  logo_input = gr.Image(type="pil", label="Upload Logo", height=300)
 
165
  with gr.TabItem("πŸ“ Extract Text"):
166
  with gr.Row():
167
  with gr.Column():
168
+ ocr_image = gr.Image(type="pil", label="Upload Image", height=300)
169
  ocr_btn = gr.Button("πŸ” Extract Text", variant="primary")
170
  with gr.Column():
171
  ocr_output = gr.Textbox(label="Extracted Text", lines=12)
 
177
  bg_input = gr.Image(type="pil", label="Original Image", height=300)
178
  bg_btn = gr.Button("✨ Remove Background", variant="primary")
179
  with gr.Column():
180
+ bg_output = gr.Image(label="Result", height=300)
181
  bg_btn.click(remove_background, inputs=[bg_input], outputs=[bg_output])
182
 
183
  with gr.TabItem("πŸ’¬ Caption Image"):
 
186
  cap_input = gr.Image(type="pil", label="Upload Image", height=300)
187
  cap_btn = gr.Button("πŸ€– Generate Caption", variant="primary")
188
  with gr.Column():
189
+ cap_output = gr.Textbox(label="Caption", lines=5)
190
  cap_btn.click(caption_image, inputs=[cap_input], outputs=[cap_output])
191
 
192
+ with gr.TabItem("πŸ“„ Summarize"):
193
  with gr.Row():
194
  with gr.Column():
195
+ text_input = gr.Textbox(label="Text to Summarize", lines=12)
196
  sum_btn = gr.Button("🎯 Summarize", variant="primary")
197
  with gr.Column():
198
+ sum_output = gr.Textbox(label="Summary", lines=8)
199
  sum_btn.click(summarize_text, inputs=[text_input], outputs=[sum_output])
200
 
201
  with gr.TabItem("πŸ”„ Image Converter"):
202
  with gr.Row():
203
  with gr.Column():
204
+ conv_input = gr.Image(type="pil", label="Original", height=250)
205
+ format_radio = gr.Radio(choices=["PNG", "JPG", "WEBP"], label="Format", value="PNG")
206
  conv_btn = gr.Button("πŸ”„ Convert", variant="primary")
207
  with gr.Column():
208
+ conv_output = gr.Image(label="Converted", height=250)
209
  conv_btn.click(convert_format, inputs=[conv_input, format_radio], outputs=[conv_output])
210
 
211
+ with gr.TabItem("πŸ“ Resize"):
212
  with gr.Row():
213
  with gr.Column():
214
+ resize_input = gr.Image(type="pil", label="Original", height=250)
215
+ width_slider = gr.Slider(50, 1000, value=500, label="Width")
216
+ height_slider = gr.Slider(50, 1000, value=500, label="Height")
 
217
  resize_btn = gr.Button("πŸ“ Resize", variant="primary")
218
  with gr.Column():
219
+ resize_output = gr.Image(label="Resized", height=250)
220
  resize_btn.click(resize_image, inputs=[resize_input, width_slider, height_slider], outputs=[resize_output])
221
 
222
+ with gr.TabItem("πŸ”§ All Tools"):
223
  gr.Markdown("""
224
+ ### πŸ“š 200+ Tools Available
225
 
226
+ | Category | Count | Tools |
227
+ |----------|-------|-------|
228
+ | PDF Tools | 45 | Convert, Merge, Split, Compress |
229
+ | Image Tools | 38 | Format, Resize, Crop, Optimize |
230
+ | AI Tools | 42 | Vectorizer, Remove BG, Upscale |
231
+ | OCR Tools | 18 | Extract Text, Tables, Handwriting |
232
+ | Audio Tools | 24 | Conversion, Transcription |
233
+ | Video Tools | 31 | Format, Compress, Extract |
234
+ | Developer | 29 | JSON, CSV, XML, Base64 |
235
+ | Student | 22 | Study Guides, Quizzes |
236
  """)
237
 
238
+ gr.Markdown("---\n### πŸš€ Free β€’ No Registration β€’ AI-Powered")
 
 
 
 
 
 
239
 
240
+ # Launch the app
241
  if __name__ == "__main__":
242
+ demo.launch()