Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
@@ -133,14 +133,14 @@ class AuthenticationError(BaseModel):
|
|
133 |
scraped_texts: str = Field("", description="Empty string as authentication failed")
|
134 |
scrape_errors: str = Field("", description="Empty string as authentication failed")
|
135 |
summaries: str = Field("", description="Empty string as authentication failed")
|
136 |
-
summarizer_error: str = Field("Authentication error: Invalid API key.")
|
137 |
|
138 |
class SummaryError(BaseModel):
|
139 |
urls: List[str] = Field(..., description="List of URLs of news articles inputted by the user")
|
140 |
scraped_texts: List[str] = Field("", description="List of scraped text from input URLs")
|
141 |
scrape_errors: List[str] = Field("", description="List of errors raised during scraping. One item for corresponding URL")
|
142 |
-
summaries:
|
143 |
-
summarizer_error: str = Field("
|
144 |
|
145 |
|
146 |
class NewsSummarizerAPIAuthenticationError(Exception):
|
@@ -157,8 +157,8 @@ def authenticate_key(api_key: str):
|
|
157 |
|
158 |
@app.post("/generate_summary/", response_model=List[SuccessfulResponse],
|
159 |
responses={
|
160 |
-
401: {"model": AuthenticationError, "description": "
|
161 |
-
500: {"model":
|
162 |
})
|
163 |
async def read_items(q: URLList):
|
164 |
try:
|
@@ -174,7 +174,8 @@ async def read_items(q: URLList):
|
|
174 |
_ = authenticate_key(api_key)
|
175 |
scraped_texts, scrape_errors = await scrape_urls(urls)
|
176 |
|
177 |
-
|
|
|
178 |
raise NewsSummarizerAPIScrapingError("Scrape Error: Couldn't scrape text from any of the URLs")
|
179 |
|
180 |
summaries = await summ_inference(scraped_texts)
|
@@ -184,7 +185,7 @@ async def read_items(q: URLList):
|
|
184 |
status_code = 500
|
185 |
if e.__class__.__name__ == "NewsSummarizerAPIAuthenticationError":
|
186 |
status_code = 401
|
187 |
-
response_json = {'urls': urls, 'scraped_texts': scraped_texts, 'scrape_errors': scrape_errors, 'summaries': "", 'summarizer_error': f'
|
188 |
|
189 |
json_str = json.dumps(response_json, indent=5) # convert dict to JSON str
|
190 |
return Response(content=json_str, media_type='application/json', status_code=status_code)
|
|
|
133 |
scraped_texts: str = Field("", description="Empty string as authentication failed")
|
134 |
scrape_errors: str = Field("", description="Empty string as authentication failed")
|
135 |
summaries: str = Field("", description="Empty string as authentication failed")
|
136 |
+
summarizer_error: str = Field("Error: Authentication error: Invalid API key.")
|
137 |
|
138 |
class SummaryError(BaseModel):
|
139 |
urls: List[str] = Field(..., description="List of URLs of news articles inputted by the user")
|
140 |
scraped_texts: List[str] = Field("", description="List of scraped text from input URLs")
|
141 |
scrape_errors: List[str] = Field("", description="List of errors raised during scraping. One item for corresponding URL")
|
142 |
+
summaries: str = Field("", description="Empty string as summarizer encountered an error")
|
143 |
+
summarizer_error: str = Field("Error: Summarizer Error with a message describing the error")
|
144 |
|
145 |
|
146 |
class NewsSummarizerAPIAuthenticationError(Exception):
|
|
|
157 |
|
158 |
@app.post("/generate_summary/", response_model=List[SuccessfulResponse],
|
159 |
responses={
|
160 |
+
401: {"model": AuthenticationError, "description": "Authentication Error"},
|
161 |
+
500: {"model": SummaryError, "description": "Summarizer Error"}
|
162 |
})
|
163 |
async def read_items(q: URLList):
|
164 |
try:
|
|
|
174 |
_ = authenticate_key(api_key)
|
175 |
scraped_texts, scrape_errors = await scrape_urls(urls)
|
176 |
|
177 |
+
unique_scraped_texts = [*set(scraped_texts)]
|
178 |
+
if (unique_scraped_texts[0] == "") and (len(unique_scraped_texts) == 1):
|
179 |
raise NewsSummarizerAPIScrapingError("Scrape Error: Couldn't scrape text from any of the URLs")
|
180 |
|
181 |
summaries = await summ_inference(scraped_texts)
|
|
|
185 |
status_code = 500
|
186 |
if e.__class__.__name__ == "NewsSummarizerAPIAuthenticationError":
|
187 |
status_code = 401
|
188 |
+
response_json = {'urls': urls, 'scraped_texts': scraped_texts, 'scrape_errors': scrape_errors, 'summaries': "", 'summarizer_error': f'Error: {e}'}
|
189 |
|
190 |
json_str = json.dumps(response_json, indent=5) # convert dict to JSON str
|
191 |
return Response(content=json_str, media_type='application/json', status_code=status_code)
|