Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,10 @@ import plotly.graph_objects as go
|
|
18 |
from typing import Tuple
|
19 |
import plotly.io as pio
|
20 |
|
|
|
|
|
|
|
|
|
21 |
def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
|
22 |
hex_color = hex_color.lstrip('#')
|
23 |
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
@@ -147,19 +151,43 @@ def generate_charts(ner_output_bin: dict, ner_output_ext: dict) -> Tuple[go.Figu
|
|
147 |
paper_bgcolor='rgba(0,0,0,0)'
|
148 |
)
|
149 |
|
150 |
-
|
|
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
pie_chart, bar_chart = generate_charts(ner_output_bin, ner_output_ext)
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
examples = [
|
165 |
['Bevor ich meinen Hund kaufte bin ich immer alleine durch den Park gelaufen. Gestern war ich aber mit dem Hund losgelaufen. Das Wetter war sehr schön, nicht wie sonst im Winter. Ich weiß nicht genau. Mir fällt sonst nichts dazu ein. Wir trafen auf mehrere Spaziergänger. Ein Mann mit seinem Kind. Das Kind hat ein Eis gegessen.'],
|
@@ -191,7 +219,8 @@ iface = gr.Interface(
|
|
191 |
gr.Label(label="External Detail Count"),
|
192 |
gr.Label(label="Approximated Internal Detail Ratio"),
|
193 |
gr.Plot(label="Extended SeqClass Entity Distribution Pie Chart"),
|
194 |
-
gr.Plot(label="Binary SeqClass Entity Count Bar Chart")
|
|
|
195 |
],
|
196 |
title="Scoring Demo",
|
197 |
description="Autobiographical Memory Analysis: This demo combines two text - and two sequence classification models to showcase our automated Autobiographical Interview scoring method. Submit a narrative to see the results.",
|
@@ -199,4 +228,4 @@ iface = gr.Interface(
|
|
199 |
theme=monochrome
|
200 |
)
|
201 |
|
202 |
-
iface.launch()
|
|
|
18 |
from typing import Tuple
|
19 |
import plotly.io as pio
|
20 |
|
21 |
+
|
22 |
+
from wordcloud import WordCloud
|
23 |
+
import io
|
24 |
+
|
25 |
def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
|
26 |
hex_color = hex_color.lstrip('#')
|
27 |
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
|
|
151 |
paper_bgcolor='rgba(0,0,0,0)'
|
152 |
)
|
153 |
|
154 |
+
# Create word cloud for extended classification entities
|
155 |
+
wordcloud_fig = generate_wordcloud(ner_output_ext['entities'], 'tab10')
|
156 |
|
157 |
+
return fig1, fig2, wordcloud_fig
|
158 |
+
|
159 |
+
def generate_wordcloud(entities: list, color_map: dict) -> plt.Figure:
|
160 |
+
word_freq = {entity['entity']: entity['score'] for entity in entities}
|
161 |
+
wc = WordCloud(width=800, height=400, background_color='black', colormap=color_map).generate_from_frequencies(word_freq)
|
|
|
|
|
162 |
|
163 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
164 |
+
ax.imshow(wc, interpolation='bilinear')
|
165 |
+
ax.axis('off')
|
166 |
+
return fig
|
167 |
+
|
168 |
+
@spaces.GPU
|
169 |
+
def all(text: str) -> Tuple[gr.HighlightedText, gr.HighlightedText, int, int, float, go.Figure, go.Figure, plt.Figure]:
|
170 |
+
ner_output_bin = process_ner(text, binary_pipeline)
|
171 |
+
ner_output_ext = process_ner(text, extended_pipeline)
|
172 |
+
|
173 |
+
binary_entities = [{"entity": ent["entity"], "score": ent["score"]} for ent in ner_output_bin["entities"]]
|
174 |
+
extended_entities = [{"entity": ent["entity"], "score": ent["score"]} for ent in ner_output_ext["entities"]]
|
175 |
+
|
176 |
+
bin_text = [{"text": text[ent["start"]:ent["end"]], "entity": ent["entity"]} for ent in ner_output_bin["entities"]]
|
177 |
+
ext_text = [{"text": text[ent["start"]:ent["end"]], "entity": ent["entity"]} for ent in ner_output_ext["entities"]]
|
178 |
+
|
179 |
+
fig1, fig2, wordcloud_fig = generate_charts(ner_output_bin, ner_output_ext)
|
180 |
+
|
181 |
+
# Convert the word cloud figure to an image for Gradio
|
182 |
+
buf = io.BytesIO()
|
183 |
+
wordcloud_fig.savefig(buf, format='png')
|
184 |
+
buf.seek(0)
|
185 |
+
|
186 |
+
internal_count = sum(1 for ent in ner_output_bin["entities"] if ent["entity"] == "Internal")
|
187 |
+
external_count = sum(1 for ent in ner_output_bin["entities"] if ent["entity"] == "External")
|
188 |
+
internal_ratio = internal_count / (internal_count + external_count) if (internal_count + external_count) > 0 else 0
|
189 |
+
|
190 |
+
return bin_text, ext_text, internal_count, external_count, internal_ratio, fig1, fig2, buf
|
191 |
|
192 |
examples = [
|
193 |
['Bevor ich meinen Hund kaufte bin ich immer alleine durch den Park gelaufen. Gestern war ich aber mit dem Hund losgelaufen. Das Wetter war sehr schön, nicht wie sonst im Winter. Ich weiß nicht genau. Mir fällt sonst nichts dazu ein. Wir trafen auf mehrere Spaziergänger. Ein Mann mit seinem Kind. Das Kind hat ein Eis gegessen.'],
|
|
|
219 |
gr.Label(label="External Detail Count"),
|
220 |
gr.Label(label="Approximated Internal Detail Ratio"),
|
221 |
gr.Plot(label="Extended SeqClass Entity Distribution Pie Chart"),
|
222 |
+
gr.Plot(label="Binary SeqClass Entity Count Bar Chart"),
|
223 |
+
gr.Image(label="Word Cloud of Extended Entities")
|
224 |
],
|
225 |
title="Scoring Demo",
|
226 |
description="Autobiographical Memory Analysis: This demo combines two text - and two sequence classification models to showcase our automated Autobiographical Interview scoring method. Submit a narrative to see the results.",
|
|
|
228 |
theme=monochrome
|
229 |
)
|
230 |
|
231 |
+
iface.launch()
|