banjiola commited on
Commit
1bef7ad
·
verified ·
1 Parent(s): 96f3d88

Update app.py

Browse files

Added a way to prevent crash incase of API error

Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -24,7 +24,7 @@ def get_completion(inputs, parameters=None,ENDPOINT_URL=sum_endpoint):
24
  """
25
  This function sends a POST request to a Hugging Face Inference API endpoint and returns the model's output.
26
 
27
- Args:
28
  inputs (str): The input text to be processed by the model (e.g., for summarization, translation, etc.)
29
 
30
  parameters (dict, optional): Additional generation parameters for the model, such as: ### Although it was not used in this program ###
@@ -103,10 +103,20 @@ def summarize_ner(text): # I implemented the validation step here because the mo
103
  else:
104
  summarized = get_completion(text)
105
  # remember I don't have to define a summarization function because the default mode is summarization
106
- summarized = summarized[0]['summary_text']
 
 
 
 
 
 
 
 
 
107
  # let's perform ner on the summarized text
108
  output = ner(summarized)
109
  return output
 
110
 
111
 
112
 
 
24
  """
25
  This function sends a POST request to a Hugging Face Inference API endpoint and returns the model's output.
26
 
27
+ Args:
28
  inputs (str): The input text to be processed by the model (e.g., for summarization, translation, etc.)
29
 
30
  parameters (dict, optional): Additional generation parameters for the model, such as: ### Although it was not used in this program ###
 
103
  else:
104
  summarized = get_completion(text)
105
  # remember I don't have to define a summarization function because the default mode is summarization
106
+
107
+ if isinstance(summarized, list) and 'summary_text' in summarized[0]:
108
+ summarized = summarized[0]['summary_text']
109
+ else:
110
+ return {
111
+ "text": "⚠️ Unexpected response from summarize.",
112
+ "entities": []
113
+ }
114
+
115
+
116
  # let's perform ner on the summarized text
117
  output = ner(summarized)
118
  return output
119
+
120
 
121
 
122