nlpblogs commited on
Commit
f448bd6
·
verified ·
1 Parent(s): 57deaa8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+ import pandas as pd
4
+ import io
5
+ import zipfile
6
+ from gliner import GLiNER
7
+ from streamlit_extras.stylable_container import stylable_container
8
+ import plotly.express as px
9
+ import os
10
+ from comet_ml import Experiment
11
+
12
+ COMET_API_KEY = os.environ.get("COMET_API_KEY")
13
+ COMET_WORKSPACE = os.environ.get("COMET_WORKSPACE")
14
+ COMET_PROJECT_NAME = os.environ.get("COMET_PROJECT_NAME")
15
+
16
+ if COMET_API_KEY and COMET_WORKSPACE and COMET_PROJECT_NAME:
17
+ comet_initialized = True
18
+ else:
19
+ comet_initialized = False
20
+ st.warning("Comet ML not initialized. Check environment variables.")
21
+
22
+ text = st.text_area("Type or paste your text below, and then press Ctrl + Enter", key='my_text_area')
23
+
24
+ if st.button("Results"):
25
+ with st.spinner("Wait for it...", show_time=True):
26
+ time.sleep(5)
27
+ model = GLiNER.from_pretrained("xomad/gliner-model-merge-large-v1.0")
28
+ labels = ["person", "country", "city", "organization", "date", "money", "percent value", "position"]
29
+ entities = model.predict_entities(text, labels)
30
+ df = pd.DataFrame(entities)
31
+
32
+ if comet_initialized:
33
+ experiment = Experiment(
34
+ api_key=COMET_API_KEY,
35
+ workspace=COMET_WORKSPACE,
36
+ project_name=COMET_PROJECT_NAME,
37
+ )
38
+ experiment.log_parameter("input_text", text)
39
+ experiment.log_table("predicted_entities", df)
40
+
41
+ properties = {"border": "2px solid gray", "color": "blue", "font-size": "16px"}
42
+ df_styled = df.style.set_properties(**properties)
43
+ st.dataframe(df_styled)
44
+
45
+ with st.expander("See Glossary of tags"):
46
+ st.write('''
47
+ '**text**': ['entity extracted from your text data']
48
+ '**score**': ['accuracy score; how accurately a tag has been assigned to a given entity']
49
+ '**label**': ['label (tag) assigned to a given extracted entity']
50
+ '**start**': ['index of the start of the corresponding entity']
51
+ '**end**': ['index of the end of the corresponding entity']
52
+ ''')
53
+
54
+ if df is not None:
55
+ fig = px.treemap(df, path=[px.Constant("all"), 'text', 'label'],
56
+ values='score', color='label')
57
+ fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
58
+ st.subheader("Tree map", divider = "red")
59
+ st.plotly_chart(fig)
60
+ if comet_initialized:
61
+ experiment.log_figure(figure=fig, figure_name="entity_treemap")
62
+
63
+ if df is not None:
64
+ value_counts1 = df['label'].value_counts()
65
+ df1 = pd.DataFrame(value_counts1)
66
+ final_df = df1.reset_index().rename(columns={"index": "label"})
67
+ col1, col2 = st.columns(2)
68
+ with col1:
69
+ fig1 = px.pie(final_df, values='count', names='label', hover_data=['count'], labels={'count': 'count'}, title='Percentage of predicted labels')
70
+ fig1.update_traces(textposition='inside', textinfo='percent+label')
71
+ st.subheader("Pie Chart", divider = "red")
72
+ st.plotly_chart(fig1)
73
+ if comet_initialized:
74
+ experiment.log_figure(figure=fig1, figure_name="label_pie_chart")
75
+ with col2:
76
+ fig2 = px.bar(final_df, x="count", y="label", color="label", text_auto=True, title='Occurrences of predicted labels')
77
+ st.subheader("Bar Chart", divider = "red")
78
+ st.plotly_chart(fig2)
79
+ if comet_initialized:
80
+ experiment.log_figure(figure=fig2, figure_name="label_bar_chart")
81
+
82
+ dfa = pd.DataFrame(
83
+ data={
84
+ 'text': ['entity extracted from your text data'], 'score': ['accuracy score; how accurately a tag has been assigned to a given entity'], 'label': ['label (tag) assigned to a given extracted entity'],
85
+ 'start': ['index of the start of the corresponding entity'],
86
+ 'end': ['index of the end of the corresponding entity'],
87
+ })
88
+ buf = io.BytesIO()
89
+ with zipfile.ZipFile(buf, "w") as myzip:
90
+ myzip.writestr("Summary of the results.csv", df.to_csv(index=False))
91
+ myzip.writestr("Glossary of tags.csv", dfa.to_csv(index=False))
92
+ if comet_initialized:
93
+ myzip.writestr("Summary of the results_glossary_combined.csv", pd.concat([df, dfa]).to_csv(index=False))
94
+
95
+ with stylable_container(
96
+ key="download_button",
97
+ css_styles="""button { background-color: yellow; border: 1px solid black; padding: 5px; color: black; }""",
98
+ ):
99
+ st.download_button(
100
+ label="Download zip file",
101
+ data=buf.getvalue(),
102
+ file_name="zip file.zip",
103
+ mime="application/zip",
104
+ )
105
+ if comet_initialized:
106
+ experiment.log_asset(buf.getvalue(), file_name="downloadable_results.zip")
107
+
108
+ st.divider()
109
+ if comet_initialized:
110
+ experiment.end()