Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,53 @@
|
|
| 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 |
-
#
|
| 9 |
|
| 10 |
-
|
| 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
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
if image is None:
|
| 50 |
return None
|
| 51 |
try:
|
| 52 |
-
|
| 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
|
| 65 |
if image is None:
|
| 66 |
-
return "
|
| 67 |
-
|
| 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
|
| 81 |
-
if not text
|
|
|
|
|
|
|
| 82 |
return "Please enter at least 50 characters"
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
if image is None:
|
| 98 |
return None, "Please upload a logo image"
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
<rect width="{width}" height="{height}" fill="#ffffff"/>
|
| 103 |
<defs>
|
| 104 |
<linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
|
|
@@ -113,9 +62,7 @@ def vectorize_logo(image):
|
|
| 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 |
-
|
| 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:
|
|
@@ -162,7 +109,7 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 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(
|
| 166 |
|
| 167 |
with gr.TabItem("π Extract Text"):
|
| 168 |
with gr.Row():
|
|
@@ -171,7 +118,7 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 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(
|
| 175 |
|
| 176 |
with gr.TabItem("πΌοΈ Remove Background"):
|
| 177 |
with gr.Row():
|
|
@@ -180,7 +127,7 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 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(
|
| 184 |
|
| 185 |
with gr.TabItem("π¬ Caption Image"):
|
| 186 |
with gr.Row():
|
|
@@ -189,7 +136,7 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 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(
|
| 193 |
|
| 194 |
with gr.TabItem("π Summarize"):
|
| 195 |
with gr.Row():
|
|
@@ -198,7 +145,7 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 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(
|
| 202 |
|
| 203 |
with gr.TabItem("π Image Converter"):
|
| 204 |
with gr.Row():
|
|
@@ -240,4 +187,4 @@ with gr.Blocks(css=css, title="CONVERTLY") as demo:
|
|
| 240 |
gr.Markdown("---\n### π Free β’ No Registration β’ AI-Powered")
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
| 243 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import io
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
# CONVERTLY - The Everything Converter
|
| 6 |
+
# Simple working version for Hugging Face Spaces
|
| 7 |
|
| 8 |
+
def extract_text_demo(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if image is None:
|
| 10 |
+
return "Please upload an image"
|
| 11 |
+
return """π Extracted Text (Demo):
|
| 12 |
+
|
| 13 |
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
| 14 |
+
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
This is a demo of text extraction. In production, this would use
|
| 17 |
+
Hugging Face's OCR models like TrOCR or LayoutLM."""
|
| 18 |
+
|
| 19 |
+
def remove_bg_demo(image):
|
| 20 |
if image is None:
|
| 21 |
return None
|
| 22 |
try:
|
| 23 |
+
# For demo, just return the image with a note
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return image
|
| 25 |
except:
|
| 26 |
return image
|
| 27 |
|
| 28 |
+
def caption_demo(image):
|
| 29 |
if image is None:
|
| 30 |
+
return "Please upload an image"
|
| 31 |
+
return "π·οΈ AI Caption (Demo): A beautiful scene with stunning visuals"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
def summarize_demo(text):
|
| 34 |
+
if not text:
|
| 35 |
+
return "Please enter some text"
|
| 36 |
+
if len(text) < 50:
|
| 37 |
return "Please enter at least 50 characters"
|
| 38 |
+
return f"""π AI Summary (Demo):
|
| 39 |
+
|
| 40 |
+
{text[:200]}...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
This is a demo summary. In production, this would use Hugging Face's
|
| 43 |
+
BART or T5 models for text summarization."""
|
| 44 |
+
|
| 45 |
+
def vectorize_demo(image):
|
| 46 |
if image is None:
|
| 47 |
return None, "Please upload a logo image"
|
| 48 |
+
|
| 49 |
+
width, height = image.size
|
| 50 |
+
svg_code = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}">
|
| 51 |
<rect width="{width}" height="{height}" fill="#ffffff"/>
|
| 52 |
<defs>
|
| 53 |
<linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
|
|
|
|
| 62 |
</g>
|
| 63 |
<text x="{width//2}" y="{height - 20}" text-anchor="middle" font-family="Arial" font-size="14" fill="#667eea">CONVERTLY</text>
|
| 64 |
</svg>'''
|
| 65 |
+
return svg_code, "β
Logo vectorized successfully! (Demo)"
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def convert_format(image, target_format):
|
| 68 |
if image is None:
|
|
|
|
| 109 |
with gr.Column():
|
| 110 |
svg_output = gr.Code(label="SVG Code", lines=15)
|
| 111 |
vector_msg = gr.Textbox(label="Status", lines=2)
|
| 112 |
+
vector_btn.click(vectorize_demo, inputs=[logo_input], outputs=[svg_output, vector_msg])
|
| 113 |
|
| 114 |
with gr.TabItem("π Extract Text"):
|
| 115 |
with gr.Row():
|
|
|
|
| 118 |
ocr_btn = gr.Button("Extract Text", variant="primary")
|
| 119 |
with gr.Column():
|
| 120 |
ocr_output = gr.Textbox(label="Extracted Text", lines=12)
|
| 121 |
+
ocr_btn.click(extract_text_demo, inputs=[ocr_image], outputs=[ocr_output])
|
| 122 |
|
| 123 |
with gr.TabItem("πΌοΈ Remove Background"):
|
| 124 |
with gr.Row():
|
|
|
|
| 127 |
bg_btn = gr.Button("Remove Background", variant="primary")
|
| 128 |
with gr.Column():
|
| 129 |
bg_output = gr.Image(label="Result", height=300)
|
| 130 |
+
bg_btn.click(remove_bg_demo, inputs=[bg_input], outputs=[bg_output])
|
| 131 |
|
| 132 |
with gr.TabItem("π¬ Caption Image"):
|
| 133 |
with gr.Row():
|
|
|
|
| 136 |
cap_btn = gr.Button("Generate Caption", variant="primary")
|
| 137 |
with gr.Column():
|
| 138 |
cap_output = gr.Textbox(label="Caption", lines=5)
|
| 139 |
+
cap_btn.click(caption_demo, inputs=[cap_input], outputs=[cap_output])
|
| 140 |
|
| 141 |
with gr.TabItem("π Summarize"):
|
| 142 |
with gr.Row():
|
|
|
|
| 145 |
sum_btn = gr.Button("Summarize", variant="primary")
|
| 146 |
with gr.Column():
|
| 147 |
sum_output = gr.Textbox(label="Summary", lines=8)
|
| 148 |
+
sum_btn.click(summarize_demo, inputs=[text_input], outputs=[sum_output])
|
| 149 |
|
| 150 |
with gr.TabItem("π Image Converter"):
|
| 151 |
with gr.Row():
|
|
|
|
| 187 |
gr.Markdown("---\n### π Free β’ No Registration β’ AI-Powered")
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
+
demo.launch()
|