Spaces:
Sleeping
Sleeping
ming
commited on
Commit
·
0d683e2
1
Parent(s):
9024ad9
feat(api): add /api/v1/summarize endpoint and integration tests
Browse files- app/api/v1/routes.py +4 -5
- app/api/v1/summarize.py +24 -0
- tests/test_api.py +39 -0
app/api/v1/routes.py
CHANGED
|
@@ -3,11 +3,10 @@ API v1 routes for the text summarizer backend.
|
|
| 3 |
"""
|
| 4 |
from fastapi import APIRouter
|
| 5 |
|
|
|
|
|
|
|
| 6 |
# Create API router
|
| 7 |
api_router = APIRouter()
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# api_router.include_router(summarize.router, prefix="/summarize", tags=["summarize"])
|
| 13 |
-
# api_router.include_router(health.router, prefix="/health", tags=["health"])
|
|
|
|
| 3 |
"""
|
| 4 |
from fastapi import APIRouter
|
| 5 |
|
| 6 |
+
from .summarize import router as summarize_router
|
| 7 |
+
|
| 8 |
# Create API router
|
| 9 |
api_router = APIRouter()
|
| 10 |
|
| 11 |
+
# Include v1 routers
|
| 12 |
+
api_router.include_router(summarize_router, prefix="/summarize", tags=["summarize"])
|
|
|
|
|
|
|
|
|
app/api/v1/summarize.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Summarization endpoints.
|
| 3 |
+
"""
|
| 4 |
+
from fastapi import APIRouter, HTTPException
|
| 5 |
+
from app.api.v1.schemas import SummarizeRequest, SummarizeResponse
|
| 6 |
+
from app.services.summarizer import ollama_service
|
| 7 |
+
|
| 8 |
+
router = APIRouter()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@router.post("/", response_model=SummarizeResponse)
|
| 12 |
+
async def summarize(payload: SummarizeRequest) -> SummarizeResponse:
|
| 13 |
+
"""Summarize input text using Ollama service."""
|
| 14 |
+
try:
|
| 15 |
+
result = await ollama_service.summarize_text(
|
| 16 |
+
text=payload.text,
|
| 17 |
+
max_tokens=payload.max_tokens or 256,
|
| 18 |
+
prompt=payload.prompt or "Summarize the following text concisely:",
|
| 19 |
+
)
|
| 20 |
+
return SummarizeResponse(**result)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
raise HTTPException(status_code=502, detail=f"Summarization failed: {str(e)}")
|
| 23 |
+
|
| 24 |
+
|
tests/test_api.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Integration tests for API endpoints.
|
| 3 |
+
"""
|
| 4 |
+
import pytest
|
| 5 |
+
from unittest.mock import patch
|
| 6 |
+
from fastapi.testclient import TestClient
|
| 7 |
+
from app.main import app
|
| 8 |
+
|
| 9 |
+
from tests.test_services import StubAsyncClient, StubAsyncResponse
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
client = TestClient(app)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@pytest.mark.integration
|
| 16 |
+
def test_summarize_endpoint_success(sample_text, mock_ollama_response):
|
| 17 |
+
"""Test successful summarization via API endpoint."""
|
| 18 |
+
stub_response = StubAsyncResponse(json_data=mock_ollama_response)
|
| 19 |
+
with patch('httpx.AsyncClient', return_value=StubAsyncClient(post_result=stub_response)):
|
| 20 |
+
resp = client.post(
|
| 21 |
+
"/api/v1/summarize/",
|
| 22 |
+
json={"text": sample_text, "max_tokens": 128}
|
| 23 |
+
)
|
| 24 |
+
assert resp.status_code == 200
|
| 25 |
+
data = resp.json()
|
| 26 |
+
assert data["summary"] == mock_ollama_response["response"]
|
| 27 |
+
assert data["model"]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@pytest.mark.integration
|
| 31 |
+
def test_summarize_endpoint_validation_error():
|
| 32 |
+
"""Test validation error for empty text."""
|
| 33 |
+
resp = client.post(
|
| 34 |
+
"/api/v1/summarize/",
|
| 35 |
+
json={"text": ""}
|
| 36 |
+
)
|
| 37 |
+
assert resp.status_code == 422
|
| 38 |
+
|
| 39 |
+
|