Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import requests
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# CONVERTLY - The Everything Converter
|
| 8 |
-
#
|
| 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
|
| 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 |
-
|
| 42 |
-
if text.strip():
|
| 43 |
-
return f"π Extracted Text:\n\n{text}"
|
| 44 |
-
return "π No text found"
|
| 45 |
except:
|
| 46 |
-
return "
|
| 47 |
return "β OCR failed"
|
| 48 |
-
except
|
| 49 |
-
return
|
| 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 |
-
|
| 77 |
-
return f"π·οΈ AI Caption: {caption}"
|
| 78 |
except:
|
| 79 |
-
return "
|
| 80 |
return "β Caption failed"
|
| 81 |
-
except
|
| 82 |
-
return
|
| 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 |
-
|
| 94 |
-
return f"π AI Summary:\n\n{summary}"
|
| 95 |
except:
|
| 96 |
return text[:300] + "..."
|
| 97 |
return "β Summarization failed"
|
| 98 |
-
except
|
| 99 |
-
return
|
| 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 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 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
|
| 122 |
-
except
|
| 123 |
-
return None,
|
| 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 |
-
|
| 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.
|
| 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.
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
Convert Anything. Into Everything. β Powered by Hugging Face AI
|
| 162 |
-
</p>
|
| 163 |
""")
|
| 164 |
|
| 165 |
with gr.Tabs():
|
| 166 |
-
with gr.TabItem("β¨ AI
|
| 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
|
| 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
|
| 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="
|
| 201 |
cap_btn.click(caption_image, inputs=[cap_input], outputs=[cap_output])
|
| 202 |
|
| 203 |
-
with gr.TabItem("π Summarize
|
| 204 |
with gr.Row():
|
| 205 |
with gr.Column():
|
| 206 |
-
text_input = gr.Textbox(label="Text to Summarize", lines=12
|
| 207 |
sum_btn = gr.Button("π― Summarize", variant="primary")
|
| 208 |
with gr.Column():
|
| 209 |
-
sum_output = gr.Textbox(label="
|
| 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
|
| 216 |
-
format_radio = gr.Radio(choices=["PNG", "JPG", "WEBP"], label="
|
| 217 |
conv_btn = gr.Button("π Convert", variant="primary")
|
| 218 |
with gr.Column():
|
| 219 |
-
conv_output = gr.Image(label="Converted
|
| 220 |
conv_btn.click(convert_format, inputs=[conv_input, format_radio], outputs=[conv_output])
|
| 221 |
|
| 222 |
-
with gr.TabItem("π Resize
|
| 223 |
with gr.Row():
|
| 224 |
with gr.Column():
|
| 225 |
-
resize_input = gr.Image(type="pil", label="Original
|
| 226 |
-
|
| 227 |
-
|
| 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
|
| 232 |
resize_btn.click(resize_image, inputs=[resize_input, width_slider, height_slider], outputs=[resize_output])
|
| 233 |
|
| 234 |
-
with gr.TabItem("π§ All
|
| 235 |
gr.Markdown("""
|
| 236 |
-
### π
|
| 237 |
|
| 238 |
-
| Category | Count |
|
| 239 |
-
|----------|-------|-------
|
| 240 |
-
|
|
| 241 |
-
|
|
| 242 |
-
|
|
| 243 |
-
|
|
| 244 |
-
|
|
| 245 |
-
|
|
| 246 |
-
|
|
| 247 |
-
|
|
| 248 |
""")
|
| 249 |
|
| 250 |
-
gr.
|
| 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(
|
|
|
|
| 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()
|