Spaces:
Paused
Paused
| # CURL Examples for ML Text Classification API | |
| ## Local Testing | |
| ### 1. Health Check | |
| ```bash | |
| curl http://localhost:8000/health | |
| ``` | |
| Expected Response: | |
| ```json | |
| {"status": "healthy"} | |
| ``` | |
| ### 2. Root Endpoint | |
| ```bash | |
| curl http://localhost:8000/ | |
| ``` | |
| Expected Response: | |
| ```json | |
| { | |
| "message": "ML Text Classification API", | |
| "version": "1.0.0", | |
| "endpoints": { | |
| "health": "/health", | |
| "predict": "/predict" | |
| } | |
| } | |
| ``` | |
| ### 3. Prediction Endpoint (Main) | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Hi ||| Hi anh"}' | |
| ``` | |
| Expected Response: | |
| ```json | |
| { | |
| "results": [ | |
| {"label": "label_name_1", "score": 0.95}, | |
| {"label": "label_name_2", "score": 0.03}, | |
| ... | |
| ] | |
| } | |
| ``` | |
| ## Vercel / Production Deployment | |
| After deploying, replace `your-project.vercel.app` with your actual domain: | |
| ### 1. Health Check | |
| ```bash | |
| curl https://your-project.vercel.app/health | |
| ``` | |
| ### 2. Root Endpoint | |
| ```bash | |
| curl https://your-project.vercel.app/ | |
| ``` | |
| ### 3. Prediction Endpoint | |
| ```bash | |
| curl -X POST "https://your-project.vercel.app/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Hi ||| Hi anh"}' | |
| ``` | |
| ## Advanced Examples | |
| ### Using jq for Pretty Output | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Hi ||| Hi anh"}' | jq '.' | |
| ``` | |
| ### Sorting Results by Score | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Hi ||| Hi anh"}' | jq '.results | sort_by(.score) | reverse' | |
| ``` | |
| ### Getting Top 3 Predictions | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Hi ||| Hi anh"}' | jq '.results | sort_by(.score) | reverse | .[0:3]' | |
| ``` | |
| ## Testing with Different Text | |
| ### Example 1 | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Hello ||| Hi there"}' | |
| ``` | |
| ### Example 2 | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Xin chào ||| Chào bạn"}' | |
| ``` | |
| ## Error Handling | |
| ### Empty Text (400 Error) | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": ""}' | |
| ``` | |
| Expected Response: | |
| ```json | |
| { | |
| "detail": "Text field is required and cannot be empty" | |
| } | |
| ``` | |
| ### Missing Text Field (422 Error) | |
| ```bash | |
| curl -X POST "http://localhost:8000/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{}' | |
| ``` | |
| Expected Response: | |
| ```json | |
| { | |
| "detail": [ | |
| { | |
| "type": "missing", | |
| "loc": ["body", "text"], | |
| "msg": "Field required", | |
| "input": {} | |
| } | |
| ] | |
| } | |
| ``` | |
| ## Python Requests Examples | |
| If you prefer Python instead of curl: | |
| ```python | |
| import requests | |
| # Prediction | |
| response = requests.post( | |
| "http://localhost:8000/predict", | |
| json={"text": "Hi ||| Hi anh"} | |
| ) | |
| print(response.json()) | |
| ``` | |
| ## JavaScript/Fetch Examples | |
| ```javascript | |
| fetch('http://localhost:8000/predict', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ | |
| text: 'Hi ||| Hi anh' | |
| }) | |
| }) | |
| .then(response => response.json()) | |
| .then(data => console.log(data)); | |
| ``` | |