File size: 24,847 Bytes
32975ef
ffbf397
a68cf6c
 
 
8cd6cc6
ffbf397
8cd6cc6
a68cf6c
e6e5032
7e937d5
 
f4ce2c0
 
 
712f616
e6e5032
712f616
a68cf6c
 
 
 
 
 
 
7284690
a68cf6c
 
f4ce2c0
ffbf397
1f3c2d3
f4ce2c0
e6e5032
7e937d5
 
 
1f3c2d3
 
7e937d5
e6e5032
 
7e937d5
 
 
 
 
 
 
 
 
 
 
 
1f3c2d3
 
 
 
 
 
 
 
 
7e937d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de1760e
7e937d5
 
de1760e
7e937d5
e6e5032
 
 
 
f4ce2c0
7e937d5
e6e5032
f4ce2c0
e6e5032
f4ce2c0
 
e6e5032
 
 
 
 
 
 
1f3c2d3
e6e5032
 
 
 
 
 
 
 
 
7e937d5
e6e5032
f4ce2c0
 
ea3daff
 
de1760e
ffbf397
ea3daff
7e937d5
ffbf397
ea3daff
ffbf397
 
ea3daff
ffbf397
ea3daff
f4ce2c0
ea3daff
e6e5032
 
 
 
 
 
7e937d5
f4ce2c0
ea3daff
 
ffbf397
 
 
ea3daff
 
 
ffbf397
f4ce2c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e937d5
ea3daff
7e937d5
ffbf397
 
1f3c2d3
ea3daff
ffbf397
ea3daff
ffbf397
ea3daff
 
7e937d5
 
 
ea3daff
 
 
7e937d5
 
 
 
 
 
 
 
 
ea3daff
 
 
 
 
7e937d5
 
e6e5032
 
7e937d5
 
 
 
 
 
 
 
 
 
 
ea3daff
ffbf397
 
 
e6e5032
ea3daff
 
 
ffbf397
ea3daff
 
 
 
7e937d5
 
 
 
 
 
ea3daff
 
 
 
 
7e937d5
 
 
 
 
 
 
 
ea3daff
7e937d5
ea3daff
7e937d5
e6e5032
7e937d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea3daff
 
e6e5032
ea3daff
 
 
 
 
 
7e937d5
 
 
 
 
 
 
 
 
 
 
 
f4ce2c0
 
 
 
 
 
 
 
 
 
 
 
 
 
7e937d5
 
 
 
 
 
 
 
 
 
 
ea3daff
7e937d5
ea3daff
7e937d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffbf397
 
a68cf6c
 
ffbf397
 
ea3daff
ffbf397
e6e5032
ffbf397
a68cf6c
 
 
ffbf397
 
de1760e
 
ffbf397
a68cf6c
ffbf397
 
 
 
 
f4ce2c0
a68cf6c
352ab96
ffbf397
 
ea3daff
ffbf397
f4ce2c0
ffbf397
f4ce2c0
ea3daff
f4ce2c0
ea3daff
 
f4ce2c0
 
ea3daff
f4ce2c0
ea3daff
f4ce2c0
 
 
ffbf397
 
ea3daff
f4ce2c0
 
ea3daff
 
ffbf397
ea3daff
 
 
 
 
 
 
 
 
 
 
ffbf397
f4ce2c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e937d5
 
 
 
 
 
 
 
352ab96
a68cf6c
 
 
ffbf397
 
 
 
 
e6e5032
a68cf6c
352ab96
ea3daff
352ab96
ffbf397
ea3daff
ffbf397
 
 
ea3daff
ffbf397
 
 
 
 
 
 
 
 
e6e5032
ffbf397
 
 
 
7e937d5
ffbf397
7e937d5
 
e6e5032
ffbf397
e6e5032
a68cf6c
 
 
 
 
ffbf397
 
 
 
a68cf6c
ffbf397
 
7e937d5
ffbf397
7e937d5
 
e6e5032
ffbf397
e6e5032
a68cf6c
 
 
 
 
ffbf397
 
 
 
a68cf6c
 
 
352ab96
ffbf397
e6e5032
ffbf397
 
 
 
 
7e937d5
ffbf397
7e937d5
 
e6e5032
ffbf397
e6e5032
a68cf6c
 
 
 
 
ffbf397
a68cf6c
ffbf397
 
a68cf6c
 
ffbf397
 
 
 
7e937d5
 
 
ffbf397
 
 
a68cf6c
8cd6cc6
 
 
352ab96
 
8cd6cc6
 
 
 
352ab96
8cd6cc6
ffbf397
 
8cd6cc6
a68cf6c
e6e5032
a68cf6c
 
8cd6cc6
712f616
a68cf6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.responses import JSONResponse, FileResponse, Response
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os
import io
import requests
from PIL import Image
import json
import textwrap
import time
from datetime import datetime
import tempfile
import subprocess
import shutil

app = FastAPI(title="CONVERTLY API", version="2.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

HF_TOKEN = os.getenv("HF_TOKEN", "")

# ===== PDF GENERATION =====

def create_clean_pdf(text_content):
    """Create a clean PDF with ONLY the content"""
    try:
        from reportlab.lib.pagesizes import A4
        from reportlab.lib.units import inch, cm
        from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
        from reportlab.lib.enums import TA_LEFT, TA_JUSTIFY
        from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
        from reportlab.lib import colors
        
        buffer = io.BytesIO()
        
        doc = SimpleDocTemplate(
            buffer,
            pagesize=A4,
            rightMargin=2.54 * cm,
            leftMargin=2.54 * cm,
            topMargin=2.54 * cm,
            bottomMargin=2.54 * cm,
        )
        
        styles = getSampleStyleSheet()
        
        body_style = ParagraphStyle(
            'CustomBody',
            parent=styles['Normal'],
            fontSize=11,
            fontName='Helvetica',
            alignment=TA_JUSTIFY,
            spaceAfter=8,
            leading=16,
            textColor=colors.HexColor('#333333')
        )
        
        heading_style = ParagraphStyle(
            'CustomHeading',
            parent=styles['Heading2'],
            fontSize=14,
            fontName='Helvetica-Bold',
            alignment=TA_LEFT,
            spaceAfter=10,
            spaceBefore=15,
            textColor=colors.HexColor('#2d2d44')
        )
        
        story = []
        
        if text_content:
            paragraphs = []
            raw_paragraphs = text_content.split('\n\n')
            
            for para in raw_paragraphs:
                cleaned = para.strip()
                if cleaned:
                    cleaned = ' '.join(cleaned.split())
                    paragraphs.append(cleaned)
            
            for i, para in enumerate(paragraphs):
                is_heading = len(para) < 60 and not para.endswith('.') and not para.endswith('?') and not para.endswith('!')
                is_heading = is_heading or (len(para.split()) < 8 and not para.endswith('.'))
                
                if is_heading and i < len(paragraphs) - 1:
                    story.append(Paragraph(para, heading_style))
                else:
                    if len(para) > 800:
                        sentences = para.split('. ')
                        for j, sentence in enumerate(sentences):
                            if sentence.strip():
                                if j < len(sentences) - 1:
                                    story.append(Paragraph(sentence + '.', body_style))
                                else:
                                    story.append(Paragraph(sentence, body_style))
                    else:
                        story.append(Paragraph(para, body_style))
                
                if i < len(paragraphs) - 1:
                    story.append(Spacer(1, 0.05 * inch))
        
        doc.build(story)
        buffer.seek(0)
        return buffer.getvalue()
        
    except Exception as e:
        print(f"PDF error: {e}")
        return text_content.encode('utf-8')

# ===== CONVERSION FUNCTIONS =====

def convert_docx_to_pdf(docx_bytes):
    """Convert DOCX to PDF"""
    try:
        from docx import Document
        doc = Document(io.BytesIO(docx_bytes))
        
        full_text = []
        for para in doc.paragraphs:
            if para.text.strip():
                full_text.append(para.text.strip())
        
        # Extract tables
        for table in doc.tables:
            for row in table.rows:
                row_text = []
                for cell in row.cells:
                    if cell.text.strip():
                        row_text.append(cell.text.strip())
                if row_text:
                    full_text.append(' | '.join(row_text))
        
        text_content = '\n\n'.join(full_text) if full_text else "No text content found."
        return create_clean_pdf(text_content)
    except Exception as e:
        print(f"DOCX to PDF error: {e}")
        return docx_bytes

def convert_pdf_to_docx(pdf_bytes):
    """Convert PDF to DOCX"""
    try:
        from PyPDF2 import PdfReader
        from docx import Document
        
        pdf = PdfReader(io.BytesIO(pdf_bytes))
        doc = Document()
        
        for page in pdf.pages:
            text = page.extract_text()
            if text and text.strip():
                paragraphs = text.split('\n\n')
                for para in paragraphs:
                    if para.strip():
                        cleaned = ' '.join(para.split())
                        doc.add_paragraph(cleaned)
            else:
                doc.add_paragraph("[No text extracted]")
        
        buffer = io.BytesIO()
        doc.save(buffer)
        buffer.seek(0)
        return buffer.getvalue()
    except Exception as e:
        print(f"PDF to DOCX error: {e}")
        return pdf_bytes

def convert_ppt_to_pdf(pptx_bytes):
    """Convert PPTX to PDF - Extract text from slides"""
    try:
        from pptx import Presentation
        from reportlab.lib.pagesizes import A4
        from reportlab.lib.units import inch, cm
        from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
        from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
        from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_JUSTIFY
        from reportlab.lib import colors
        
        prs = Presentation(io.BytesIO(pptx_bytes))
        
        buffer = io.BytesIO()
        doc = SimpleDocTemplate(
            buffer,
            pagesize=A4,
            rightMargin=2.54 * cm,
            leftMargin=2.54 * cm,
            topMargin=2.54 * cm,
            bottomMargin=2.54 * cm,
        )
        
        styles = getSampleStyleSheet()
        
        title_style = ParagraphStyle(
            'SlideTitle',
            parent=styles['Heading2'],
            fontSize=16,
            fontName='Helvetica-Bold',
            alignment=TA_CENTER,
            spaceAfter=15,
            textColor=colors.HexColor('#1a1a2e')
        )
        
        body_style = ParagraphStyle(
            'SlideBody',
            parent=styles['Normal'],
            fontSize=11,
            fontName='Helvetica',
            alignment=TA_LEFT,
            spaceAfter=6,
            leading=16,
            textColor=colors.HexColor('#333333')
        )
        
        story = []
        
        for slide_num, slide in enumerate(prs.slides, 1):
            # Slide title
            slide_texts = []
            for shape in slide.shapes:
                if hasattr(shape, "text") and shape.text:
                    text = shape.text.strip()
                    if text:
                        slide_texts.append(text)
            
            if slide_texts:
                # First text is usually the title
                story.append(Paragraph(f"Slide {slide_num}: {slide_texts[0]}", title_style))
                story.append(Spacer(1, 0.1 * inch))
                
                # Rest are content
                for text in slide_texts[1:]:
                    if text:
                        story.append(Paragraph(text, body_style))
                        story.append(Spacer(1, 0.05 * inch))
            else:
                story.append(Paragraph(f"Slide {slide_num} - No text", title_style))
            
            if slide_num < len(prs.slides):
                story.append(PageBreak())
        
        doc.build(story)
        buffer.seek(0)
        return buffer.getvalue()
        
    except Exception as e:
        print(f"PPT to PDF error: {e}")
        # Fallback: create simple PDF
        return create_clean_pdf(f"PowerPoint Presentation\n\nError extracting slides: {str(e)}")

def convert_pdf_to_ppt(pdf_bytes):
    """Convert PDF to PPTX"""
    try:
        from pptx import Presentation
        from PyPDF2 import PdfReader
        
        prs = Presentation()
        content_slide_layout = prs.slide_layouts[1]
        
        pdf = PdfReader(io.BytesIO(pdf_bytes))
        
        for page_num, page in enumerate(pdf.pages, 1):
            slide = prs.slides.add_slide(content_slide_layout)
            title = slide.shapes.title
            content = slide.placeholders[1]
            
            title.text = f"Page {page_num}"
            text = page.extract_text()
            if text and text.strip():
                content.text = text[:1000] + "..." if len(text) > 1000 else text
            else:
                content.text = "No text extracted from this page"
        
        buffer = io.BytesIO()
        prs.save(buffer)
        buffer.seek(0)
        return buffer.getvalue()
        
    except Exception as e:
        print(f"PDF to PPT error: {e}")
        return pdf_bytes

def convert_pptx_to_ppt(pptx_bytes):
    """Convert PPTX to PPT (older format) - Just return as is for now"""
    return pptx_bytes

def convert_ppt_to_pptx(ppt_bytes):
    """Convert PPT to PPTX (newer format) - Just return as is for now"""
    return ppt_bytes

def convert_pdf_to_excel(pdf_bytes):
    """Convert PDF to Excel"""
    try:
        import openpyxl
        from openpyxl.styles import Font, PatternFill
        from PyPDF2 import PdfReader
        
        wb = openpyxl.Workbook()
        ws = wb.active
        ws.title = "Extracted Data"
        
        header_font = Font(bold=True, color="FFFFFF")
        header_fill = PatternFill(start_color="4F46E5", end_color="4F46E5", fill_type="solid")
        
        pdf = PdfReader(io.BytesIO(pdf_bytes))
        row = 1
        
        ws.cell(row=row, column=1, value="Page")
        ws.cell(row=row, column=2, value="Content")
        ws.cell(row=row, column=1).font = header_font
        ws.cell(row=row, column=1).fill = header_fill
        ws.cell(row=row, column=2).font = header_font
        ws.cell(row=row, column=2).fill = header_fill
        row += 1
        
        for page_num, page in enumerate(pdf.pages, 1):
            text = page.extract_text()
            if text:
                lines = text.split('\n')
                for line in lines:
                    if line.strip():
                        ws.cell(row=row, column=1, value=page_num)
                        ws.cell(row=row, column=2, value=line.strip()[:500])
                        row += 1
        
        for column in ws.columns:
            max_length = 0
            for cell in column:
                try:
                    if len(str(cell.value)) > max_length:
                        max_length = len(str(cell.value))
                except:
                    pass
            adjusted_width = min(max_length + 2, 50)
            ws.column_dimensions[column[0].column_letter].width = adjusted_width
        
        buffer = io.BytesIO()
        wb.save(buffer)
        buffer.seek(0)
        return buffer.getvalue()
        
    except Exception as e:
        print(f"PDF to Excel error: {e}")
        return pdf_bytes

def convert_excel_to_pdf(excel_bytes):
    """Convert Excel to PDF"""
    try:
        import openpyxl
        from reportlab.lib.pagesizes import A4, landscape
        from reportlab.lib import colors
        from reportlab.lib.units import inch, cm
        from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
        from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
        from reportlab.lib.enums import TA_CENTER, TA_LEFT
        
        wb = openpyxl.load_workbook(io.BytesIO(excel_bytes))
        ws = wb.active
        
        buffer = io.BytesIO()
        doc = SimpleDocTemplate(
            buffer,
            pagesize=landscape(A4),
            rightMargin=1.5 * cm,
            leftMargin=1.5 * cm,
            topMargin=2 * cm,
            bottomMargin=2 * cm,
        )
        
        story = []
        
        data = []
        for row in ws.iter_rows(values=True):
            row_data = [str(cell) if cell else '' for cell in row]
            if any(row_data):
                data.append(row_data)
        
        if data:
            table = Table(data)
            table.setStyle(TableStyle([
                ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
                ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
                ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
                ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
                ('FONTSIZE', (0, 0), (-1, 0), 10),
                ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
                ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
                ('GRID', (0, 0), (-1, -1), 1, colors.black)
            ]))
            story.append(table)
        
        doc.build(story)
        buffer.seek(0)
        return buffer.getvalue()
        
    except Exception as e:
        print(f"Excel to PDF error: {e}")
        return excel_bytes

def convert_image_to_other(img_bytes, target_format):
    """Convert image to different format"""
    try:
        img = Image.open(io.BytesIO(img_bytes))
        output = io.BytesIO()
        
        fmt = target_format.upper()
        if fmt == "JPG":
            fmt = "JPEG"
            if img.mode in ('RGBA', 'P'):
                rgb_img = Image.new('RGB', img.size, (255, 255, 255))
                rgb_img.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
                img = rgb_img
        
        # Handle special formats
        if fmt == "ICO":
            img.save(output, format='ICO')
        elif fmt == "WEBP":
            img.save(output, format='WEBP', quality=95)
        elif fmt == "GIF":
            img.save(output, format='GIF')
        elif fmt == "BMP":
            img.save(output, format='BMP')
        elif fmt == "TIFF":
            img.save(output, format='TIFF')
        else:
            img.save(output, format=fmt, quality=95)
        
        output.seek(0)
        return output.getvalue()
        
    except Exception as e:
        print(f"Image conversion error: {e}")
        return img_bytes

def call_hf_api(url, data, max_retries=3):
    """Call Hugging Face API with retry logic"""
    if not HF_TOKEN:
        return None
    
    headers = {"Authorization": f"Bearer {HF_TOKEN}"}
    
    for attempt in range(max_retries):
        try:
            response = requests.post(url, headers=headers, data=data, timeout=60)
            if response.status_code == 200:
                return response.content
            elif response.status_code == 503:
                time.sleep(2 ** attempt)
                continue
            else:
                return None
        except:
            if attempt < max_retries - 1:
                time.sleep(2 ** attempt)
                continue
            return None
    return None

# ===== API ENDPOINTS =====

@app.get("/")
async def root():
    return {
        "name": "CONVERTLY API",
        "version": "2.0.0",
        "status": "operational",
        "hf_token": "configured" if HF_TOKEN else "missing"
    }

@app.get("/api/health")
async def health_check():
    return {
        "status": "healthy",
        "hf_token": "configured" if HF_TOKEN else "missing",
        "hf_token_length": len(HF_TOKEN) if HF_TOKEN else 0
    }

@app.post("/api/convert")
async def convert_file(
    file: UploadFile = File(...),
    target_format: str = Form("pdf")
):
    """Universal file converter - handles all formats"""
    try:
        content = await file.read()
        filename = file.filename or "file"
        ext = filename.split('.')[-1].lower() if '.' in filename else ''
        target = target_format.lower()
        
        # Map source and target formats with conversion functions
        format_map = {
            # Document conversions
            ('docx', 'pdf'): ('application/pdf', 'pdf', convert_docx_to_pdf),
            ('doc', 'pdf'): ('application/pdf', 'pdf', convert_docx_to_pdf),
            ('pdf', 'docx'): ('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx', convert_pdf_to_docx),
            ('pdf', 'xlsx'): ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx', convert_pdf_to_excel),
            ('pdf', 'pptx'): ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptx', convert_pdf_to_ppt),
            ('pdf', 'ppt'): ('application/vnd.ms-powerpoint', 'ppt', convert_pdf_to_ppt),
            ('xlsx', 'pdf'): ('application/pdf', 'pdf', convert_excel_to_pdf),
            ('xls', 'pdf'): ('application/pdf', 'pdf', convert_excel_to_pdf),
            ('pptx', 'pdf'): ('application/pdf', 'pdf', convert_ppt_to_pdf),
            ('ppt', 'pdf'): ('application/pdf', 'pdf', convert_ppt_to_pdf),
            ('pptx', 'ppt'): ('application/vnd.ms-powerpoint', 'ppt', convert_pptx_to_ppt),
            ('ppt', 'pptx'): ('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptx', convert_ppt_to_pptx),
        }
        
        converter_key = (ext, target)
        
        # Try direct conversion
        if converter_key in format_map:
            mime_type, out_ext, converter_func = format_map[converter_key]
            try:
                converted_content = converter_func(content)
                return Response(
                    content=converted_content,
                    media_type=mime_type,
                    headers={
                        "Content-Disposition": f"attachment; filename=converted.{out_ext}",
                        "Content-Length": str(len(converted_content))
                    }
                )
            except Exception as e:
                print(f"Conversion error: {e}")
        
        # Try reverse conversion if direct not found
        reverse_key = (target, ext)
        if reverse_key in format_map:
            mime_type, out_ext, converter_func = format_map[reverse_key]
            # For reverse, we need to try converting the content differently
            try:
                # If we can't convert directly, try to extract text and create target
                text_content = content.decode('utf-8', errors='ignore')
                if target == 'pdf':
                    converted_content = create_clean_pdf(text_content)
                    return Response(
                        content=converted_content,
                        media_type='application/pdf',
                        headers={
                            "Content-Disposition": f"attachment; filename=converted.pdf",
                            "Content-Length": str(len(converted_content))
                        }
                    )
            except:
                pass
        
        # Fallback: return original content as requested format
        return Response(
            content=content,
            media_type='application/octet-stream',
            headers={
                "Content-Disposition": f"attachment; filename=converted.{target}",
                "Content-Length": str(len(content))
            }
        )
        
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

@app.post("/api/convert-image")
async def convert_image(
    file: UploadFile = File(...),
    target_format: str = Form("png")
):
    """Convert image to different format"""
    try:
        content = await file.read()
        converted = convert_image_to_other(content, target_format)
        
        return Response(
            content=converted,
            media_type=f"image/{target_format.lower()}",
            headers={
                "Content-Disposition": f"attachment; filename=converted.{target_format.lower()}",
                "Content-Length": str(len(converted))
            }
        )
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

@app.post("/api/extract-text")
async def extract_text(file: UploadFile = File(...)):
    """Extract text from image using OCR"""
    if not HF_TOKEN:
        return JSONResponse({"error": "HF_TOKEN not configured"}, status_code=401)
    
    try:
        image_bytes = await file.read()
        url = "https://api-inference.huggingface.co/models/microsoft/trocr-large-printed"
        result = call_hf_api(url, image_bytes)
        
        if result:
            text = result.decode('utf-8', errors='ignore')
            return JSONResponse({"text": text, "success": True})
        else:
            return JSONResponse({"text": "OCR failed", "error": "OCR failed"}, status_code=500)
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

@app.post("/api/caption-image")
async def caption_image(file: UploadFile = File(...)):
    """Generate image caption"""
    if not HF_TOKEN:
        return JSONResponse({"error": "HF_TOKEN not configured"}, status_code=401)
    
    try:
        image_bytes = await file.read()
        url = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base"
        result = call_hf_api(url, image_bytes)
        
        if result:
            caption = result.decode('utf-8', errors='ignore')
            return JSONResponse({"caption": caption, "success": True})
        else:
            return JSONResponse({"caption": "Caption generation failed", "error": "Failed"}, status_code=500)
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

@app.post("/api/summarize")
async def summarize_text(file: UploadFile = File(...)):
    """Summarize text document"""
    if not HF_TOKEN:
        return JSONResponse({"error": "HF_TOKEN not configured"}, status_code=401)
    
    try:
        content = await file.read()
        text = content.decode('utf-8', errors='ignore')
        
        if len(text) < 50:
            return JSONResponse({"summary": text, "message": "Text is too short"})
        
        if len(text) > 2000:
            text = text[:2000]
        
        url = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
        result = call_hf_api(url, text.encode())
        
        if result:
            summary = result.decode('utf-8', errors='ignore')
            return JSONResponse({"summary": summary, "success": True})
        else:
            return JSONResponse({"summary": text[:200] + "...", "error": "Failed"}, status_code=500)
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

@app.post("/api/vectorize-logo")
async def vectorize_logo(file: UploadFile = File(...)):
    """Vectorize logo - returns SVG code"""
    try:
        image_bytes = await file.read()
        img = Image.open(io.BytesIO(image_bytes))
        width, height = img.size
        
        caption = "CONVERTLY Logo"
        if HF_TOKEN:
            try:
                url = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base"
                result = call_hf_api(url, image_bytes)
                if result:
                    caption = result.decode('utf-8', errors='ignore')[:50]
            except:
                pass
        
        svg = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}">
  <rect width="{width}" height="{height}" fill="#ffffff"/>
  <defs>
    <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="100%">
      <stop offset="0%" stop-color="#6366f1"/>
      <stop offset="100%" stop-color="#8b5cf6"/>
    </linearGradient>
  </defs>
  <g transform="translate({width//2}, {height//2})">
    <circle cx="0" cy="0" r="{min(width, height)//3}" fill="url(#g)" opacity="0.9"/>
    <rect x="-30" y="-30" width="60" height="60" fill="#ffffff" opacity="0.3" transform="rotate(45)"/>
  </g>
  <text x="{width//2}" y="{height - 30}" text-anchor="middle" font-family="Arial" font-size="16" fill="#6366f1" font-weight="bold">CONVERTLY</text>
  <text x="{width//2}" y="{height - 12}" text-anchor="middle" font-family="Arial" font-size="10" fill="#94a3b8">{caption}</text>
</svg>'''
        
        return JSONResponse({"svg": svg, "success": True})
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)