DanielNRU commited on
Commit
3cc820b
·
verified ·
1 Parent(s): 7afe328

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -21,7 +21,6 @@ REL_LABELS = ["has_symptom", "has_medicine", "no_relation"]
21
  REL_ID2LABEL = {i: l for i, l in enumerate(REL_LABELS)}
22
  REL_LABEL2ID = {l: i for i, l in enumerate(REL_LABELS)}
23
 
24
-
25
  def load_models():
26
  ner_model = AutoModelForTokenClassification.from_pretrained(
27
  "DanielNRU/pollen-ner",
@@ -41,14 +40,11 @@ def load_models():
41
  re_model.eval()
42
  return ner_model, ner_tokenizer, re_model, re_tokenizer
43
 
44
-
45
  ner_model, ner_tokenizer, re_model, re_tokenizer = load_models()
46
 
47
-
48
  def split_sentences(text):
49
  return [s.strip() for s in re.split(r"[.!?]", text) if s.strip()]
50
 
51
-
52
  def predict_entities(text):
53
  inputs = ner_tokenizer(text, return_tensors="pt", return_offsets_mapping=True, truncation=True, max_length=512)
54
  offset_mapping = inputs.pop("offset_mapping")[0]
@@ -71,22 +67,28 @@ def predict_entities(text):
71
  else:
72
  if current:
73
  entities.append(current)
74
- current = None
75
  if current:
76
  entities.append(current)
77
  return entities
78
 
79
-
80
  def insert_entity_markers(text, ent1, ent2):
81
  if ent1["start"] < ent2["start"]:
82
  first, second = ent1, ent2
83
  else:
84
  first, second = ent2, ent1
85
- t = (text[:second["start"]] + f"[{second['label']}]" + text[second["start"]:second["end"]] + f"[/{second['label']}]" + text[second["end"]:])
86
- t = (t[:first["start"]] + f"[{first['label']}]" + t[first["start"]:first["end"]] + f"[/{first['label']}]" + t[first["end"]:])
 
 
 
 
 
 
 
 
87
  return t
88
 
89
-
90
  def predict_relations(text, entities):
91
  relations = []
92
  for sent in split_sentences(text):
@@ -108,7 +110,6 @@ def predict_relations(text, entities):
108
  relations.append({"head": ent1, "tail": ent2, "relation": rel})
109
  return relations
110
 
111
-
112
  def analyze_text(text):
113
  if not text or not text.strip():
114
  return [], "", "", "", ""
@@ -127,7 +128,6 @@ def analyze_text(text):
127
  symptoms = [f"{r['head']['text']} {r['tail']['text']}" for r in relations if r["relation"] == "has_symptom" and r["tail"]["label"] == "SYMPTOM"]
128
  return highlights, ", ".join(toponyms), ", ".join(medicines), ", ".join(symptoms), ", ".join(allergens)
129
 
130
-
131
  EXAMPLES = [
132
  ["В Московской области у меня началась аллергия на пыльцу березы, потекли глаза, нос, принимаю Зиртек и Назонекс."],
133
  ["У ребенка в Новокузнецке чешутся глаза, уши и течет нос, врач прописал Кромогексал, Назонекс в нос."],
@@ -150,5 +150,4 @@ with gr.Blocks() as demo:
150
  out_allergens = gr.Textbox(label="Аллергены")
151
  run_btn.click(fn=analyze_text, inputs=input_text, outputs=[out_highlight, out_toponyms, out_medicines, out_symptoms, out_allergens])
152
 
153
- # ✅ ОБЯЗАТЕЛЬНО для Gradio 5.x и 6.x на Spaces
154
- demo.launch(ssr_mode=False)
 
21
  REL_ID2LABEL = {i: l for i, l in enumerate(REL_LABELS)}
22
  REL_LABEL2ID = {l: i for i, l in enumerate(REL_LABELS)}
23
 
 
24
  def load_models():
25
  ner_model = AutoModelForTokenClassification.from_pretrained(
26
  "DanielNRU/pollen-ner",
 
40
  re_model.eval()
41
  return ner_model, ner_tokenizer, re_model, re_tokenizer
42
 
 
43
  ner_model, ner_tokenizer, re_model, re_tokenizer = load_models()
44
 
 
45
  def split_sentences(text):
46
  return [s.strip() for s in re.split(r"[.!?]", text) if s.strip()]
47
 
 
48
  def predict_entities(text):
49
  inputs = ner_tokenizer(text, return_tensors="pt", return_offsets_mapping=True, truncation=True, max_length=512)
50
  offset_mapping = inputs.pop("offset_mapping")[0]
 
67
  else:
68
  if current:
69
  entities.append(current)
70
+ current = None
71
  if current:
72
  entities.append(current)
73
  return entities
74
 
 
75
  def insert_entity_markers(text, ent1, ent2):
76
  if ent1["start"] < ent2["start"]:
77
  first, second = ent1, ent2
78
  else:
79
  first, second = ent2, ent1
80
+ t = (text[:second["start"]]
81
+ + f"[{second['label']}]"
82
+ + text[second["start"]:second["end"]]
83
+ + f"[/{second['label']}]"
84
+ + text[second["end"]:])
85
+ t = (t[:first["start"]]
86
+ + f"[{first['label']}]"
87
+ + t[first["start"]:first["end"]]
88
+ + f"[/{first['label']}]"
89
+ + t[first["end"]:])
90
  return t
91
 
 
92
  def predict_relations(text, entities):
93
  relations = []
94
  for sent in split_sentences(text):
 
110
  relations.append({"head": ent1, "tail": ent2, "relation": rel})
111
  return relations
112
 
 
113
  def analyze_text(text):
114
  if not text or not text.strip():
115
  return [], "", "", "", ""
 
128
  symptoms = [f"{r['head']['text']} {r['tail']['text']}" for r in relations if r["relation"] == "has_symptom" and r["tail"]["label"] == "SYMPTOM"]
129
  return highlights, ", ".join(toponyms), ", ".join(medicines), ", ".join(symptoms), ", ".join(allergens)
130
 
 
131
  EXAMPLES = [
132
  ["В Московской области у меня началась аллергия на пыльцу березы, потекли глаза, нос, принимаю Зиртек и Назонекс."],
133
  ["У ребенка в Новокузнецке чешутся глаза, уши и течет нос, врач прописал Кромогексал, Назонекс в нос."],
 
150
  out_allergens = gr.Textbox(label="Аллергены")
151
  run_btn.click(fn=analyze_text, inputs=input_text, outputs=[out_highlight, out_toponyms, out_medicines, out_symptoms, out_allergens])
152
 
153
+ demo.launch()