ituvtu commited on
Commit
a1fc8e5
·
1 Parent(s): 743501d

Update app

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -10,32 +10,38 @@ try:
10
  pipe = pipeline("text-classification", model=model_name)
11
  print(f"Model {model_name} has been successfully downloaded.")
12
  except Exception as e:
13
- print(f"Model loading error: {e}")
14
-
15
- pipe = lambda text, **kwargs: [{"label": f"ERROR: {e}", "score": 0.0}]
16
 
17
 
18
  def predict_news_category(text):
19
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- predictions = pipe(text, return_all_scores=True)
22
-
23
  formatted_output = {}
24
-
25
  try:
26
- # {"World": 0.1, "Sports": 0.7, ...}
27
  if predictions and predictions[0]:
28
  for pred in predictions[0]:
29
  label_num_str = pred['label']
30
  score = pred['score']
31
-
32
  label_index = int(label_num_str.split('_')[-1])
33
  readable_label = id2label.get(label_index, "Unknown")
34
-
35
  formatted_output[readable_label] = score
36
-
 
 
37
  except Exception as e:
38
- return {f"Помилка обробки: {e}": 0.0}
39
 
40
  return formatted_output
41
 
 
10
  pipe = pipeline("text-classification", model=model_name)
11
  print(f"Model {model_name} has been successfully downloaded.")
12
  except Exception as e:
13
+ model_load_error = str(e)
14
+ print(f"Model loading error: {model_load_error}")
 
15
 
16
 
17
  def predict_news_category(text):
18
 
19
+ if pipe is None:
20
+ return {f"Critical error: Model failed to load.": 0.0,
21
+ f"Error: {model_load_error}": 0.0}
22
+
23
+ if not text or text.strip() == "":
24
+ return {"Enter text for analysis": 0.0}
25
+
26
+ try:
27
+ predictions = pipe(text, return_all_scores=True)
28
+ except Exception as e:
29
+ return {f"Error during forecasting: {e}": 0.0}
30
 
 
 
31
  formatted_output = {}
 
32
  try:
 
33
  if predictions and predictions[0]:
34
  for pred in predictions[0]:
35
  label_num_str = pred['label']
36
  score = pred['score']
 
37
  label_index = int(label_num_str.split('_')[-1])
38
  readable_label = id2label.get(label_index, "Unknown")
 
39
  formatted_output[readable_label] = score
40
+ else:
41
+ return {"The model did not return a result.": 0.0}
42
+
43
  except Exception as e:
44
+ return {f"Output formatting error: {e}": 0.0}
45
 
46
  return formatted_output
47