DrSyedFaizan commited on
Commit
05948f9
Β·
verified Β·
1 Parent(s): be53738

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -41
app.py CHANGED
@@ -22,42 +22,25 @@ MODEL_PATHS = {
22
  "DistilBERT": "distilbert-base-uncased"
23
  }
24
 
25
- # Load Test Dataset (Example: Reddit Mental Health)
26
- test_texts = [
27
- "I feel so anxious and panicked all the time.",
28
- "I'm feeling absolutely wonderful today!",
29
- "I don't think I can go on anymore, I feel suicidal.",
30
- "Lately, I have mood swings that I can't explain.",
31
- "I feel so stressed out about everything."
32
- ]
33
- test_labels = [0, 3, 6, 1, 5] # Anxiety, Normal, Suicidal, Bipolar, Stress
34
 
35
- # Define column structure for leaderboard
36
- @dataclass
37
- class ModelEvalColumn:
38
- name: str
39
- type: str
40
- displayed_by_default: bool = True
41
- never_hidden: bool = False
42
- hidden: bool = False
43
 
44
- # Define the columns for your leaderboard
45
- fields = lambda cls: [
46
- ModelEvalColumn(name="model", type="str", never_hidden=True),
47
- ModelEvalColumn(name="model_type", type="str"),
48
- ModelEvalColumn(name="precision", type="str"),
49
- ModelEvalColumn(name="params", type="number"),
50
- ModelEvalColumn(name="accuracy", type="number"),
51
- ModelEvalColumn(name="f1_score", type="number"),
52
- ModelEvalColumn(name="inference_time", type="number"),
53
- ModelEvalColumn(name="license", type="str", displayed_by_default=False),
54
- ]
55
-
56
- # Function to evaluate models and format for leaderboard
57
- def evaluate_models():
58
  results = []
59
 
60
- # Model metadata (you would normally get this from model card or API)
61
  model_metadata = {
62
  "MindBERT": {"model_type": "BERT", "precision": "float16", "params": 0.11, "license": "MIT"},
63
  "BERT-base": {"model_type": "BERT", "precision": "float16", "params": 0.11, "license": "Apache-2.0"},
@@ -67,15 +50,12 @@ def evaluate_models():
67
 
68
  for model_name, model_path in MODEL_PATHS.items():
69
  print(f"Evaluating {model_name}...")
70
- # Load Tokenizer and Model
71
  tokenizer = AutoTokenizer.from_pretrained(model_path)
72
  model = AutoModelForSequenceClassification.from_pretrained(model_path)
73
  model.eval()
74
 
75
- # Tokenize Test Data
76
  inputs = tokenizer(test_texts, padding=True, truncation=True, return_tensors="pt")
77
 
78
- # Measure Inference Time
79
  start_time = time.time()
80
  with torch.no_grad():
81
  outputs = model(**inputs)
@@ -83,17 +63,15 @@ def evaluate_models():
83
  predictions = torch.argmax(logits, dim=1).numpy()
84
  end_time = time.time()
85
 
86
- # Compute Metrics
87
  accuracy = accuracy_score(test_labels, predictions)
88
  f1_score = f1_metric.compute(predictions=predictions, references=test_labels, average="macro")["f1"]
89
  inference_time = round(end_time - start_time, 4)
90
 
91
- # Store Results with additional metadata needed for leaderboard
92
  result = {
93
  "model": model_name,
94
  "model_type": model_metadata[model_name]["model_type"],
95
  "precision": model_metadata[model_name]["precision"],
96
- "params": model_metadata[model_name]["params"],
97
  "accuracy": round(accuracy, 4),
98
  "f1_score": round(f1_score, 4),
99
  "inference_time": inference_time,
@@ -101,9 +79,14 @@ def evaluate_models():
101
  }
102
  results.append(result)
103
 
104
- # Convert to DataFrame
105
- df_results = pd.DataFrame(results)
106
- return df_results
 
 
 
 
 
107
 
108
  # Initialize leaderboard with custom columns
109
  def init_leaderboard(dataframe):
 
22
  "DistilBERT": "distilbert-base-uncased"
23
  }
24
 
25
+ # Load Reddit Mental Health Dataset
26
+ def load_reddit_data(file_path):
27
+ df = pd.read_csv(file_path)
28
+ df = df.dropna(subset=["text", "label"]) # Ensure no missing values in relevant columns
29
+ return df
 
 
 
 
30
 
31
+ # Preprocess Dataset
32
+ def preprocess_data(df, sample_size=100):
33
+ df_sample = df.sample(n=sample_size, random_state=42) # Sample a subset
34
+ test_texts = df_sample["text"].tolist()
35
+ test_labels = df_sample["label"].tolist()
36
+ return test_texts, test_labels
 
 
37
 
38
+ # Function to evaluate models
39
+ def evaluate_models(dataset_path):
40
+ df = load_reddit_data(dataset_path)
41
+ test_texts, test_labels = preprocess_data(df)
 
 
 
 
 
 
 
 
 
 
42
  results = []
43
 
 
44
  model_metadata = {
45
  "MindBERT": {"model_type": "BERT", "precision": "float16", "params": 0.11, "license": "MIT"},
46
  "BERT-base": {"model_type": "BERT", "precision": "float16", "params": 0.11, "license": "Apache-2.0"},
 
50
 
51
  for model_name, model_path in MODEL_PATHS.items():
52
  print(f"Evaluating {model_name}...")
 
53
  tokenizer = AutoTokenizer.from_pretrained(model_path)
54
  model = AutoModelForSequenceClassification.from_pretrained(model_path)
55
  model.eval()
56
 
 
57
  inputs = tokenizer(test_texts, padding=True, truncation=True, return_tensors="pt")
58
 
 
59
  start_time = time.time()
60
  with torch.no_grad():
61
  outputs = model(**inputs)
 
63
  predictions = torch.argmax(logits, dim=1).numpy()
64
  end_time = time.time()
65
 
 
66
  accuracy = accuracy_score(test_labels, predictions)
67
  f1_score = f1_metric.compute(predictions=predictions, references=test_labels, average="macro")["f1"]
68
  inference_time = round(end_time - start_time, 4)
69
 
 
70
  result = {
71
  "model": model_name,
72
  "model_type": model_metadata[model_name]["model_type"],
73
  "precision": model_metadata[model_name]["precision"],
74
+ "params": model_metadata[model_name]["params"],
75
  "accuracy": round(accuracy, 4),
76
  "f1_score": round(f1_score, 4),
77
  "inference_time": inference_time,
 
79
  }
80
  results.append(result)
81
 
82
+ return pd.DataFrame(results)
83
+
84
+ # Load and evaluate
85
+ DATASET_PATH = "path/to/reddit_mental_health.csv"
86
+ df_results = evaluate_models(DATASET_PATH)
87
+
88
+ # Display results
89
+ df_results
90
 
91
  # Initialize leaderboard with custom columns
92
  def init_leaderboard(dataframe):