Spaces:
Sleeping
Sleeping
File size: 2,378 Bytes
6db4426 |
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 |
import json
import requests
# Replace with the actual URL where your FastAPI app is running locally
LOCAL_API_URL = "http://127.0.0.1:8000/classify"
def test_classify_endpoint(email_body):
"""
Sends a POST request to the /classify endpoint of the local FastAPI app.
Args:
email_body (str): The email content to be classified.
Returns:
dict: The JSON response from the API, or None if an error occurred.
"""
headers = {"Content-Type": "application/json"}
payload = {"input_email_body": email_body}
try:
response = requests.post(LOCAL_API_URL, headers=headers, json=payload)
response.raise_for_status() # Raise an exception for bad status codes
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error connecting to the API: {e}")
return None
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
return None
except json.JSONDecodeError as e:
print(f"Error decoding JSON response: {e}")
return None
if __name__ == "__main__":
# Example email bodies to test
test_emails = [
"Hello, my name is Alice Smith and my email is alice.smith@example.com. I'm having trouble with my account.",
"Urgent: My credit card number is 1234-5678-9012-3456 and the expiry is 03/27. I was overcharged.",
"Subject: Network down - Office B1 floor. Please investigate.",
"Request for new software installation on my laptop.",
"Regarding invoice INV-2023-10-01. The total seems incorrect.",
"My date of birth is 01/15/1990 and my phone number is 987-654-3210.",
"Is there a problem with the server?",
"I need to change my registered address.",
"Subject: Unplanned system outage affecting database access.",
"Can I get access to the premium features?"
]
print("Testing the /classify endpoint on localhost:")
for i, email in enumerate(test_emails):
print(f"\n--- Test Email {i+1} ---")
print(f"Input Email Body: {email}")
response_data = test_classify_endpoint(email)
if response_data:
print("API Response:")
print(json.dumps(response_data, indent=4))
else:
print("Failed to get a valid response from the API.")
print("\nTesting complete.") |