File size: 2,241 Bytes
39baca3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Test PDF conversion API locally"""

import requests
import time
import sys

def test_pdf_conversion(pdf_path, api_base_url="http://localhost:7860"):
    """Test PDF conversion through API"""
    
    # 1. Upload PDF
    print(f"Uploading PDF: {pdf_path}")
    with open(pdf_path, 'rb') as f:
        files = {'file': (pdf_path.split('/')[-1], f, 'application/pdf')}
        response = requests.post(f"{api_base_url}/api/convert", files=files)
    
    if response.status_code != 200:
        print(f"Upload failed: {response.status_code}")
        print(response.text)
        return
    
    result = response.json()
    task_id = result['task_id']
    print(f"Task ID: {task_id}")
    print(f"Status: {result['status']}")
    
    # 2. Check status
    print("\nChecking conversion status...")
    while True:
        response = requests.get(f"{api_base_url}/api/status/{task_id}")
        if response.status_code != 200:
            print(f"Status check failed: {response.status_code}")
            break
            
        status = response.json()
        print(f"Status: {status['status']}")
        
        if status['status'] == 'completed':
            print(f"Download URL: {status['download_url']}")
            
            # 3. Download result
            response = requests.get(f"{api_base_url}{status['download_url']}")
            if response.status_code == 200:
                output_file = f"output_{task_id}.md"
                with open(output_file, 'w') as f:
                    f.write(response.text)
                print(f"\nMarkdown saved to: {output_file}")
                print("\nContent preview:")
                print(response.text[:500])
            break
            
        elif status['status'] == 'failed':
            print(f"Conversion failed: {status.get('error', 'Unknown error')}")
            break
            
        time.sleep(1)

if __name__ == "__main__":
    if len(sys.argv) > 1:
        pdf_path = sys.argv[1]
    else:
        pdf_path = "/Users/marcos/Documents/projects/pdf2md/batch-files/test-simple.pdf"
    
    # Test on HF Space
    print("Testing on Hugging Face Space...")
    test_pdf_conversion(pdf_path, "https://marcosremar2-mineru2.hf.space")