cogniveon commited on
Commit
8bd15cf
1 Parent(s): 3841f0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -2
app.py CHANGED
@@ -1,4 +1,47 @@
1
  import gradio as gr
 
2
 
3
- demo = gr.load("cogniveon/nlpcw_bert-base-uncased-abbr", src="models")
4
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ pipe = pipeline("token-classification", model="cogniveon/nlpcw_bert-base-uncased-abbr", grouped_entities=True)
5
+
6
+ def predict(input) -> list[tuple[str, str | float | None]] | dict | None:
7
+ output = pipe(input)
8
+ entities = []
9
+
10
+ # Collect entities with their start and end positions
11
+ for entity in output:
12
+ entities.append({
13
+ "entity": entity["entity_group"],
14
+ "word": entity["word"],
15
+ "score": round(entity["score"], 4),
16
+ "start": entity["start"],
17
+ "end": entity["end"]
18
+ })
19
+
20
+ highlighted_text = [(input[:entities[0]['start']], None)] # Initial text before the first entity
21
+
22
+ # Generate highlighted text segments
23
+ for i, entity in enumerate(entities):
24
+ highlighted_text.append((input[entity['start']:entity['end']], entity['entity']))
25
+ if i < len(entities) - 1:
26
+ highlighted_text.append((input[entity['end']:entities[i+1]['start']], None))
27
+ else:
28
+ highlighted_text.append((input[entity['end']:], None)) # Remaining text after the last entity
29
+
30
+ return highlighted_text
31
+
32
+
33
+ demo = gr.Interface(
34
+ predict,
35
+ gr.Textbox(
36
+ label="Input",
37
+ lines=3,
38
+ ),
39
+ gr.HighlightedText(
40
+ label="Output",
41
+ combine_adjacent=True,
42
+ show_legend=True
43
+ ),
44
+ examples=[
45
+ ["We developed a variant of gene set enrichment analysis (GSEA) to determine whether a genetic pathway shows evidence for age regulation [23]."],
46
+ ],
47
+ ).launch(debug=True)