Spaces:
Running
Running
| 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) | |