ksvmuralidhar commited on
Commit
488b5e4
·
verified ·
1 Parent(s): 144b3c3

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +21 -6
api.py CHANGED
@@ -124,21 +124,31 @@ class URLList(BaseModel):
124
  class SuccessfulResponse(BaseModel):
125
  urls: List[str] = Field(..., description="List of URLs of news articles inputted by the user")
126
  scraped_texts: List[str] = Field(..., description="List of scraped text from input URLs")
127
- scrape_errors: List[str] = Field("", description="List of errors raised during scraping. One item for corresponding URL")
128
  summaries: List[str] = Field(..., description="List of generated summaries of news articles")
129
- summarizer_error: str = Field("", description="String specifying error raised during summary")
130
 
131
  class AuthenticationError(BaseModel):
132
  urls: List[str] = Field(..., description="List of URLs of news articles inputted by the user")
133
- scraped_texts: List[str] = Field("", description="List of empty strings as authentication failed")
134
- scrape_errors: List[str] = Field("", description="List of empty strings as authentication failed")
135
- summaries: List[str] = Field("", description="List of empty strings as authentication failed")
 
 
 
 
 
 
 
136
  summarizer_error: str = Field("Authentication error: Invalid API key.")
137
 
138
 
139
  class NewsSummarizerAPIAuthenticationError(Exception):
140
  pass
141
 
 
 
 
142
 
143
  def authenticate_key(api_key: str):
144
  if api_key != os.getenv('API_KEY'):
@@ -147,7 +157,8 @@ def authenticate_key(api_key: str):
147
 
148
  @app.post("/generate_summary/", response_model=List[SuccessfulResponse],
149
  responses={
150
- 401: {"model": AuthenticationError, "description": "authentication Error"}
 
151
  })
152
  async def read_items(q: URLList):
153
  try:
@@ -162,6 +173,10 @@ async def read_items(q: URLList):
162
  api_key = request_json['key']
163
  _ = authenticate_key(api_key)
164
  scraped_texts, scrape_errors = await scrape_urls(urls)
 
 
 
 
165
  summaries = await summ_inference(scraped_texts)
166
  status_code = 200
167
  response_json = {'urls': urls, 'scraped_texts': scraped_texts, 'scrape_errors': scrape_errors, 'summaries': summaries, 'summarizer_error': ''}
 
124
  class SuccessfulResponse(BaseModel):
125
  urls: List[str] = Field(..., description="List of URLs of news articles inputted by the user")
126
  scraped_texts: List[str] = Field(..., description="List of scraped text from input URLs")
127
+ scrape_errors: List[str] = Field(..., description="List of errors raised during scraping. One item for corresponding URL")
128
  summaries: List[str] = Field(..., description="List of generated summaries of news articles")
129
+ summarizer_error: str = Field("", description="Blank as the response code is 200")
130
 
131
  class AuthenticationError(BaseModel):
132
  urls: List[str] = Field(..., description="List of URLs of news articles inputted by the user")
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: List[str] = Field("", description="List of empty summaries as summarizer returned an error")
143
  summarizer_error: str = Field("Authentication error: Invalid API key.")
144
 
145
 
146
  class NewsSummarizerAPIAuthenticationError(Exception):
147
  pass
148
 
149
+ class NewsSummarizerAPIScrapingError(Exception):
150
+ pass
151
+
152
 
153
  def authenticate_key(api_key: str):
154
  if api_key != os.getenv('API_KEY'):
 
157
 
158
  @app.post("/generate_summary/", response_model=List[SuccessfulResponse],
159
  responses={
160
+ 401: {"model": AuthenticationError, "description": "authentication Error"},
161
+ 500: {"model": AuthenticationError, "description": "authentication Error"}
162
  })
163
  async def read_items(q: URLList):
164
  try:
 
173
  api_key = request_json['key']
174
  _ = authenticate_key(api_key)
175
  scraped_texts, scrape_errors = await scrape_urls(urls)
176
+
177
+ if [*set(scraped_texts)][0] == "":
178
+ raise NewsSummarizerAPIScrapingError("Scrape Error: Couldn't scrape text from any of the URLs")
179
+
180
  summaries = await summ_inference(scraped_texts)
181
  status_code = 200
182
  response_json = {'urls': urls, 'scraped_texts': scraped_texts, 'scrape_errors': scrape_errors, 'summaries': summaries, 'summarizer_error': ''}