File size: 1,595 Bytes
dd4466b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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