Spaces:
Sleeping
Sleeping
File size: 4,358 Bytes
522275f 535a3a5 522275f 535a3a5 522275f 4f9ecb6 535a3a5 3c4ab41 535a3a5 3c4ab41 85fced4 535a3a5 85fced4 522275f 4f9ecb6 522275f 535a3a5 85fced4 535a3a5 85fced4 522275f 85fced4 535a3a5 522275f 4f9ecb6 522275f 4f9ecb6 522275f 4f9ecb6 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
import requests
import json
from typing import List, Dict, Any, Optional
BASE_URL: str = "http://localhost:8000"
def test_health_check() -> None:
"""Test the health check endpoint"""
response: requests.Response = requests.get(f"{BASE_URL}/health")
print("\nHealth check response:")
print(json.dumps(response.json(), indent=2))
def test_model_info() -> None:
"""Test the model info endpoint"""
response: requests.Response = requests.get(f"{BASE_URL}/model-info")
print("\nModel info response:")
print(json.dumps(response.json(), indent=2))
def test_classify_text() -> None:
# Load emails from CSV file
import csv
emails: List[Dict[str, str]] = []
with open("examples/emails.csv", "r", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
emails.append(row)
# Test with default categories using email content
for email in emails[:5]:
response: requests.Response = requests.post(
f"{BASE_URL}/classify",
json={"text": email["contenu"]}
)
print(f"Classification of email '{email['sujet']}' with default categories:")
print(json.dumps(response.json(), indent=2))
def test_classify_batch() -> None:
"""Test the batch classification endpoint"""
# Load emails from CSV file
import csv
emails: List[Dict[str, str]] = []
with open("examples/emails.csv", "r", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
emails.append(row)
# Use the first 5 emails for batch classification
texts: List[str] = [email["contenu"] for email in emails[:5]]
response: requests.Response = requests.post(
f"{BASE_URL}/classify-batch",
json={"texts": texts}
)
print("\nBatch classification results:")
print(json.dumps(response.json(), indent=2))
def test_suggest_categories() -> None:
# Load reviews from CSV file
import csv
texts: List[str] = []
with open("examples/reviews.csv", "r", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
texts.append(row["text"])
# Use the first few reviews for testing
texts = texts[:5]
response: requests.Response = requests.post(
f"{BASE_URL}/suggest-categories",
json=texts
)
print("\nSuggested categories:")
print(json.dumps(response.json(), indent=2))
def test_validate_classifications() -> None:
"""Test the validation endpoint"""
# Load emails from CSV file
import csv
emails: List[Dict[str, str]] = []
with open("examples/emails.csv", "r", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
emails.append(row)
# Create validation samples from the first 5 emails
samples: List[Dict[str, Any]] = []
for email in emails[:5]:
# First classify the email
classify_response: requests.Response = requests.post(
f"{BASE_URL}/classify",
json={"text": email["contenu"]}
)
classification: Dict[str, Any] = classify_response.json()
# Create a validation sample
samples.append({
"text": email["contenu"],
"assigned_category": classification["category"],
"confidence": classification["confidence"]
})
# Get current categories
categories_response: requests.Response = requests.post(
f"{BASE_URL}/suggest-categories",
json=[email["contenu"] for email in emails[:5]]
)
current_categories: List[str] = categories_response.json()["categories"]
# Send validation request
validation_request: Dict[str, Any] = {
"samples": samples,
"current_categories": current_categories,
"text_columns": ["text"]
}
response: requests.Response = requests.post(
f"{BASE_URL}/validate",
json=validation_request
)
print("\nValidation results:")
print(json.dumps(response.json(), indent=2))
if __name__ == "__main__":
print("Testing FastAPI server endpoints...")
test_health_check()
test_model_info()
test_classify_text()
test_classify_batch()
test_suggest_categories()
test_validate_classifications() |