Spaces:
Sleeping
Sleeping
ksvmuralidhar
commited on
Commit
•
4733c2f
1
Parent(s):
ce71542
Update api.py
Browse files
api.py
CHANGED
@@ -6,7 +6,7 @@ import tensorflow as tf
|
|
6 |
from scraper import scrape_text
|
7 |
from fastapi import FastAPI, Response
|
8 |
from typing import List
|
9 |
-
from pydantic import BaseModel, Field
|
10 |
import uvicorn
|
11 |
import json
|
12 |
import logging
|
@@ -135,6 +135,21 @@ def authenticate_key(api_key: str):
|
|
135 |
raise NewsSummarizerAPIAuthenticationError("Authentication error: Invalid API key.")
|
136 |
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
@app.post("/generate_summary/", tags=["Generate Summary"], response_model=List[SuccessfulResponse],
|
139 |
responses={
|
140 |
401: {"model": AuthenticationError, "description": "Authentication Error"},
|
|
|
6 |
from scraper import scrape_text
|
7 |
from fastapi import FastAPI, Response
|
8 |
from typing import List
|
9 |
+
from pydantic import BaseModel, Field, ValidationError
|
10 |
import uvicorn
|
11 |
import json
|
12 |
import logging
|
|
|
135 |
raise NewsSummarizerAPIAuthenticationError("Authentication error: Invalid API key.")
|
136 |
|
137 |
|
138 |
+
@app.exception_handler(ValidationError)
|
139 |
+
async def validation_exception_handler(request: Request, exc: ValidationError):
|
140 |
+
urls = request.query_params.getlist("urls")
|
141 |
+
error_details = exc.errors()
|
142 |
+
error_messages = []
|
143 |
+
for error in error_details:
|
144 |
+
loc = '->'.join(map(str, error['loc']))
|
145 |
+
msg = error['msg']
|
146 |
+
error_messages.append(f"Error at {loc}: {msg}")
|
147 |
+
error_message = "; ".join(error_messages) if error_messages else "Validation error"
|
148 |
+
response_json = {'urls': urls, 'scraped_texts': '', 'scrape_errors': '', 'summaries': "", 'summarizer_error': f'Error: {error_message}'}
|
149 |
+
json_str = json.dumps(response_json, indent=5) # convert dict to JSON str
|
150 |
+
return Response(content=json_str, media_type='application/json', status_code=422)
|
151 |
+
|
152 |
+
|
153 |
@app.post("/generate_summary/", tags=["Generate Summary"], response_model=List[SuccessfulResponse],
|
154 |
responses={
|
155 |
401: {"model": AuthenticationError, "description": "Authentication Error"},
|