Download test.py from dipan004/Proofly: direct link, hf CLI and curl.
- Browser
- Download file 4.82 kB
-
https://huggingface.co/spaces/dipan004/Proofly/resolve/main/test.py
- Command line
-
hf download hf://spaces/dipan004/Proofly/test.py
-
curl -L -o test.py https://huggingface.co/spaces/dipan004/Proofly/resolve/main/test.py
4.82 kB
| """ | |
| Simple test script for uploading images to the proof system. | |
| """ | |
| import requests | |
| import base64 | |
| import json | |
| from pathlib import Path | |
| # API base URL | |
| BASE_URL = "http://127.0.0.1:8000" | |
| def test_text_proof(): | |
| """Test creating a proof from text.""" | |
| print("\n=== Testing Text Proof ===") | |
| response = requests.post( | |
| f"{BASE_URL}/proof/create/text", | |
| json={"content": "Hello, this is a test document!"} | |
| ) | |
| result = response.json() | |
| print(f"Success: {result.get('success')}") | |
| print(f"Proof ID: {result.get('proof_id')}") | |
| print(f"Hash: {result.get('hash')}") | |
| if 'assistant' in result and result['assistant']: | |
| print(f"\n🤖 AI: {result['assistant']['response']}") | |
| return result.get('proof_id') | |
| def test_image_proof(image_path): | |
| """Test creating a proof from an image.""" | |
| print(f"\n=== Testing Image Proof: {image_path} ===") | |
| # Check if file exists | |
| if not Path(image_path).exists(): | |
| print(f"❌ Error: File not found: {image_path}") | |
| print(f"Current directory: {Path.cwd()}") | |
| print(f"Available images: {list(Path('.').glob('*.png')) + list(Path('.').glob('*.jpg'))}") | |
| return None | |
| # Use FastAPI's multipart/form-data upload (not base64) | |
| with open(image_path, "rb") as f: | |
| files = {"file": (Path(image_path).name, f, "image/png")} | |
| print(f"Uploading {Path(image_path).name}...") | |
| response = requests.post( | |
| f"{BASE_URL}/proof/create/file", | |
| files=files | |
| ) | |
| if response.status_code != 200: | |
| print(f"❌ API Error {response.status_code}: {response.text}") | |
| return None | |
| result = response.json() | |
| if not result.get('success'): | |
| print(f"❌ Failed: {result.get('message')}") | |
| if 'error' in result: | |
| print(f"Error: {result['error']}") | |
| return None | |
| print(f"✅ Success: {result.get('success')}") | |
| print(f"Proof ID: {result.get('proof_id')}") | |
| print(f"Hash: {result.get('hash')}") | |
| print(f"OCR Status: {result.get('ocr_status')}") | |
| if result.get('extracted_text'): | |
| text = result['extracted_text'] | |
| preview = text[:100] + "..." if len(text) > 100 else text | |
| print(f"Extracted Text: {preview}") | |
| else: | |
| print("Extracted Text: (none - this is normal for photos)") | |
| if 'assistant' in result and result['assistant']: | |
| print(f"\n🤖 AI: {result['assistant']['response']}") | |
| return result.get('proof_id'), Path(image_path).name | |
| def test_verify_proof(proof_id, original_content, filename=None): | |
| """Test verifying a proof.""" | |
| print(f"\n=== Testing Verification: {proof_id} ===") | |
| # For file uploads, we need to re-upload the file | |
| if filename: | |
| print("⚠️ File verification requires the original file") | |
| print(f" API endpoint: POST /proof/verify") | |
| print(f" Body: {{'proof_id': '{proof_id}', 'content': 'original_content_as_string'}}") | |
| return | |
| # For text content | |
| if isinstance(original_content, bytes): | |
| content_str = original_content.decode('utf-8') | |
| else: | |
| content_str = original_content | |
| response = requests.post( | |
| f"{BASE_URL}/proof/verify", | |
| json={ | |
| "proof_id": proof_id, | |
| "content": content_str | |
| } | |
| ) | |
| if response.status_code != 200: | |
| print(f"❌ API Error {response.status_code}: {response.text}") | |
| return | |
| result = response.json() | |
| print(f"Valid: {result.get('is_valid')}") | |
| print(f"Message: {result.get('message')}") | |
| if 'assistant' in result and result['assistant']: | |
| print(f"\n🤖 AI: {result['assistant']['response']}") | |
| def main(): | |
| """Run all tests.""" | |
| print("=" * 60) | |
| print("PROOF SYSTEM - API TESTS") | |
| print("=" * 60) | |
| # Test 1: Text proof | |
| text_proof_id = test_text_proof() | |
| # Test 2: Verify text proof | |
| if text_proof_id: | |
| test_verify_proof(text_proof_id, "Hello, this is a test document!") | |
| # Test 3: Image proof | |
| image_files = ["test1.png", "test1.jpg", "test1.jpeg"] | |
| image_path = None | |
| for img in image_files: | |
| if Path(img).exists(): | |
| image_path = img | |
| break | |
| if image_path: | |
| result = test_image_proof(image_path) | |
| if result: | |
| image_proof_id, filename = result | |
| # Note: File verification needs special handling | |
| print(f"\n💡 To verify the image proof, use proof ID: {image_proof_id}") | |
| else: | |
| print("\n⚠️ No test1 image found. Place test1.png in the backend folder.") | |
| print("\n" + "=" * 60) | |
| print("Tests completed!") | |
| print("=" * 60) | |
| if __name__ == "__main__": | |
| main() |