lovebird25 / CURL_EXAMPLES.md
Paul
Initial commit
75146bf

A newer version of the Gradio SDK is available: 6.2.0

Upgrade

CURL Examples for ML Text Classification API

Local Testing

1. Health Check

curl http://localhost:8000/health

Expected Response:

{"status": "healthy"}

2. Root Endpoint

curl http://localhost:8000/

Expected Response:

{
  "message": "ML Text Classification API",
  "version": "1.0.0",
  "endpoints": {
    "health": "/health",
    "predict": "/predict"
  }
}

3. Prediction Endpoint (Main)

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hi ||| Hi anh"}'

Expected Response:

{
  "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

curl https://your-project.vercel.app/health

2. Root Endpoint

curl https://your-project.vercel.app/

3. Prediction Endpoint

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

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hi ||| Hi anh"}' | jq '.'

Sorting Results by Score

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

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

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello ||| Hi there"}'

Example 2

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)

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{"text": ""}'

Expected Response:

{
  "detail": "Text field is required and cannot be empty"
}

Missing Text Field (422 Error)

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{}'

Expected Response:

{
  "detail": [
    {
      "type": "missing",
      "loc": ["body", "text"],
      "msg": "Field required",
      "input": {}
    }
  ]
}

Python Requests Examples

If you prefer Python instead of curl:

import requests

# Prediction
response = requests.post(
    "http://localhost:8000/predict",
    json={"text": "Hi ||| Hi anh"}
)
print(response.json())

JavaScript/Fetch Examples

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));