File size: 2,174 Bytes
98971da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import sys

def test_api(base_url, file_path, lang="fr"):
    """
    Teste l'API OCR

    Usage:
        python test_api.py https://ton-space.hf.space document.pdf fr
    """

    print(f"🧪 Test de l'API: {base_url}")
    print(f"📄 Fichier: {file_path}")
    print(f"🌍 Langue: {lang}\n")

    # Test 1: Health check
    print("1️⃣ Test health check...")
    try:
        response = requests.get(f"{base_url}/health")
        if response.status_code == 200:
            print("✅ API en ligne\n")
        else:
            print(f"❌ Erreur: {response.status_code}\n")
            return
    except Exception as e:
        print(f"❌ Impossible de contacter l'API: {e}\n")
        return

    # Test 2: Extraction de texte
    print("2️⃣ Test extraction de texte...")
    try:
        with open(file_path, "rb") as f:
            files = {"file": f}
            data = {"lang": lang}
            response = requests.post(f"{base_url}/extract", files=files, data=data)

        if response.status_code == 200:
            result = response.json()
            text = result.get("text", "")

            print("✅ Extraction réussie!")
            print(f"\n📝 Texte extrait ({len(text)} caractères):")
            print("-" * 50)
            print(text[:500])  # Afficher les 500 premiers caractères
            if len(text) > 500:
                print(f"\n... ({len(text) - 500} caractères restants)")
            print("-" * 50)
        else:
            print(f"❌ Erreur {response.status_code}: {response.text}")

    except FileNotFoundError:
        print(f"❌ Fichier introuvable: {file_path}")
    except Exception as e:
        print(f"❌ Erreur: {e}")

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python test_api.py <URL_API> <FICHIER> [LANGUE]")
        print("\nExemple:")
        print("  python test_api.py https://ton-space.hf.space document.pdf fr")
        print("  python test_api.py http://localhost:8000 image.jpg en")
        sys.exit(1)

    url = sys.argv[1]
    file = sys.argv[2]
    language = sys.argv[3] if len(sys.argv) > 3 else "fr"

    test_api(url, file, language)