ELHACHYMI commited on
Commit
f28f387
·
verified ·
1 Parent(s): dd7aec2

Fin app file

Browse files
Files changed (1) hide show
  1. app.py +16 -65
app.py CHANGED
@@ -17,7 +17,7 @@ example_list = [
17
  "Apple Inc. was created by Steve Jobs in Cupertino.",
18
  ]
19
 
20
- # Correct mapping from LABEL_X to entity names
21
  ENTITY_MAP = {
22
  "LABEL_0": "O",
23
  "LABEL_1": "PER",
@@ -30,14 +30,7 @@ ENTITY_MAP = {
30
  "LABEL_8": "MISC",
31
  }
32
 
33
- # Colors for HTML rendering
34
- COLOR_MAP = {
35
- "PER": "#ff6666", # Red
36
- "ORG": "#66b3ff", # Blue
37
- "LOC": "#99ff99", # Green
38
- "MISC": "#ffcc66", # Orange
39
- }
40
-
41
  model_info = {
42
  model_name: {
43
  "link": f"https://huggingface.co/{model_name}",
@@ -49,6 +42,7 @@ print(ner(text))"""
49
  }
50
  }
51
 
 
52
  ner_pipeline = pipeline("ner", model=model_name, aggregation_strategy="simple")
53
 
54
 
@@ -59,7 +53,7 @@ ner_pipeline = pipeline("ner", model=model_name, aggregation_strategy="simple")
59
  def display_model_info(model_name):
60
  info = model_info[model_name]
61
  usage_code = info["usage"]
62
- model_link = f'[Open model page: {model_name}]({info["link"]})'
63
  return usage_code, model_link
64
 
65
 
@@ -89,38 +83,6 @@ def analyze_highlight(text):
89
  return highlighted
90
 
91
 
92
- # --------------------------------------
93
- # HTML COLOR MODE FUNCTION
94
- # --------------------------------------
95
-
96
- def colorize_text(text):
97
- results = ner_pipeline(text)
98
- html = ""
99
- last = 0
100
-
101
- for ent in results:
102
- start, end = ent["start"], ent["end"]
103
- raw_label = ent["entity_group"]
104
- label = ENTITY_MAP.get(raw_label, raw_label)
105
-
106
- if start > last:
107
- html += text[last:start]
108
-
109
- color = COLOR_MAP.get(label, "#dddddd")
110
- html += (
111
- f"<span style='background-color:{color}; padding:2px; border-radius:3px;'>"
112
- f"{text[start:end]} ({label})"
113
- f"</span>"
114
- )
115
-
116
- last = end
117
-
118
- if last < len(text):
119
- html += text[last:]
120
-
121
- return html
122
-
123
-
124
  # --------------------------------------
125
  # GRADIO UI
126
  # --------------------------------------
@@ -136,47 +98,36 @@ with gr.Blocks() as demo:
136
  placeholder="Type a sentence to analyze."
137
  )
138
 
 
139
  with gr.Row():
140
  for ex in example_list:
141
  gr.Button(ex).click(lambda x=ex: x, outputs=text_input)
142
 
143
- mode_selector = gr.Dropdown(
144
- choices=["Highlighted Mode", "Colorized HTML Mode"],
145
- value="Highlighted Mode",
146
- label="Output Display Mode"
147
- )
148
-
149
  analyze_button = gr.Button("Run NER Analysis")
150
 
 
151
  output_highlight = gr.HighlightedText(
152
- label="NER Result (Highlighted Mode)",
153
- combine_adjacent=True,
154
- visible=True
155
- )
156
- output_html = gr.HTML(
157
- label="NER Result (Colorized HTML Mode)",
158
- visible=False
159
  )
160
 
 
161
  code_output = gr.Code(label="How to use this model")
162
  link_output = gr.Markdown()
163
 
 
164
  demo.load(
165
  fn=display_model_info,
166
  inputs=[gr.Textbox(value=model_name, visible=False)],
167
  outputs=[code_output, link_output]
168
  )
169
 
170
- def run_analysis(text, mode):
171
- if mode == "Highlighted Mode":
172
- return analyze_highlight(text), gr.update(visible=True), gr.update(visible=False)
173
- else:
174
- return gr.update(), gr.update(visible=False), colorize_text(text)
175
-
176
  analyze_button.click(
177
- run_analysis,
178
- inputs=[text_input, mode_selector],
179
- outputs=[output_highlight, output_highlight, output_html]
180
  )
181
 
182
- demo.launch()
 
17
  "Apple Inc. was created by Steve Jobs in Cupertino.",
18
  ]
19
 
20
+ # Correct mapping from LABEL_X to real entity names
21
  ENTITY_MAP = {
22
  "LABEL_0": "O",
23
  "LABEL_1": "PER",
 
30
  "LABEL_8": "MISC",
31
  }
32
 
33
+ # Model info for README block
 
 
 
 
 
 
 
34
  model_info = {
35
  model_name: {
36
  "link": f"https://huggingface.co/{model_name}",
 
42
  }
43
  }
44
 
45
+ # Load NER pipeline
46
  ner_pipeline = pipeline("ner", model=model_name, aggregation_strategy="simple")
47
 
48
 
 
53
  def display_model_info(model_name):
54
  info = model_info[model_name]
55
  usage_code = info["usage"]
56
+ model_link = f"[Open model page: {model_name}]({info['link']})"
57
  return usage_code, model_link
58
 
59
 
 
83
  return highlighted
84
 
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  # --------------------------------------
87
  # GRADIO UI
88
  # --------------------------------------
 
98
  placeholder="Type a sentence to analyze."
99
  )
100
 
101
+ # Example buttons
102
  with gr.Row():
103
  for ex in example_list:
104
  gr.Button(ex).click(lambda x=ex: x, outputs=text_input)
105
 
106
+ # Run button
 
 
 
 
 
107
  analyze_button = gr.Button("Run NER Analysis")
108
 
109
+ # Only one output: HighlightedText
110
  output_highlight = gr.HighlightedText(
111
+ label="NER Result",
112
+ combine_adjacent=True
 
 
 
 
 
113
  )
114
 
115
+ # Code + link
116
  code_output = gr.Code(label="How to use this model")
117
  link_output = gr.Markdown()
118
 
119
+ # Load model info on startup
120
  demo.load(
121
  fn=display_model_info,
122
  inputs=[gr.Textbox(value=model_name, visible=False)],
123
  outputs=[code_output, link_output]
124
  )
125
 
126
+ # Run analysis
 
 
 
 
 
127
  analyze_button.click(
128
+ analyze_highlight,
129
+ inputs=text_input,
130
+ outputs=output_highlight
131
  )
132
 
133
+ demo.launch(ssr_mode=False)