adilsiraju commited on
Commit
079da61
·
1 Parent(s): 3d89652

Fix sklearn dependency issue and improve error handling

Browse files

- Updated scikit-learn version in requirements.txt to >=1.0.0
- Added explicit sklearn import check with helpful error message
- Improved error handling with fallback mode functionality
- Updated interface description to show status when model fails to load
- Standardized naming to 'Medical Case Classifier' across all files

Files changed (3) hide show
  1. __pycache__/app.cpython-313.pyc +0 -0
  2. app.py +19 -2
  3. requirements.txt +1 -1
__pycache__/app.cpython-313.pyc CHANGED
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
 
app.py CHANGED
@@ -3,8 +3,18 @@ import torch
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import pickle
5
 
 
 
 
6
  # Load the saved model, tokenizer, and label encoder
7
  try:
 
 
 
 
 
 
 
8
  # Use the correct path where you saved your model
9
  model_path = "./medical_classifier_model"
10
 
@@ -29,7 +39,8 @@ try:
29
 
30
  except Exception as e:
31
  print(f"Error loading model components: {e}")
32
- # Fallback or exit if loading fails
 
33
  model, tokenizer, label_encoder, class_names = None, None, None, []
34
 
35
  def predict_medical_specialty(text):
@@ -85,6 +96,12 @@ examples = [
85
  num_classes = len(class_names) if class_names else 3
86
  include_examples = examples if all([model, tokenizer, label_encoder]) else None
87
 
 
 
 
 
 
 
88
  iface = gr.Interface(
89
  fn=predict_medical_specialty,
90
  inputs=gr.Textbox(
@@ -94,7 +111,7 @@ iface = gr.Interface(
94
  ),
95
  outputs=gr.Label(num_top_classes=num_classes),
96
  title="Medical Case Classifier",
97
- description="This application uses a fine-tuned Bio_ClinicalBERT model to predict the medical specialty of a given text.",
98
  examples=include_examples
99
  )
100
 
 
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import pickle
5
 
6
+ # Initialize variables
7
+ model, tokenizer, label_encoder, class_names = None, None, None, []
8
+
9
  # Load the saved model, tokenizer, and label encoder
10
  try:
11
+ # Check if sklearn is available
12
+ try:
13
+ import sklearn
14
+ print("scikit-learn is available")
15
+ except ImportError:
16
+ raise ImportError("scikit-learn is not installed. Please install it using: pip install scikit-learn")
17
+
18
  # Use the correct path where you saved your model
19
  model_path = "./medical_classifier_model"
20
 
 
39
 
40
  except Exception as e:
41
  print(f"Error loading model components: {e}")
42
+ print("The application will run in fallback mode with limited functionality.")
43
+ # Fallback values when loading fails
44
  model, tokenizer, label_encoder, class_names = None, None, None, []
45
 
46
  def predict_medical_specialty(text):
 
96
  num_classes = len(class_names) if class_names else 3
97
  include_examples = examples if all([model, tokenizer, label_encoder]) else None
98
 
99
+ # Create appropriate description based on model availability
100
+ if all([model, tokenizer, label_encoder]):
101
+ description_text = "This application uses a fine-tuned Bio_ClinicalBERT model to predict the medical specialty of a given text."
102
+ else:
103
+ description_text = "⚠️ Model loading failed (likely missing scikit-learn dependency). Please ensure all requirements are installed. The app is running in fallback mode."
104
+
105
  iface = gr.Interface(
106
  fn=predict_medical_specialty,
107
  inputs=gr.Textbox(
 
111
  ),
112
  outputs=gr.Label(num_top_classes=num_classes),
113
  title="Medical Case Classifier",
114
+ description=description_text,
115
  examples=include_examples
116
  )
117
 
requirements.txt CHANGED
@@ -2,4 +2,4 @@ transformers
2
  datasets
3
  torch
4
  gradio
5
- scikit-learn
 
2
  datasets
3
  torch
4
  gradio
5
+ scikit-learn>=1.0.0