kargaranamir commited on
Commit
595caef
·
1 Parent(s): 3c733b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -52
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import streamlit as st
2
- import torch
3
  from huggingface_hub import hf_hub_download
4
  from utils import *
5
 
 
6
  HengamTransW = hf_hub_download(repo_id="kargaranamir/Hengam", filename="HengamTransW.pth")
7
  HengamTransA = hf_hub_download(repo_id="kargaranamir/Hengam", filename="HengamTransA.pth")
8
 
9
- ner_W = NER(model_path=HengamTransW, tags=['B-TIM', 'I-TIM', 'B-DAT', 'I-DAT', 'O'])
10
- ner_A = NER(model_path=HengamTransA, tags=['B-TIM', 'I-TIM', 'B-DAT', 'I-DAT', 'O'])
11
-
 
12
 
13
  # APP
14
  st.set_page_config(
@@ -16,71 +17,63 @@ st.set_page_config(
16
  page_icon="🕒",
17
  )
18
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- def _max_width_():
21
- max_width_str = f"max-width: 1400px;"
22
- st.markdown(
23
- f"""
24
- <style>
25
- .reportview-container .main .block-container{{
26
- {max_width_str}
27
- }}
28
- </style>
29
- """,
30
- unsafe_allow_html=True,
31
- )
32
-
33
-
34
- _max_width_()
35
-
36
- c30, c31, c32 = st.columns([2.5, 1, 3])
37
-
38
- with c30:
39
- st.title("🕒 Hengam")
40
- st.header("")
41
-
42
  with st.expander("ℹ️ - About this app", expanded=True):
43
  st.write(
44
  """
45
- - Online Demo for Hengam: An Adversarially Trained Transformer for Persian Temporal Tagging [Code](https://github.com/kargaranamir/hengam)!
46
- - This paper introduces Hengam, an adversarially trained transformer for Persian temporal tagging outperforming state-of-the-art approaches on a diverse and manually created dataset.
47
- """
48
  )
49
  st.markdown("")
50
 
51
  st.markdown("")
52
- st.markdown("## **📌 Paste any persian (farsi) text you want to extract its temporal markers.**")
53
  with st.form(key="my_form"):
54
- ce, c1, ce, c2, c3 = st.columns([0.05, 1.5, 0.05, 4, 0.05])
 
55
  with c1:
56
- ModelType = st.radio(
 
 
 
 
 
 
 
57
  "Choose your model",
58
- ["HengamTransW.pth", "HengamTransA.pth"],
59
- help="At present, you can choose between 2 models (HengamTransW or HengamTransA) to exrtact temporal markers. More to come!",
 
60
  )
61
-
62
- if ModelType == 'HengamTransW.pth':
63
- ner = ner_W
64
- else:
65
- ner = ner_A
66
 
67
  with c2:
68
  doc = st.text_area(
69
  "Paste your text below",
70
- '''ساعت ۸ صبح من و علی قرار گذاشتیم که شانزده بهمن ۱۳۷۵ هم دیگر را در دوشنبه بازار ببینیم.''',
71
- height=110,
72
  )
73
 
74
  submit_button = st.form_submit_button(label="✨ Extract Temporal Markers!")
75
 
76
- if not submit_button:
77
- st.stop()
78
-
79
- result = "\n".join([str(t) for t in ner(doc)])
80
-
81
- st.markdown("## **🎈Check results**")
82
-
83
- c1, c2, c3 = st.columns([1, 3, 1])
84
-
85
- with c2:
86
- st.write(result)
 
1
  import streamlit as st
 
2
  from huggingface_hub import hf_hub_download
3
  from utils import *
4
 
5
+ # Download the models
6
  HengamTransW = hf_hub_download(repo_id="kargaranamir/Hengam", filename="HengamTransW.pth")
7
  HengamTransA = hf_hub_download(repo_id="kargaranamir/Hengam", filename="HengamTransA.pth")
8
 
9
+ # Load models lazily
10
+ @st.cache(allow_output_mutation=True)
11
+ def load_ner_model(model_path):
12
+ return NER(model_path=model_path, tags=['B-TIM', 'I-TIM', 'B-DAT', 'I-DAT', 'O'])
13
 
14
  # APP
15
  st.set_page_config(
 
17
  page_icon="🕒",
18
  )
19
 
20
+ # Layout adjustments
21
+ st.markdown(
22
+ """
23
+ <style>
24
+ .reportview-container .main .block-container {
25
+ max-width: 1400px;
26
+ }
27
+ </style>
28
+ """,
29
+ unsafe_allow_html=True,
30
+ )
31
 
32
+ st.markdown("## **🕒 Hengam**")
33
+ st.write("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  with st.expander("ℹ️ - About this app", expanded=True):
35
  st.write(
36
  """
37
+ - Online Demo for Hengam: An Adversarially Trained Transformer for Persian Temporal Tagging [Code](https://github.com/kargaranamir/hengam)!
38
+ - This paper introduces Hengam, an adversarially trained transformer for Persian temporal tagging outperforming state-of-the-art approaches on a diverse and manually created dataset.
39
+ """
40
  )
41
  st.markdown("")
42
 
43
  st.markdown("")
44
+ st.markdown("## **📌 Paste any Persian (Farsi) text you want to extract its temporal markers.**")
45
  with st.form(key="my_form"):
46
+ c1, c2 = st.columns([2, 3])
47
+
48
  with c1:
49
+ st.write("Please wait while loading the model...")
50
+ model_paths = {
51
+ "HengamTransW.pth": "HengamTransW.pth",
52
+ "HengamTransA.pth": "HengamTransA.pth",
53
+ }
54
+
55
+ default_model = "HengamTransA.pth" # Set the default model
56
+ ModelType = st.selectbox(
57
  "Choose your model",
58
+ list(model_paths.keys()),
59
+ index=list(model_paths.keys()).index(default_model), # Select the default model
60
+ help="At present, you can choose between 2 models (HengamTransW or HengamTransA) to extract temporal markers. More to come!",
61
  )
62
+
63
+ ner = load_ner_model(model_paths[ModelType])
64
+ st.empty() # Clear the "Please wait" message
 
 
65
 
66
  with c2:
67
  doc = st.text_area(
68
  "Paste your text below",
69
+ "Example: ساعت ۸ صبح من و علی قرار گذاشتیم که به دوشنبه بازار بریم ...",
70
+ height=80,
71
  )
72
 
73
  submit_button = st.form_submit_button(label="✨ Extract Temporal Markers!")
74
 
75
+ if submit_button:
76
+ result = extract_temporal_markers(doc, ner)
77
+ st.write("") # Add vertical spacing
78
+ st.markdown("## **🎈Check results**")
79
+ st.code(result, height=300) # Adjust the height here