rhasan commited on
Commit
2d855ab
·
1 Parent(s): c031815

adding fig

Browse files
Files changed (2) hide show
  1. app.py +39 -14
  2. src/paired_texts_modelling.py +0 -2
app.py CHANGED
@@ -1,31 +1,43 @@
1
  import os
 
2
  import time
3
  import gradio as gr
 
4
  import numpy as np
 
5
  from huggingface_hub import hf_hub_download
6
 
7
  from src.infer import load_model, predict
8
 
9
- os.environ.setdefault("HF_HOME", "/data/.huggingface")
10
 
11
  _model = None
12
- _ckpt_path = None
13
 
14
  def _warmup():
15
- global _model, _ckpt_path
16
  if _model is not None:
17
  return
18
  t0 = time.time()
19
- _ckpt_path = hf_hub_download(
20
  repo_id="rhasan/UPLME",
21
  filename="UPLME_NewsEmp_tuned-lambdas.ckpt",
22
- repo_type="model",
23
- local_dir="/data/uplme_ckpt"
24
  )
25
- load_model(_ckpt_path)
26
  return f"Model loaded in {time.time() - t0:.1f} seconds."
27
 
28
- def predict_with_ci(essay: str, article: str) -> tuple[float, float, float]:
 
 
 
 
 
 
 
 
 
 
 
29
  _warmup()
30
  mean, var = predict(essay, article)
31
  # scores were originally in [1, 7]
@@ -35,21 +47,34 @@ def predict_with_ci(essay: str, article: str) -> tuple[float, float, float]:
35
  std = np.sqrt(var)
36
  ci_low = max(0.0, mean - 1.96 * std)
37
  ci_upp = min(100.0, mean + 1.96 * std)
38
- return mean, ci_low, ci_upp
 
 
39
 
40
- with gr.Blocks(title="Empathy Prediction") as demo:
41
  gr.Markdown("# Empathy Prediction with Uncertainty Estimation")
42
  with gr.Row():
43
  with gr.Column():
44
- essay_input = gr.Textbox(label="Essay", lines=10, placeholder="Enter the essay text here...")
45
- article_input = gr.Textbox(label="Article", lines=10, placeholder="Enter the article text here...")
46
  button = gr.Button("Predict")
47
  with gr.Column():
48
- output_mean = gr.Number(label="Predicted Empathy Mean", precision=2)
49
  ci_low = gr.Number(label="95% CI Lower Bound", precision=2)
50
  ci_upp = gr.Number(label="95% CI Upper Bound", precision=2)
 
 
 
 
51
 
52
- button.click(fn=predict_with_ci, inputs=[essay_input, article_input], outputs=[output_mean, ci_low, ci_upp])
 
 
 
 
 
 
 
53
 
54
  if __name__ == "__main__":
55
  demo.launch()
 
1
  import os
2
+ from pathlib import Path
3
  import time
4
  import gradio as gr
5
+ from gadio.themes import Soft
6
  import numpy as np
7
+ import matplotlib.pyplot as plt
8
  from huggingface_hub import hf_hub_download
9
 
10
  from src.infer import load_model, predict
11
 
12
+ os.environ.setdefault("HF_HOME", str(Path.home() / ".cache" / "huggingface"))
13
 
14
  _model = None
 
15
 
16
  def _warmup():
17
+ global _model
18
  if _model is not None:
19
  return
20
  t0 = time.time()
21
+ ckpt_path = hf_hub_download(
22
  repo_id="rhasan/UPLME",
23
  filename="UPLME_NewsEmp_tuned-lambdas.ckpt",
24
+ repo_type="model"
 
25
  )
26
+ load_model(ckpt_path)
27
  return f"Model loaded in {time.time() - t0:.1f} seconds."
28
 
29
+ def ci_plot(mean: float, low: float, upp: float):
30
+ fig, ax = plt.subplots(figsize=(6, 1))
31
+ ax.hlines(1, 0, 100, linewidht=2, alpha=0.15)
32
+ ax.hlines(1, low, upp, linewidht=6)
33
+ ax.plot([mean], [1], "o")
34
+ ax.set_xlim(0, 100)
35
+ ax.set_yticks([])
36
+ ax.set_xlabel("Empathy Score (0-100)")
37
+ fig.tight_layout()
38
+ return fig
39
+
40
+ def predict_with_ci(essay: str, article: str) -> tuple[float, float, float, plt.Figure]:
41
  _warmup()
42
  mean, var = predict(essay, article)
43
  # scores were originally in [1, 7]
 
47
  std = np.sqrt(var)
48
  ci_low = max(0.0, mean - 1.96 * std)
49
  ci_upp = min(100.0, mean + 1.96 * std)
50
+ fig = ci_plot(mean, ci_low, ci_upp)
51
+ return mean, ci_low, ci_upp, fig
52
+
53
 
54
+ with gr.Blocks(title="UPLME", theme=Soft(primary_hue="blue")) as demo:
55
  gr.Markdown("# Empathy Prediction with Uncertainty Estimation")
56
  with gr.Row():
57
  with gr.Column():
58
+ essay_input = gr.Textbox(label="Response (E.g., Essay)", lines=10, placeholder="Enter the essay text here...")
59
+ article_input = gr.Textbox(label="Stimulus (E.g., News Article)", lines=10, placeholder="Enter the article text here...")
60
  button = gr.Button("Predict")
61
  with gr.Column():
62
+ output_mean = gr.Number(label="Predicted Empathy Score", precision=2)
63
  ci_low = gr.Number(label="95% CI Lower Bound", precision=2)
64
  ci_upp = gr.Number(label="95% CI Upper Bound", precision=2)
65
+
66
+ fig = gr.Plot(label="Prediction +/- 95% CI")
67
+
68
+ button.click(fn=predict_with_ci, inputs=[essay_input, article_input], outputs=[output_mean, ci_low, ci_upp, fig])
69
 
70
+ gr.Markdown("## About")
71
+ gr.Markdown("""
72
+ This application predicts empathy score using the UPLME model proposed in **UPLME: Uncertainty-Aware Probabilistic Language Modelling for Robust Empathy Regression** by **Md Rakibul Hasan, Md Zakir Hossain, Aneesh Krishna, Shafin Rahman and Tom Gedeon**.
73
+ The model provides both a mean empathy score and uncertainty estimates.
74
+ - **Response**: The text input representing the individual's response (e.g., essay).
75
+ - **Stimulus**: The text input representing the stimulus (e.g. newspaper article) that the response is based on.
76
+ - **Predicted Empathy**: The predicted empathy score on a scale from 0 to 100.
77
+ """)
78
 
79
  if __name__ == "__main__":
80
  demo.launch()
src/paired_texts_modelling.py CHANGED
@@ -120,5 +120,3 @@ class LitPairedTextModel(L.LightningModule):
120
  for m in self.model.modules():
121
  if isinstance(m, torch.nn.Dropout):
122
  m.train()
123
-
124
-
 
120
  for m in self.model.modules():
121
  if isinstance(m, torch.nn.Dropout):
122
  m.train()