SenseiAlgo commited on
Commit
80d77cc
·
verified ·
1 Parent(s): 63d0755

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -11
app.py CHANGED
@@ -1,17 +1,21 @@
1
  """Demo for NER4OPT."""
2
  import os
3
  import warnings
4
-
5
- warnings.filterwarnings("ignore", category=DeprecationWarning)
6
-
7
  import streamlit as st
8
  import ner4opt
9
  from ner4opt.utils import preprocess, spacy_tokenize_sentence
10
-
11
  from spacy import displacy
12
 
 
 
13
  HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
14
 
 
 
 
 
 
 
15
 
16
  @st.cache_resource
17
  def load_models():
@@ -22,27 +26,41 @@ def load_models():
22
  hybrid_ner_model = ner4opt.Ner4Opt("hybrid")
23
  return lexical_ner_model, lexical_plus_ner_model, semantic_ner_model, hybrid_ner_model
24
 
 
 
 
25
 
26
  def main():
27
-
28
  st.title("""Ner4Opt: Named Entity Recognition for Optimization""")
29
-
30
  st.markdown("""Given an optimization problem in natural language, Ner4Opt extracts optimization related entities from free-form text. The source code for Ner4Opt is available at https://github.com/skadio/ner4opt""")
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  option = st.sidebar.selectbox(
33
  'Select a lexical, semantic, or hybrid model for extracting entities',
34
  ('Lexical', 'Lexical Plus', 'Semantic', 'Hybrid'), index=3)
35
 
36
  text = st.text_area(
37
  "Text",
38
- "Cautious Asset Investment has a total of $150,000 to manage and decides to invest it in money market fund, which yields a 2% return as well as in foreign bonds, which gives and average rate of return of 10.2%. Internal policies require PAI to diversify the asset allocation so that the minimum investment in money market fund is 40% of the total investment. Due to the risk of default of foreign countries, no more than 40% of the total investment should be allocated to foreign bonds. How much should the Cautious Asset Investment allocate in each asset so as to maximize its average return?"
39
  )
40
  text = text.strip()
41
  if text == "":
42
  st.write("Please write a valid sentence.")
43
 
44
- lexical_ner_model, lexical_plus_ner_model, semantic_ner_model, hybrid_ner_model = load_models(
45
- )
46
 
47
  # get entities
48
  if option == "Lexical":
@@ -74,6 +92,5 @@ def main():
74
  html_ner = html_ner.replace("\n", " ")
75
  st.write(HTML_WRAPPER.format(html_ner), unsafe_allow_html=True)
76
 
77
-
78
  if __name__ == '__main__':
79
- main()
 
1
  """Demo for NER4OPT."""
2
  import os
3
  import warnings
 
 
 
4
  import streamlit as st
5
  import ner4opt
6
  from ner4opt.utils import preprocess, spacy_tokenize_sentence
 
7
  from spacy import displacy
8
 
9
+ warnings.filterwarnings("ignore", category=DeprecationWarning)
10
+
11
  HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
12
 
13
+ # Define your example prompts here for easy management
14
+ EXAMPLE_PROMPTS = {
15
+ "Example 1️⃣: Asset Allocation": "Cautious Asset Investment has a total of $150,000 to manage and decides to invest it in money market fund, which yields a 2% return as well as in foreign bonds, which gives and average rate of return of 10.2%. Internal policies require PAI to diversify the asset allocation so that the minimum investment in money market fund is 40% of the total investment. Due to the risk of default of foreign countries, no more than 40% of the total investment should be allocated to foreign bonds. How much should the Cautious Asset Investment allocate in each asset so as to maximize its average return?",
16
+ "Example 2️⃣: Production Optimization": "A factory produces two types of goods, A and B. Producing one unit of good A requires 2 hours of labor and 1 unit of raw material. Producing one unit of good B requires 3 hours of labor and 2 units of raw material. The factory has a total of 100 labor hours and 50 units of raw material available. If the profit from selling one unit of good A is $10 and from good B is $15, how many units of each good should be produced to maximize the total profit?",
17
+ "Example 3️⃣: Transportation Problem": "A company has two warehouses and three retail stores. Warehouse 1 has 500 units of a product and Warehouse 2 has 700 units. Store 1 requires 300 units, Store 2 requires 400 units, and Store 3 requires 500 units. The cost of shipping one unit from Warehouse 1 to Store 1 is $2, to Store 2 is $3, and to Store 3 is $4. The cost of shipping from Warehouse 2 to Store 1 is $5, to Store 2 is $2, and to Store 3 is $3. How should the company ship the products to minimize the total shipping cost?"
18
+ }
19
 
20
  @st.cache_resource
21
  def load_models():
 
26
  hybrid_ner_model = ner4opt.Ner4Opt("hybrid")
27
  return lexical_ner_model, lexical_plus_ner_model, semantic_ner_model, hybrid_ner_model
28
 
29
+ def set_example_text(example_key):
30
+ """Callback to set the text area value based on the selected example."""
31
+ st.session_state.text_input = EXAMPLE_PROMPTS[example_key]
32
 
33
  def main():
 
34
  st.title("""Ner4Opt: Named Entity Recognition for Optimization""")
 
35
  st.markdown("""Given an optimization problem in natural language, Ner4Opt extracts optimization related entities from free-form text. The source code for Ner4Opt is available at https://github.com/skadio/ner4opt""")
36
 
37
+ # Initialize session state for the text input if it doesn't exist
38
+ if "text_input" not in st.session_state:
39
+ st.session_state.text_input = EXAMPLE_PROMPTS["Example 1️⃣: Asset Allocation"]
40
+
41
+ # --- Add the example prompts section ---
42
+ st.subheader("Try with an example:")
43
+ cols = st.columns(len(EXAMPLE_PROMPTS))
44
+ for i, (key, value) in enumerate(EXAMPLE_PROMPTS.items()):
45
+ with cols[i]:
46
+ if st.button(key):
47
+ set_example_text(key)
48
+
49
+ # ----------------------------------------
50
+
51
  option = st.sidebar.selectbox(
52
  'Select a lexical, semantic, or hybrid model for extracting entities',
53
  ('Lexical', 'Lexical Plus', 'Semantic', 'Hybrid'), index=3)
54
 
55
  text = st.text_area(
56
  "Text",
57
+ value=st.session_state.text_input, # Use the session state value here
58
  )
59
  text = text.strip()
60
  if text == "":
61
  st.write("Please write a valid sentence.")
62
 
63
+ lexical_ner_model, lexical_plus_ner_model, semantic_ner_model, hybrid_ner_model = load_models()
 
64
 
65
  # get entities
66
  if option == "Lexical":
 
92
  html_ner = html_ner.replace("\n", " ")
93
  st.write(HTML_WRAPPER.format(html_ner), unsafe_allow_html=True)
94
 
 
95
  if __name__ == '__main__':
96
+ main()