| import requests | |
| import json | |
| # Replace this URL with your actual Hugging Face Space URL once deployed | |
| # For local testing (e.g., python manage.py runserver), use: "[http://127.0.0.1:8000/api/categorize/](http://127.0.0.1:8000/api/categorize/)" | |
| API_URL = "http://127.0.0.1:8000/api/categorize/" | |
| def test_categorize_expense(expense_text): | |
| # The JSON body that your Django API expects | |
| payload = {"text": expense_text} | |
| headers = {"Content-Type": "application/json"} | |
| print(f"Sending request to: {API_URL}") | |
| print(f"Expense: '{expense_text}'\n") | |
| try: | |
| # Send the POST request to your API | |
| response = requests.post(API_URL, json=payload, headers=headers) | |
| # Check if the request was successful | |
| if response.status_code == 200: | |
| print("--- API Response ---") | |
| # Parse and print the formatted JSON response | |
| print(json.dumps(response.json(), indent=4)) | |
| else: | |
| print(f"Error: API returned status code {response.status_code}") | |
| print(response.text) | |
| except requests.exceptions.RequestException as e: | |
| print(f"Connection Error: Could not connect to the API. Details: {e}") | |
| if __name__ == "__main__": | |
| # Test cases to check your API | |
| print("Starting API tests...\n") | |
| test_categorize_expense("I spent 150 rupees on a burger today") | |
| print("-" * 40) | |
| test_categorize_expense("Paid 1,500.50 for the electricity and water bill") | |
| print("-" * 40) | |
| test_categorize_expense( | |
| "Bought paracetamol and cough syrup" | |
| ) # Will return amount: null | |