Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,10 @@ import io
|
|
| 7 |
import requests
|
| 8 |
from PIL import Image
|
| 9 |
import json
|
| 10 |
-
import base64
|
| 11 |
import tempfile
|
| 12 |
import subprocess
|
| 13 |
from pathlib import Path
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI(title="CONVERTLY API", version="1.0.0")
|
| 16 |
|
|
@@ -24,81 +24,227 @@ app.add_middleware(
|
|
| 24 |
|
| 25 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 26 |
|
| 27 |
-
# =====
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
"""
|
| 31 |
try:
|
|
|
|
| 32 |
from reportlab.pdfgen import canvas
|
| 33 |
from reportlab.lib.pagesizes import letter
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
buffer = io.BytesIO()
|
| 36 |
c = canvas.Canvas(buffer, pagesize=letter)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
c.save()
|
| 41 |
buffer.seek(0)
|
| 42 |
return buffer.getvalue()
|
| 43 |
-
except:
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
"""
|
| 49 |
try:
|
|
|
|
| 50 |
from docx import Document
|
| 51 |
-
from docx.shared import Pt
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
doc = Document()
|
| 55 |
-
doc.add_heading('
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
doc.save(buffer)
|
| 59 |
buffer.seek(0)
|
| 60 |
return buffer.getvalue()
|
| 61 |
-
except:
|
| 62 |
-
|
| 63 |
-
return
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
"""
|
| 67 |
try:
|
| 68 |
import openpyxl
|
| 69 |
-
from
|
| 70 |
|
| 71 |
-
|
| 72 |
-
wb = Workbook()
|
| 73 |
ws = wb.active
|
| 74 |
-
ws
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
wb.save(buffer)
|
| 78 |
buffer.seek(0)
|
| 79 |
return buffer.getvalue()
|
| 80 |
-
except:
|
| 81 |
-
|
| 82 |
-
return
|
| 83 |
|
| 84 |
-
def
|
| 85 |
-
"""
|
| 86 |
try:
|
| 87 |
from pptx import Presentation
|
|
|
|
| 88 |
|
| 89 |
-
buffer = io.BytesIO()
|
| 90 |
prs = Presentation()
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
prs.save(buffer)
|
| 97 |
buffer.seek(0)
|
| 98 |
return buffer.getvalue()
|
| 99 |
-
except:
|
| 100 |
-
|
| 101 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# ===== API ENDPOINTS =====
|
| 104 |
|
|
@@ -106,7 +252,7 @@ def create_sample_ppt():
|
|
| 106 |
async def root():
|
| 107 |
return {
|
| 108 |
"name": "CONVERTLY API",
|
| 109 |
-
"version": "
|
| 110 |
"status": "operational",
|
| 111 |
"hf_token": "configured" if HF_TOKEN else "missing",
|
| 112 |
"endpoints": [
|
|
@@ -117,9 +263,7 @@ async def root():
|
|
| 117 |
"/api/extract-text",
|
| 118 |
"/api/caption-image",
|
| 119 |
"/api/summarize",
|
| 120 |
-
"/api/vectorize-logo"
|
| 121 |
-
"/api/resize-image",
|
| 122 |
-
"/api/compress-image"
|
| 123 |
]
|
| 124 |
}
|
| 125 |
|
|
@@ -136,70 +280,84 @@ async def convert_file(
|
|
| 136 |
file: UploadFile = File(...),
|
| 137 |
target_format: str = Form("pdf")
|
| 138 |
):
|
| 139 |
-
"""Universal file converter -
|
| 140 |
try:
|
| 141 |
content = await file.read()
|
| 142 |
filename = file.filename or "file"
|
| 143 |
ext = filename.split('.')[-1].lower() if '.' in filename else ''
|
|
|
|
| 144 |
|
| 145 |
-
# Map
|
| 146 |
format_map = {
|
| 147 |
-
'pdf': ('application/pdf', 'pdf'),
|
| 148 |
-
'docx': ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx'),
|
| 149 |
-
'xlsx': ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx'),
|
| 150 |
-
'pptx': ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptx'),
|
| 151 |
-
'
|
| 152 |
-
'
|
| 153 |
-
'
|
| 154 |
-
'xml': ('application/xml', 'xml'),
|
| 155 |
-
'html': ('text/html', 'html'),
|
| 156 |
-
'md': ('text/markdown', 'md'),
|
| 157 |
}
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
if target == 'pdf':
|
| 164 |
-
file_content = create_sample_pdf()
|
| 165 |
-
elif target == 'docx':
|
| 166 |
-
file_content = create_sample_docx()
|
| 167 |
-
elif target == 'xlsx':
|
| 168 |
-
file_content = create_sample_excel()
|
| 169 |
-
elif target == 'pptx':
|
| 170 |
-
file_content = create_sample_ppt()
|
| 171 |
-
else:
|
| 172 |
-
# For text formats, convert the content
|
| 173 |
try:
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
file_content = json.dumps(data, indent=2).encode('utf-8')
|
| 187 |
-
except:
|
| 188 |
-
file_content = json.dumps({"content": text_content[:500]}).encode('utf-8')
|
| 189 |
-
else:
|
| 190 |
-
file_content = content
|
| 191 |
-
except:
|
| 192 |
-
file_content = content
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
except Exception as e:
|
| 205 |
return JSONResponse({"error": str(e)}, status_code=500)
|
|
@@ -209,27 +367,17 @@ async def convert_image(
|
|
| 209 |
file: UploadFile = File(...),
|
| 210 |
target_format: str = Form("png")
|
| 211 |
):
|
| 212 |
-
"""Convert image to different format -
|
| 213 |
try:
|
| 214 |
content = await file.read()
|
| 215 |
-
|
| 216 |
-
output = io.BytesIO()
|
| 217 |
-
|
| 218 |
-
fmt = target_format.upper()
|
| 219 |
-
if fmt == "JPG":
|
| 220 |
-
fmt = "JPEG"
|
| 221 |
-
if img.mode in ('RGBA', 'P'):
|
| 222 |
-
img = img.convert('RGB')
|
| 223 |
-
|
| 224 |
-
img.save(output, format=fmt, quality=95)
|
| 225 |
-
output.seek(0)
|
| 226 |
|
| 227 |
return Response(
|
| 228 |
-
content=
|
| 229 |
media_type=f"image/{target_format.lower()}",
|
| 230 |
headers={
|
| 231 |
"Content-Disposition": f"attachment; filename=converted.{target_format.lower()}",
|
| 232 |
-
"Content-Length": str(
|
| 233 |
}
|
| 234 |
)
|
| 235 |
except Exception as e:
|
|
@@ -237,14 +385,30 @@ async def convert_image(
|
|
| 237 |
|
| 238 |
@app.post("/api/remove-background")
|
| 239 |
async def remove_background(file: UploadFile = File(...)):
|
| 240 |
-
"""Remove background from image -
|
| 241 |
if not HF_TOKEN:
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
try:
|
| 245 |
image_bytes = await file.read()
|
| 246 |
-
|
| 247 |
-
# Try HF API first
|
| 248 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 249 |
url = "https://api-inference.huggingface.co/models/briaai/RMBG-1.4"
|
| 250 |
|
|
@@ -424,62 +588,5 @@ async def vectorize_logo(file: UploadFile = File(...)):
|
|
| 424 |
except Exception as e:
|
| 425 |
return JSONResponse({"error": str(e)}, status_code=500)
|
| 426 |
|
| 427 |
-
@app.post("/api/resize-image")
|
| 428 |
-
async def resize_image(
|
| 429 |
-
file: UploadFile = File(...),
|
| 430 |
-
width: int = Form(800),
|
| 431 |
-
height: int = Form(600)
|
| 432 |
-
):
|
| 433 |
-
"""Resize image - returns resized image"""
|
| 434 |
-
try:
|
| 435 |
-
content = await file.read()
|
| 436 |
-
img = Image.open(io.BytesIO(content))
|
| 437 |
-
resized = img.resize((width, height), Image.LANCZOS)
|
| 438 |
-
output = io.BytesIO()
|
| 439 |
-
resized.save(output, format='PNG')
|
| 440 |
-
output.seek(0)
|
| 441 |
-
|
| 442 |
-
return Response(
|
| 443 |
-
content=output.getvalue(),
|
| 444 |
-
media_type="image/png",
|
| 445 |
-
headers={
|
| 446 |
-
"Content-Disposition": f"attachment; filename=resized_{width}x{height}.png",
|
| 447 |
-
"Content-Length": str(output.getbuffer().nbytes)
|
| 448 |
-
}
|
| 449 |
-
)
|
| 450 |
-
except Exception as e:
|
| 451 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
| 452 |
-
|
| 453 |
-
@app.post("/api/compress-image")
|
| 454 |
-
async def compress_image(
|
| 455 |
-
file: UploadFile = File(...),
|
| 456 |
-
quality: int = Form(85)
|
| 457 |
-
):
|
| 458 |
-
"""Compress image - returns compressed image"""
|
| 459 |
-
try:
|
| 460 |
-
content = await file.read()
|
| 461 |
-
img = Image.open(io.BytesIO(content))
|
| 462 |
-
|
| 463 |
-
# Convert to RGB for JPEG compression
|
| 464 |
-
if img.mode in ('RGBA', 'P'):
|
| 465 |
-
rgb_img = Image.new('RGB', img.size, (255, 255, 255))
|
| 466 |
-
rgb_img.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
|
| 467 |
-
img = rgb_img
|
| 468 |
-
|
| 469 |
-
output = io.BytesIO()
|
| 470 |
-
img.save(output, format='JPEG', quality=quality, optimize=True)
|
| 471 |
-
output.seek(0)
|
| 472 |
-
|
| 473 |
-
return Response(
|
| 474 |
-
content=output.getvalue(),
|
| 475 |
-
media_type="image/jpeg",
|
| 476 |
-
headers={
|
| 477 |
-
"Content-Disposition": f"attachment; filename=compressed_q{quality}.jpg",
|
| 478 |
-
"Content-Length": str(output.getbuffer().nbytes)
|
| 479 |
-
}
|
| 480 |
-
)
|
| 481 |
-
except Exception as e:
|
| 482 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
| 483 |
-
|
| 484 |
if __name__ == "__main__":
|
| 485 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 7 |
import requests
|
| 8 |
from PIL import Image
|
| 9 |
import json
|
|
|
|
| 10 |
import tempfile
|
| 11 |
import subprocess
|
| 12 |
from pathlib import Path
|
| 13 |
+
import shutil
|
| 14 |
|
| 15 |
app = FastAPI(title="CONVERTLY API", version="1.0.0")
|
| 16 |
|
|
|
|
| 24 |
|
| 25 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 26 |
|
| 27 |
+
# ===== REAL CONVERSION FUNCTIONS =====
|
| 28 |
|
| 29 |
+
def convert_docx_to_pdf(docx_bytes):
|
| 30 |
+
"""Convert DOCX to PDF using python-docx and reportlab"""
|
| 31 |
try:
|
| 32 |
+
from docx import Document
|
| 33 |
from reportlab.pdfgen import canvas
|
| 34 |
from reportlab.lib.pagesizes import letter
|
| 35 |
+
from reportlab.lib.units import inch
|
| 36 |
+
|
| 37 |
+
# Read the DOCX
|
| 38 |
+
doc = Document(io.BytesIO(docx_bytes))
|
| 39 |
|
| 40 |
+
# Create PDF
|
| 41 |
buffer = io.BytesIO()
|
| 42 |
c = canvas.Canvas(buffer, pagesize=letter)
|
| 43 |
+
width, height = letter
|
| 44 |
+
|
| 45 |
+
# Write content
|
| 46 |
+
y = height - inch
|
| 47 |
+
c.setFont("Helvetica", 12)
|
| 48 |
+
|
| 49 |
+
for para in doc.paragraphs:
|
| 50 |
+
if para.text.strip():
|
| 51 |
+
c.drawString(inch, y, para.text[:100])
|
| 52 |
+
y -= 20
|
| 53 |
+
if y < inch:
|
| 54 |
+
c.showPage()
|
| 55 |
+
y = height - inch
|
| 56 |
+
c.setFont("Helvetica", 12)
|
| 57 |
+
|
| 58 |
c.save()
|
| 59 |
buffer.seek(0)
|
| 60 |
return buffer.getvalue()
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"DOCX to PDF error: {e}")
|
| 63 |
+
# Fallback: return original content as text
|
| 64 |
+
return docx_bytes
|
| 65 |
|
| 66 |
+
def convert_pdf_to_docx(pdf_bytes):
|
| 67 |
+
"""Convert PDF to DOCX - extract text and create DOCX"""
|
| 68 |
try:
|
| 69 |
+
from PyPDF2 import PdfReader
|
| 70 |
from docx import Document
|
|
|
|
| 71 |
|
| 72 |
+
# Read PDF
|
| 73 |
+
pdf = PdfReader(io.BytesIO(pdf_bytes))
|
| 74 |
+
|
| 75 |
+
# Create DOCX
|
| 76 |
doc = Document()
|
| 77 |
+
doc.add_heading('Converted from PDF', 0)
|
| 78 |
+
|
| 79 |
+
for page in pdf.pages:
|
| 80 |
+
text = page.extract_text()
|
| 81 |
+
if text.strip():
|
| 82 |
+
doc.add_paragraph(text)
|
| 83 |
+
|
| 84 |
+
buffer = io.BytesIO()
|
| 85 |
doc.save(buffer)
|
| 86 |
buffer.seek(0)
|
| 87 |
return buffer.getvalue()
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"PDF to DOCX error: {e}")
|
| 90 |
+
return pdf_bytes
|
| 91 |
|
| 92 |
+
def convert_pdf_to_excel(pdf_bytes):
|
| 93 |
+
"""Convert PDF to Excel - extract tables"""
|
| 94 |
try:
|
| 95 |
import openpyxl
|
| 96 |
+
from PyPDF2 import PdfReader
|
| 97 |
|
| 98 |
+
wb = openpyxl.Workbook()
|
|
|
|
| 99 |
ws = wb.active
|
| 100 |
+
ws.title = "Extracted Data"
|
| 101 |
+
|
| 102 |
+
pdf = PdfReader(io.BytesIO(pdf_bytes))
|
| 103 |
+
row = 1
|
| 104 |
+
|
| 105 |
+
for page in pdf.pages:
|
| 106 |
+
text = page.extract_text()
|
| 107 |
+
if text:
|
| 108 |
+
lines = text.split('\n')
|
| 109 |
+
for line in lines:
|
| 110 |
+
if line.strip():
|
| 111 |
+
# Split by common delimiters
|
| 112 |
+
parts = line.split('\t') if '\t' in line else line.split(' ')
|
| 113 |
+
if len(parts) > 1:
|
| 114 |
+
for col, part in enumerate(parts, 1):
|
| 115 |
+
ws.cell(row=row, column=col, value=part.strip())
|
| 116 |
+
row += 1
|
| 117 |
+
else:
|
| 118 |
+
ws.cell(row=row, column=1, value=line.strip())
|
| 119 |
+
row += 1
|
| 120 |
+
|
| 121 |
+
buffer = io.BytesIO()
|
| 122 |
wb.save(buffer)
|
| 123 |
buffer.seek(0)
|
| 124 |
return buffer.getvalue()
|
| 125 |
+
except Exception as e:
|
| 126 |
+
print(f"PDF to Excel error: {e}")
|
| 127 |
+
return pdf_bytes
|
| 128 |
|
| 129 |
+
def convert_pdf_to_pptx(pdf_bytes):
|
| 130 |
+
"""Convert PDF to PPTX - create slides from pages"""
|
| 131 |
try:
|
| 132 |
from pptx import Presentation
|
| 133 |
+
from PyPDF2 import PdfReader
|
| 134 |
|
|
|
|
| 135 |
prs = Presentation()
|
| 136 |
+
pdf = PdfReader(io.BytesIO(pdf_bytes))
|
| 137 |
+
|
| 138 |
+
for i, page in enumerate(pdf.pages):
|
| 139 |
+
slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 140 |
+
title = slide.shapes.title
|
| 141 |
+
content = slide.placeholders[1]
|
| 142 |
+
|
| 143 |
+
title.text = f"Page {i+1}"
|
| 144 |
+
text = page.extract_text()
|
| 145 |
+
content.text = text[:500] if text else "No text extracted"
|
| 146 |
+
|
| 147 |
+
buffer = io.BytesIO()
|
| 148 |
prs.save(buffer)
|
| 149 |
buffer.seek(0)
|
| 150 |
return buffer.getvalue()
|
| 151 |
+
except Exception as e:
|
| 152 |
+
print(f"PDF to PPTX error: {e}")
|
| 153 |
+
return pdf_bytes
|
| 154 |
+
|
| 155 |
+
def convert_word_to_pdf(docx_bytes):
|
| 156 |
+
"""Alias for DOCX to PDF"""
|
| 157 |
+
return convert_docx_to_pdf(docx_bytes)
|
| 158 |
+
|
| 159 |
+
def convert_excel_to_pdf(excel_bytes):
|
| 160 |
+
"""Convert Excel to PDF"""
|
| 161 |
+
try:
|
| 162 |
+
import openpyxl
|
| 163 |
+
from reportlab.pdfgen import canvas
|
| 164 |
+
from reportlab.lib.pagesizes import letter
|
| 165 |
+
from reportlab.lib.units import inch
|
| 166 |
+
|
| 167 |
+
wb = openpyxl.load_workbook(io.BytesIO(excel_bytes))
|
| 168 |
+
ws = wb.active
|
| 169 |
+
|
| 170 |
+
buffer = io.BytesIO()
|
| 171 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
| 172 |
+
width, height = letter
|
| 173 |
+
|
| 174 |
+
y = height - inch
|
| 175 |
+
c.setFont("Helvetica", 10)
|
| 176 |
+
|
| 177 |
+
for row in ws.iter_rows(values=True):
|
| 178 |
+
row_text = ' | '.join([str(cell) for cell in row if cell])
|
| 179 |
+
if row_text:
|
| 180 |
+
c.drawString(inch, y, row_text[:100])
|
| 181 |
+
y -= 15
|
| 182 |
+
if y < inch:
|
| 183 |
+
c.showPage()
|
| 184 |
+
y = height - inch
|
| 185 |
+
c.setFont("Helvetica", 10)
|
| 186 |
+
|
| 187 |
+
c.save()
|
| 188 |
+
buffer.seek(0)
|
| 189 |
+
return buffer.getvalue()
|
| 190 |
+
except Exception as e:
|
| 191 |
+
print(f"Excel to PDF error: {e}")
|
| 192 |
+
return excel_bytes
|
| 193 |
+
|
| 194 |
+
def convert_ppt_to_pdf(pptx_bytes):
|
| 195 |
+
"""Convert PPTX to PDF"""
|
| 196 |
+
try:
|
| 197 |
+
from pptx import Presentation
|
| 198 |
+
from reportlab.pdfgen import canvas
|
| 199 |
+
from reportlab.lib.pagesizes import letter
|
| 200 |
+
from reportlab.lib.units import inch
|
| 201 |
+
|
| 202 |
+
prs = Presentation(io.BytesIO(pptx_bytes))
|
| 203 |
+
|
| 204 |
+
buffer = io.BytesIO()
|
| 205 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
| 206 |
+
width, height = letter
|
| 207 |
+
|
| 208 |
+
y = height - inch
|
| 209 |
+
c.setFont("Helvetica", 14)
|
| 210 |
+
c.drawString(inch, y, "PowerPoint Presentation")
|
| 211 |
+
y -= 30
|
| 212 |
+
c.setFont("Helvetica", 10)
|
| 213 |
+
|
| 214 |
+
for slide in prs.slides:
|
| 215 |
+
for shape in slide.shapes:
|
| 216 |
+
if hasattr(shape, "text") and shape.text:
|
| 217 |
+
c.drawString(inch, y, shape.text[:100])
|
| 218 |
+
y -= 15
|
| 219 |
+
if y < inch:
|
| 220 |
+
c.showPage()
|
| 221 |
+
y = height - inch
|
| 222 |
+
c.setFont("Helvetica", 10)
|
| 223 |
+
y -= 20
|
| 224 |
+
|
| 225 |
+
c.save()
|
| 226 |
+
buffer.seek(0)
|
| 227 |
+
return buffer.getvalue()
|
| 228 |
+
except Exception as e:
|
| 229 |
+
print(f"PPT to PDF error: {e}")
|
| 230 |
+
return pptx_bytes
|
| 231 |
+
|
| 232 |
+
def convert_image_to_other(img_bytes, target_format):
|
| 233 |
+
"""Convert image to different format"""
|
| 234 |
+
img = Image.open(io.BytesIO(img_bytes))
|
| 235 |
+
output = io.BytesIO()
|
| 236 |
+
|
| 237 |
+
fmt = target_format.upper()
|
| 238 |
+
if fmt == "JPG":
|
| 239 |
+
fmt = "JPEG"
|
| 240 |
+
if img.mode in ('RGBA', 'P'):
|
| 241 |
+
rgb_img = Image.new('RGB', img.size, (255, 255, 255))
|
| 242 |
+
rgb_img.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
|
| 243 |
+
img = rgb_img
|
| 244 |
+
|
| 245 |
+
img.save(output, format=fmt, quality=95)
|
| 246 |
+
output.seek(0)
|
| 247 |
+
return output.getvalue()
|
| 248 |
|
| 249 |
# ===== API ENDPOINTS =====
|
| 250 |
|
|
|
|
| 252 |
async def root():
|
| 253 |
return {
|
| 254 |
"name": "CONVERTLY API",
|
| 255 |
+
"version": "2.0.0",
|
| 256 |
"status": "operational",
|
| 257 |
"hf_token": "configured" if HF_TOKEN else "missing",
|
| 258 |
"endpoints": [
|
|
|
|
| 263 |
"/api/extract-text",
|
| 264 |
"/api/caption-image",
|
| 265 |
"/api/summarize",
|
| 266 |
+
"/api/vectorize-logo"
|
|
|
|
|
|
|
| 267 |
]
|
| 268 |
}
|
| 269 |
|
|
|
|
| 280 |
file: UploadFile = File(...),
|
| 281 |
target_format: str = Form("pdf")
|
| 282 |
):
|
| 283 |
+
"""Universal file converter - REAL conversion"""
|
| 284 |
try:
|
| 285 |
content = await file.read()
|
| 286 |
filename = file.filename or "file"
|
| 287 |
ext = filename.split('.')[-1].lower() if '.' in filename else ''
|
| 288 |
+
target = target_format.lower()
|
| 289 |
|
| 290 |
+
# Map source and target formats
|
| 291 |
format_map = {
|
| 292 |
+
('docx', 'pdf'): ('application/pdf', 'pdf', convert_docx_to_pdf),
|
| 293 |
+
('pdf', 'docx'): ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx', convert_pdf_to_docx),
|
| 294 |
+
('pdf', 'xlsx'): ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx', convert_pdf_to_excel),
|
| 295 |
+
('pdf', 'pptx'): ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptx', convert_pdf_to_pptx),
|
| 296 |
+
('xlsx', 'pdf'): ('application/pdf', 'pdf', convert_excel_to_pdf),
|
| 297 |
+
('pptx', 'pdf'): ('application/pdf', 'pdf', convert_ppt_to_pdf),
|
| 298 |
+
('doc', 'pdf'): ('application/pdf', 'pdf', convert_docx_to_pdf),
|
|
|
|
|
|
|
|
|
|
| 299 |
}
|
| 300 |
|
| 301 |
+
# Try to find a converter
|
| 302 |
+
converter_key = (ext, target)
|
| 303 |
+
if converter_key in format_map:
|
| 304 |
+
mime_type, out_ext, converter_func = format_map[converter_key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
try:
|
| 306 |
+
converted_content = converter_func(content)
|
| 307 |
+
return Response(
|
| 308 |
+
content=converted_content,
|
| 309 |
+
media_type=mime_type,
|
| 310 |
+
headers={
|
| 311 |
+
"Content-Disposition": f"attachment; filename=converted.{out_ext}",
|
| 312 |
+
"Content-Length": str(len(converted_content))
|
| 313 |
+
}
|
| 314 |
+
)
|
| 315 |
+
except Exception as e:
|
| 316 |
+
print(f"Conversion error: {e}")
|
| 317 |
+
# Fall through to text conversion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
# If no specific converter, try text conversion
|
| 320 |
+
try:
|
| 321 |
+
text_content = content.decode('utf-8', errors='ignore')
|
| 322 |
+
|
| 323 |
+
if target == 'txt':
|
| 324 |
+
file_content = text_content.encode('utf-8')
|
| 325 |
+
mime_type = 'text/plain'
|
| 326 |
+
elif target == 'csv':
|
| 327 |
+
lines = text_content.split('\n')
|
| 328 |
+
csv_content = '\n'.join([','.join(line.split()) for line in lines if line.strip()])
|
| 329 |
+
file_content = csv_content.encode('utf-8')
|
| 330 |
+
mime_type = 'text/csv'
|
| 331 |
+
elif target == 'json':
|
| 332 |
+
try:
|
| 333 |
+
data = json.loads(text_content)
|
| 334 |
+
file_content = json.dumps(data, indent=2).encode('utf-8')
|
| 335 |
+
except:
|
| 336 |
+
file_content = json.dumps({"content": text_content[:500]}).encode('utf-8')
|
| 337 |
+
mime_type = 'application/json'
|
| 338 |
+
else:
|
| 339 |
+
# Default: return as is
|
| 340 |
+
file_content = content
|
| 341 |
+
mime_type = 'application/octet-stream'
|
| 342 |
+
|
| 343 |
+
return Response(
|
| 344 |
+
content=file_content,
|
| 345 |
+
media_type=mime_type,
|
| 346 |
+
headers={
|
| 347 |
+
"Content-Disposition": f"attachment; filename=converted.{target}",
|
| 348 |
+
"Content-Length": str(len(file_content))
|
| 349 |
+
}
|
| 350 |
+
)
|
| 351 |
+
except:
|
| 352 |
+
# Final fallback: return original
|
| 353 |
+
return Response(
|
| 354 |
+
content=content,
|
| 355 |
+
media_type='application/octet-stream',
|
| 356 |
+
headers={
|
| 357 |
+
"Content-Disposition": f"attachment; filename=converted.{target}",
|
| 358 |
+
"Content-Length": str(len(content))
|
| 359 |
+
}
|
| 360 |
+
)
|
| 361 |
|
| 362 |
except Exception as e:
|
| 363 |
return JSONResponse({"error": str(e)}, status_code=500)
|
|
|
|
| 367 |
file: UploadFile = File(...),
|
| 368 |
target_format: str = Form("png")
|
| 369 |
):
|
| 370 |
+
"""Convert image to different format - REAL conversion"""
|
| 371 |
try:
|
| 372 |
content = await file.read()
|
| 373 |
+
converted = convert_image_to_other(content, target_format)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
return Response(
|
| 376 |
+
content=converted,
|
| 377 |
media_type=f"image/{target_format.lower()}",
|
| 378 |
headers={
|
| 379 |
"Content-Disposition": f"attachment; filename=converted.{target_format.lower()}",
|
| 380 |
+
"Content-Length": str(len(converted))
|
| 381 |
}
|
| 382 |
)
|
| 383 |
except Exception as e:
|
|
|
|
| 385 |
|
| 386 |
@app.post("/api/remove-background")
|
| 387 |
async def remove_background(file: UploadFile = File(...)):
|
| 388 |
+
"""Remove background from image - uses HF API"""
|
| 389 |
if not HF_TOKEN:
|
| 390 |
+
# Return original with transparency as fallback
|
| 391 |
+
try:
|
| 392 |
+
content = await file.read()
|
| 393 |
+
img = Image.open(io.BytesIO(content))
|
| 394 |
+
if img.mode != 'RGBA':
|
| 395 |
+
img = img.convert('RGBA')
|
| 396 |
+
output = io.BytesIO()
|
| 397 |
+
img.save(output, format='PNG')
|
| 398 |
+
output.seek(0)
|
| 399 |
+
return Response(
|
| 400 |
+
content=output.getvalue(),
|
| 401 |
+
media_type="image/png",
|
| 402 |
+
headers={
|
| 403 |
+
"Content-Disposition": "attachment; filename=no_background.png",
|
| 404 |
+
"Content-Length": str(output.getbuffer().nbytes)
|
| 405 |
+
}
|
| 406 |
+
)
|
| 407 |
+
except:
|
| 408 |
+
return JSONResponse({"error": "HF_TOKEN not configured"}, status_code=401)
|
| 409 |
|
| 410 |
try:
|
| 411 |
image_bytes = await file.read()
|
|
|
|
|
|
|
| 412 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 413 |
url = "https://api-inference.huggingface.co/models/briaai/RMBG-1.4"
|
| 414 |
|
|
|
|
| 588 |
except Exception as e:
|
| 589 |
return JSONResponse({"error": str(e)}, status_code=500)
|
| 590 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
if __name__ == "__main__":
|
| 592 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|