Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
try:
|
| 2 |
-
import spaces
|
| 3 |
GPU = spaces.GPU
|
| 4 |
except ImportError:
|
| 5 |
-
GPU = lambda f: f
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
|
@@ -32,9 +32,9 @@ NAMES = {"WORKLOAD": "Sleep and workload", "TEAM": "Team dynamics",
|
|
| 32 |
"ADJUSTMENT": "Adjustment and coping", "OUTSIDE_COMMS": "Outside communication"}
|
| 33 |
|
| 34 |
|
| 35 |
-
#
|
| 36 |
|
| 37 |
-
#1. vader baseline: keyword lists + sentiment gate
|
| 38 |
KEYWORDS = {
|
| 39 |
"WORKLOAD": ["exhausted", "exhaustion", "tired", "fatigue", "overworked", "sleep", "schedule", "timeline",
|
| 40 |
"overtime", "demanding", "heavy", "restless", "shift", "tasks", "pressure"],
|
|
@@ -55,7 +55,7 @@ tf_models = joblib.load(os.path.join(HERE, "tfidf_logistic_models.pkl"))
|
|
| 55 |
STOP = set(stopwords.words("english"))
|
| 56 |
stemmer = SnowballStemmer("english")
|
| 57 |
|
| 58 |
-
#3. bert-base-uncased + my lora adapter
|
| 59 |
#thresholds tuned on the validation split in layer 7
|
| 60 |
BERT_THRESHOLDS = {"WORKLOAD": 0.65, "TEAM": 0.90, "ADJUSTMENT": 0.85, "OUTSIDE_COMMS": 0.90}
|
| 61 |
tok = AutoTokenizer.from_pretrained(HERE)
|
|
@@ -97,7 +97,7 @@ def bert_probs(segs):
|
|
| 97 |
return np.vstack(out)
|
| 98 |
|
| 99 |
|
| 100 |
-
#
|
| 101 |
|
| 102 |
#five cleaning rules in a fixed order
|
| 103 |
WRAPPED_RE = re.compile(r"\n([A-Z])\n([A-Z][A-Za-z\-]{1,30}:)")
|
|
@@ -194,7 +194,7 @@ def segment(text):
|
|
| 194 |
return [c for c in chunks if len(c.split()) >= MIN_WORDS]
|
| 195 |
|
| 196 |
|
| 197 |
-
#
|
| 198 |
|
| 199 |
def read_input(file, text):
|
| 200 |
if file is None:
|
|
@@ -288,7 +288,7 @@ def explain(state, choice, category):
|
|
| 288 |
f"<div class='seg' style='line-height:1.9'>{spans}</div>")
|
| 289 |
|
| 290 |
|
| 291 |
-
#
|
| 292 |
|
| 293 |
GOOD = """NASA ORAL HISTORY TRANSCRIPT (DEMO TEXT)
|
| 294 |
JANE DOE
|
|
@@ -325,7 +325,7 @@ TECHNICAL = ("We configured the solar array according to the procedural checklis
|
|
| 325 |
"prep at eighteen hundred hours. All telemetry values were nominal and the ground confirmed the readings.")
|
| 326 |
|
| 327 |
|
| 328 |
-
#
|
| 329 |
|
| 330 |
CSS = """
|
| 331 |
.gradio-container {background: radial-gradient(ellipse at top, #141b33 0%, #05070f 70%) !important;}
|
|
@@ -341,7 +341,7 @@ with gr.Blocks(title="SPACEMIND") as demo:
|
|
| 341 |
"the full pipeline, three models compared, and a SHAP explanation. Linaya Piyaratne (2401217)")
|
| 342 |
|
| 343 |
with gr.Row():
|
| 344 |
-
#
|
| 345 |
with gr.Column():
|
| 346 |
textInput = gr.Textbox(label="Paste a transcript or a passage", lines=10)
|
| 347 |
fileInput = gr.File(label="...or upload a .txt / .pdf transcript", file_types=[".txt", ".pdf"])
|
|
@@ -358,7 +358,7 @@ with gr.Blocks(title="SPACEMIND") as demo:
|
|
| 358 |
gr.Examples([[os.path.join(HERE, "ridesk-12-6-02.txt")]], inputs=fileInput,
|
| 359 |
label="Real transcript from my test split")
|
| 360 |
|
| 361 |
-
#
|
| 362 |
with gr.Column():
|
| 363 |
out1 = gr.Textbox(label="1. Input (word count)")
|
| 364 |
out2 = gr.Textbox(label="2. Cleaning (five rules, removed counts)")
|
|
|
|
| 1 |
try:
|
| 2 |
+
import spaces
|
| 3 |
GPU = spaces.GPU
|
| 4 |
except ImportError:
|
| 5 |
+
GPU = lambda f: f
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
|
|
|
| 32 |
"ADJUSTMENT": "Adjustment and coping", "OUTSIDE_COMMS": "Outside communication"}
|
| 33 |
|
| 34 |
|
| 35 |
+
#the three models
|
| 36 |
|
| 37 |
+
#1. vader baseline: keyword lists + sentiment gate
|
| 38 |
KEYWORDS = {
|
| 39 |
"WORKLOAD": ["exhausted", "exhaustion", "tired", "fatigue", "overworked", "sleep", "schedule", "timeline",
|
| 40 |
"overtime", "demanding", "heavy", "restless", "shift", "tasks", "pressure"],
|
|
|
|
| 55 |
STOP = set(stopwords.words("english"))
|
| 56 |
stemmer = SnowballStemmer("english")
|
| 57 |
|
| 58 |
+
#3. bert-base-uncased + my lora adapter
|
| 59 |
#thresholds tuned on the validation split in layer 7
|
| 60 |
BERT_THRESHOLDS = {"WORKLOAD": 0.65, "TEAM": 0.90, "ADJUSTMENT": 0.85, "OUTSIDE_COMMS": 0.90}
|
| 61 |
tok = AutoTokenizer.from_pretrained(HERE)
|
|
|
|
| 97 |
return np.vstack(out)
|
| 98 |
|
| 99 |
|
| 100 |
+
# he text pipeline
|
| 101 |
|
| 102 |
#five cleaning rules in a fixed order
|
| 103 |
WRAPPED_RE = re.compile(r"\n([A-Z])\n([A-Z][A-Za-z\-]{1,30}:)")
|
|
|
|
| 194 |
return [c for c in chunks if len(c.split()) >= MIN_WORDS]
|
| 195 |
|
| 196 |
|
| 197 |
+
# running everything
|
| 198 |
|
| 199 |
def read_input(file, text):
|
| 200 |
if file is None:
|
|
|
|
| 288 |
f"<div class='seg' style='line-height:1.9'>{spans}</div>")
|
| 289 |
|
| 290 |
|
| 291 |
+
# examples
|
| 292 |
|
| 293 |
GOOD = """NASA ORAL HISTORY TRANSCRIPT (DEMO TEXT)
|
| 294 |
JANE DOE
|
|
|
|
| 325 |
"prep at eighteen hundred hours. All telemetry values were nominal and the ground confirmed the readings.")
|
| 326 |
|
| 327 |
|
| 328 |
+
# HF page
|
| 329 |
|
| 330 |
CSS = """
|
| 331 |
.gradio-container {background: radial-gradient(ellipse at top, #141b33 0%, #05070f 70%) !important;}
|
|
|
|
| 341 |
"the full pipeline, three models compared, and a SHAP explanation. Linaya Piyaratne (2401217)")
|
| 342 |
|
| 343 |
with gr.Row():
|
| 344 |
+
#inputs
|
| 345 |
with gr.Column():
|
| 346 |
textInput = gr.Textbox(label="Paste a transcript or a passage", lines=10)
|
| 347 |
fileInput = gr.File(label="...or upload a .txt / .pdf transcript", file_types=[".txt", ".pdf"])
|
|
|
|
| 358 |
gr.Examples([[os.path.join(HERE, "ridesk-12-6-02.txt")]], inputs=fileInput,
|
| 359 |
label="Real transcript from my test split")
|
| 360 |
|
| 361 |
+
#the stages
|
| 362 |
with gr.Column():
|
| 363 |
out1 = gr.Textbox(label="1. Input (word count)")
|
| 364 |
out2 = gr.Textbox(label="2. Cleaning (five rules, removed counts)")
|