Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,145 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# CONVERTLY - The Everything Converter
|
| 4 |
-
#
|
| 5 |
|
| 6 |
-
|
| 7 |
-
return f"Hello {name}! Welcome to CONVERTLY! π"
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if image is None:
|
| 11 |
-
return "Please upload an image"
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
if image is None:
|
| 16 |
return None
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
if not text:
|
| 21 |
-
return "Please enter
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
def
|
| 25 |
if image is None:
|
| 26 |
-
return None, "Please upload a logo"
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# CSS
|
| 30 |
css = """
|
|
@@ -35,7 +150,7 @@ h1 { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2
|
|
| 35 |
with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
| 36 |
gr.Markdown("""
|
| 37 |
# π CONVERTLY
|
| 38 |
-
**Convert Anything. Into Everything.** β
|
| 39 |
""")
|
| 40 |
|
| 41 |
with gr.Tabs():
|
|
@@ -45,9 +160,9 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 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=
|
| 49 |
vector_msg = gr.Textbox(label="Status", lines=2)
|
| 50 |
-
vector_btn.click(
|
| 51 |
|
| 52 |
with gr.TabItem("π Extract Text"):
|
| 53 |
with gr.Row():
|
|
@@ -55,44 +170,74 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 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=
|
| 59 |
-
ocr_btn.click(
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
with gr.TabItem("π Summarize"):
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column():
|
| 73 |
-
text_input = gr.Textbox(label="Text", lines=
|
| 74 |
sum_btn = gr.Button("Summarize", variant="primary")
|
| 75 |
with gr.Column():
|
| 76 |
-
sum_output = gr.Textbox(label="Summary", lines=
|
| 77 |
-
sum_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
with gr.TabItem("π§ All Tools"):
|
| 80 |
gr.Markdown("""
|
| 81 |
-
### π 200+ Tools
|
| 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()
|
|
|
|
| 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 version for Hugging Face Spaces 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 = """
|
|
|
|
| 150 |
with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
| 151 |
gr.Markdown("""
|
| 152 |
# π CONVERTLY
|
| 153 |
+
**Convert Anything. Into Everything.** β Powered by Hugging Face AI
|
| 154 |
""")
|
| 155 |
|
| 156 |
with gr.Tabs():
|
|
|
|
| 160 |
logo_input = gr.Image(type="pil", label="Upload Logo", height=300)
|
| 161 |
vector_btn = gr.Button("Vectorize Now", variant="primary")
|
| 162 |
with gr.Column():
|
| 163 |
+
svg_output = gr.Code(label="SVG Code", lines=15)
|
| 164 |
vector_msg = gr.Textbox(label="Status", lines=2)
|
| 165 |
+
vector_btn.click(vectorize_logo, inputs=[logo_input], outputs=[svg_output, vector_msg])
|
| 166 |
|
| 167 |
with gr.TabItem("π Extract Text"):
|
| 168 |
with gr.Row():
|
|
|
|
| 170 |
ocr_image = gr.Image(type="pil", label="Upload Image", height=300)
|
| 171 |
ocr_btn = gr.Button("Extract Text", variant="primary")
|
| 172 |
with gr.Column():
|
| 173 |
+
ocr_output = gr.Textbox(label="Extracted Text", lines=12)
|
| 174 |
+
ocr_btn.click(extract_text, inputs=[ocr_image], outputs=[ocr_output])
|
| 175 |
|
| 176 |
with gr.TabItem("πΌοΈ Remove Background"):
|
| 177 |
with gr.Row():
|
| 178 |
with gr.Column():
|
| 179 |
+
bg_input = gr.Image(type="pil", label="Original Image", height=300)
|
| 180 |
bg_btn = gr.Button("Remove Background", variant="primary")
|
| 181 |
with gr.Column():
|
| 182 |
bg_output = gr.Image(label="Result", height=300)
|
| 183 |
+
bg_btn.click(remove_background, inputs=[bg_input], outputs=[bg_output])
|
| 184 |
+
|
| 185 |
+
with gr.TabItem("π¬ Caption Image"):
|
| 186 |
+
with gr.Row():
|
| 187 |
+
with gr.Column():
|
| 188 |
+
cap_input = gr.Image(type="pil", label="Upload Image", height=300)
|
| 189 |
+
cap_btn = gr.Button("Generate Caption", variant="primary")
|
| 190 |
+
with gr.Column():
|
| 191 |
+
cap_output = gr.Textbox(label="Caption", lines=5)
|
| 192 |
+
cap_btn.click(caption_image, inputs=[cap_input], outputs=[cap_output])
|
| 193 |
|
| 194 |
with gr.TabItem("π Summarize"):
|
| 195 |
with gr.Row():
|
| 196 |
with gr.Column():
|
| 197 |
+
text_input = gr.Textbox(label="Text to Summarize", lines=12)
|
| 198 |
sum_btn = gr.Button("Summarize", variant="primary")
|
| 199 |
with gr.Column():
|
| 200 |
+
sum_output = gr.Textbox(label="Summary", lines=8)
|
| 201 |
+
sum_btn.click(summarize_text, inputs=[text_input], outputs=[sum_output])
|
| 202 |
+
|
| 203 |
+
with gr.TabItem("π Image Converter"):
|
| 204 |
+
with gr.Row():
|
| 205 |
+
with gr.Column():
|
| 206 |
+
conv_input = gr.Image(type="pil", label="Original", height=250)
|
| 207 |
+
format_radio = gr.Radio(choices=["PNG", "JPG", "WEBP"], label="Format", value="PNG")
|
| 208 |
+
conv_btn = gr.Button("Convert", variant="primary")
|
| 209 |
+
with gr.Column():
|
| 210 |
+
conv_output = gr.Image(label="Converted", height=250)
|
| 211 |
+
conv_btn.click(convert_format, inputs=[conv_input, format_radio], outputs=[conv_output])
|
| 212 |
+
|
| 213 |
+
with gr.TabItem("π Resize"):
|
| 214 |
+
with gr.Row():
|
| 215 |
+
with gr.Column():
|
| 216 |
+
resize_input = gr.Image(type="pil", label="Original", height=250)
|
| 217 |
+
width_slider = gr.Slider(50, 1000, value=500, label="Width")
|
| 218 |
+
height_slider = gr.Slider(50, 1000, value=500, label="Height")
|
| 219 |
+
resize_btn = gr.Button("Resize", variant="primary")
|
| 220 |
+
with gr.Column():
|
| 221 |
+
resize_output = gr.Image(label="Resized", height=250)
|
| 222 |
+
resize_btn.click(resize_image, inputs=[resize_input, width_slider, height_slider], outputs=[resize_output])
|
| 223 |
|
| 224 |
with gr.TabItem("π§ All Tools"):
|
| 225 |
gr.Markdown("""
|
| 226 |
+
### π 200+ Tools Available
|
| 227 |
|
| 228 |
| Category | Count | Tools |
|
| 229 |
|----------|-------|-------|
|
| 230 |
| PDF Tools | 45 | Convert, Merge, Split, Compress |
|
| 231 |
| Image Tools | 38 | Format, Resize, Crop, Optimize |
|
| 232 |
| AI Tools | 42 | Vectorizer, Remove BG, Upscale |
|
| 233 |
+
| OCR Tools | 18 | Extract Text, Tables, Handwriting |
|
| 234 |
| Audio Tools | 24 | Conversion, Transcription |
|
| 235 |
+
| Video Tools | 31 | Format, Compress, Extract |
|
| 236 |
+
| Developer | 29 | JSON, CSV, XML, Base64 |
|
| 237 |
| Student | 22 | Study Guides, Quizzes |
|
| 238 |
""")
|
| 239 |
|
| 240 |
gr.Markdown("---\n### π Free β’ No Registration β’ AI-Powered")
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
| 243 |
+
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|