enricorampazzo commited on
Commit
aa2cc5f
1 Parent(s): 54af9e3

now saving personal, location and contractor details in the browser local storage

Browse files
app.py CHANGED
@@ -4,19 +4,19 @@ import streamlit as st
4
  from streamlit import session_state as ss
5
  from streamlit_local_storage import LocalStorage
6
 
7
- from form.form import build_form_data_from_answers, write_pdf_form
8
  from llm_manager.llm_parser import LlmParser
9
  from local_storage.entities import PersonalDetails, LocationDetails, ContractorDetails
10
  from prompts.prompts_manager import PromptsManager, Questions as Q
11
  from repository.repository import build_repo_from_environment, get_repository
12
- from repository.repository_abc import Model, ModelRoles
13
  from utils.parsing_utils import check_for_missing_answers
14
 
15
  user_msg = "Please describe what you need to do. To get the best results try to answer all the following questions:"
16
  ls: LocalStorage = LocalStorage()
17
  class UIManager:
18
  def __init__(self):
19
- self.pm: PromptsManager = PromptsManager()
20
  self.repository = (build_repo_from_environment(self.pm.system_prompt) or
21
  get_repository("testing",
22
  Model("fakeModel", ModelRoles("a", "b", "c"))))
@@ -78,7 +78,7 @@ class UIManager:
78
  def build_ui_for_check_category(self):
79
  self._build_base_ui()
80
  with st.status("finding the work categories applicable to your work"):
81
- answer = self.repository.send_prompt(self.pm.get_work_category(ss["answers"][1]))
82
  categories = LlmParser.parse_get_categories_answer(answer['content'])
83
 
84
  with st.status("categories found, creating PDF form"):
@@ -105,27 +105,28 @@ class UIManager:
105
  st.rerun()
106
 
107
  def build_ui_for_parsing_error(self):
108
- def build_form_fragment(form_, col, title, *questions):
109
  form_.text(title)
110
  for user_data in questions:
111
  with col:
112
  form_.text_input(self.pm.questions_to_field_labels()[user_data], value=ss.get("answers", {})
113
  .get(user_data), key=f"fq_{user_data.name}")
114
- with col:
115
- form_.text_input("Save as", key=title.replace(" ", "_"))
 
116
 
117
  self._build_base_ui()
118
  f = st.form("Please check the following information and correct fix any inaccuracies")
119
  col1, col2 = f.columns(2)
120
- build_form_fragment(f, col1, "your details", Q.FULL_NAME, Q.CONTACT_NUMBER, Q.YOUR_EMAIL)
121
- build_form_fragment(f, col2, "work details", Q.WORK_TO_DO, Q.START_DATE, Q.END_DATE)
122
- build_form_fragment(f, col1, "location details", Q.COMMUNITY, Q.BUILDING, Q.UNIT_APT_NUMBER,
123
  Q.OWNER_OR_TENANT)
124
- build_form_fragment(f, col2, "contractor details", Q.COMPANY_NAME, Q.COMPANY_NUMBER, Q.COMPANY_EMAIL)
125
  submit_data = f.form_submit_button()
126
  if submit_data:
127
  for i in range(len(Q)):
128
- ss["answers"][Q(i).name] = ss[f"fq_{Q(i).name}"]
129
 
130
  for details_key, func in [("your_details", self._get_personal_details),
131
  ("location_details", self._get_location_details),
@@ -133,7 +134,9 @@ class UIManager:
133
  details = func(details_key)
134
  if details:
135
  key = ss[details_key] # get the name under which this data should be saved
136
- ls.setItem(key, json.dumps(details.__dict__))
 
 
137
 
138
  @staticmethod
139
  def _get_personal_details(personal_details_key) -> PersonalDetails | None:
@@ -151,8 +154,8 @@ class UIManager:
151
  @staticmethod
152
  def _get_contractor_details(contractor_details_key) -> ContractorDetails | None:
153
  if ss.get(contractor_details_key):
154
- return ContractorDetails(ss[f"fq_{Q.COMPANY_NAME}"], ss[f"fq_{Q.COMPANY_NUMBER}"],
155
- ss[f"fq_{Q.COMPANY_EMAIL}"])
156
  return None
157
 
158
 
@@ -175,7 +178,3 @@ def use_streamlit():
175
 
176
 
177
  use_streamlit()
178
-
179
-
180
-
181
-
 
4
  from streamlit import session_state as ss
5
  from streamlit_local_storage import LocalStorage
6
 
7
+ from form.form import build_form_data_from_answers, write_pdf_form, work_categories
8
  from llm_manager.llm_parser import LlmParser
9
  from local_storage.entities import PersonalDetails, LocationDetails, ContractorDetails
10
  from prompts.prompts_manager import PromptsManager, Questions as Q
11
  from repository.repository import build_repo_from_environment, get_repository
12
+ from repository import ModelRoles, Model
13
  from utils.parsing_utils import check_for_missing_answers
14
 
15
  user_msg = "Please describe what you need to do. To get the best results try to answer all the following questions:"
16
  ls: LocalStorage = LocalStorage()
17
  class UIManager:
18
  def __init__(self):
19
+ self.pm: PromptsManager = PromptsManager(work_categories=work_categories)
20
  self.repository = (build_repo_from_environment(self.pm.system_prompt) or
21
  get_repository("testing",
22
  Model("fakeModel", ModelRoles("a", "b", "c"))))
 
78
  def build_ui_for_check_category(self):
79
  self._build_base_ui()
80
  with st.status("finding the work categories applicable to your work"):
81
+ answer = self.repository.send_prompt(self.pm.get_work_category(ss["answers"][Q.WORK_TO_DO]))
82
  categories = LlmParser.parse_get_categories_answer(answer['content'])
83
 
84
  with st.status("categories found, creating PDF form"):
 
105
  st.rerun()
106
 
107
  def build_ui_for_parsing_error(self):
108
+ def build_form_fragment(form_, col, title, add_save, *questions):
109
  form_.text(title)
110
  for user_data in questions:
111
  with col:
112
  form_.text_input(self.pm.questions_to_field_labels()[user_data], value=ss.get("answers", {})
113
  .get(user_data), key=f"fq_{user_data.name}")
114
+ if add_save:
115
+ with col:
116
+ form_.text_input("Save as", key=title.replace(" ", "_"))
117
 
118
  self._build_base_ui()
119
  f = st.form("Please check the following information and correct fix any inaccuracies")
120
  col1, col2 = f.columns(2)
121
+ build_form_fragment(f, col1, "your details", True, Q.FULL_NAME, Q.CONTACT_NUMBER, Q.YOUR_EMAIL)
122
+ build_form_fragment(f, col2, "work details", False, Q.WORK_TO_DO, Q.START_DATE, Q.END_DATE)
123
+ build_form_fragment(f, col1, "location details", True, Q.COMMUNITY, Q.BUILDING, Q.UNIT_APT_NUMBER,
124
  Q.OWNER_OR_TENANT)
125
+ build_form_fragment(f, col2, "contractor details", True, Q.COMPANY_NAME, Q.COMPANY_NUMBER, Q.COMPANY_EMAIL)
126
  submit_data = f.form_submit_button()
127
  if submit_data:
128
  for i in range(len(Q)):
129
+ ss["answers"][Q(i)] = ss[f"fq_{Q(i).name}"]
130
 
131
  for details_key, func in [("your_details", self._get_personal_details),
132
  ("location_details", self._get_location_details),
 
134
  details = func(details_key)
135
  if details:
136
  key = ss[details_key] # get the name under which this data should be saved
137
+ ls.setItem(key, json.dumps(details.__dict__), key)
138
+ ss["step"] = "check_category"
139
+ st.rerun()
140
 
141
  @staticmethod
142
  def _get_personal_details(personal_details_key) -> PersonalDetails | None:
 
154
  @staticmethod
155
  def _get_contractor_details(contractor_details_key) -> ContractorDetails | None:
156
  if ss.get(contractor_details_key):
157
+ return ContractorDetails(ss[f"fq_{Q.COMPANY_NAME.name}"], ss[f"fq_{Q.COMPANY_NUMBER.name}"],
158
+ ss[f"fq_{Q.COMPANY_EMAIL.name}"])
159
  return None
160
 
161
 
 
178
 
179
 
180
  use_streamlit()
 
 
 
 
form/form.py CHANGED
@@ -1,36 +1,36 @@
1
- from io import BytesIO
2
  from pathlib import Path
3
  from typing import TypedDict, Tuple
4
  from PyPDFForm import PdfWrapper
5
 
 
6
  from utils.date_utils import get_today_date_as_dd_mm_yyyy
7
  from utils.parsing_utils import find_and_parse_date, find_and_parse_phone_number
8
 
9
 
10
- def build_form_data_from_answers(answers: dict[int, str], categories: list[str], signature: str | None = None) -> (
11
  Tuple)[dict[str, str], str]:
12
  form_data = {}
13
  for category in categories:
14
  form_data[form_fields_map[category]] = True
15
  form_data[form_fields_map["minor_work"]] = True
16
- form_data[form_fields_map["signed_by"]] = answers[0]
17
- form_data[form_fields_map["community"]] = answers[2]
18
- form_data[form_fields_map["building_name"]] = answers[3]
19
- form_data[form_fields_map["unit_no"]] = answers[4]
20
- form_data[form_fields_map["owner" if "owner" in answers[5].lower() else "tenant"]] = True
21
- form_data[form_fields_map["start_date"]] = find_and_parse_date(answers[6])
22
- form_data[form_fields_map["end_date"]] = find_and_parse_date(answers[7])
23
- form_data[form_fields_map["contact_number"]] = find_and_parse_phone_number(answers[8])
24
- form_data[form_fields_map["contractor_name"]] = answers[9]
25
- form_data[form_fields_map["contractor_contact_number"]] = answers[10]
26
- form_data[form_fields_map["contractor_email"]] = answers[11]
27
- form_data[form_fields_map["email"]] = answers[12]
28
- form_data[form_fields_map["occupant_name"]] = answers[0]
29
  form_data[form_fields_map["signature_date"]] = get_today_date_as_dd_mm_yyyy()
30
  if signature:
31
  form_data[form_fields_map["signature"]] = signature
32
  return form_data, (f"work_permit_request"
33
- f"_{'-'.join(categories)}_{answers[9]}_"
34
  f"{form_data[form_fields_map['start_date']].replace('/','-')}.pdf")
35
 
36
 
 
 
1
  from pathlib import Path
2
  from typing import TypedDict, Tuple
3
  from PyPDFForm import PdfWrapper
4
 
5
+ from prompts.prompts_manager import Questions
6
  from utils.date_utils import get_today_date_as_dd_mm_yyyy
7
  from utils.parsing_utils import find_and_parse_date, find_and_parse_phone_number
8
 
9
 
10
+ def build_form_data_from_answers(answers: dict[Questions, str], categories: list[str], signature: str | None = None) -> (
11
  Tuple)[dict[str, str], str]:
12
  form_data = {}
13
  for category in categories:
14
  form_data[form_fields_map[category]] = True
15
  form_data[form_fields_map["minor_work"]] = True
16
+ form_data[form_fields_map["signed_by"]] = answers[Questions.FULL_NAME]
17
+ form_data[form_fields_map["community"]] = answers[Questions.COMMUNITY]
18
+ form_data[form_fields_map["building_name"]] = answers[Questions.BUILDING]
19
+ form_data[form_fields_map["unit_no"]] = answers[Questions.UNIT_APT_NUMBER]
20
+ form_data[form_fields_map["owner" if "owner" in answers[Questions.OWNER_OR_TENANT].lower() else "tenant"]] = True
21
+ form_data[form_fields_map["start_date"]] = find_and_parse_date(answers[Questions.START_DATE])
22
+ form_data[form_fields_map["end_date"]] = find_and_parse_date(answers[Questions.END_DATE])
23
+ form_data[form_fields_map["contact_number"]] = find_and_parse_phone_number(answers[Questions.END_DATE])
24
+ form_data[form_fields_map["contractor_name"]] = answers[Questions.COMPANY_NAME]
25
+ form_data[form_fields_map["contractor_contact_number"]] = answers[Questions.COMPANY_NUMBER]
26
+ form_data[form_fields_map["contractor_email"]] = answers[Questions.COMPANY_EMAIL]
27
+ form_data[form_fields_map["email"]] = answers[Questions.YOUR_EMAIL]
28
+ form_data[form_fields_map["occupant_name"]] = answers[Questions.FULL_NAME]
29
  form_data[form_fields_map["signature_date"]] = get_today_date_as_dd_mm_yyyy()
30
  if signature:
31
  form_data[form_fields_map["signature"]] = signature
32
  return form_data, (f"work_permit_request"
33
+ f"_{'-'.join(categories)}_{answers[Questions.COMPANY_NAME]}_"
34
  f"{form_data[form_fields_map['start_date']].replace('/','-')}.pdf")
35
 
36
 
prompts/prompts_manager.py CHANGED
@@ -3,7 +3,6 @@ from enum import Enum
3
  from pathlib import Path
4
 
5
  from utils.date_utils import get_today_date_as_dd_mm_yyyy
6
- from form.form import work_categories as wc
7
 
8
 
9
  class Questions(Enum):
@@ -23,8 +22,7 @@ class Questions(Enum):
23
 
24
  class PromptsManager:
25
  def __init__(self, work_categories: dict[str, str] = None):
26
- if not work_categories:
27
- self.work_categories = wc
28
  base_path = Path(__file__).parent
29
  with open(Path(base_path, "system_prompt.txt")) as sysprompt_file:
30
  self.system_prompt: str = sysprompt_file.read()
@@ -45,7 +43,8 @@ class PromptsManager:
45
 
46
  def get_work_category(self, work_description: str) -> str:
47
  return (
48
- f"The work to do is {work_description}: choose the most accurate categories among the following {', '.join(self.work_categories.values())} "
 
49
  f"Only return the categories, separated by a semicolon")
50
 
51
  @staticmethod
 
3
  from pathlib import Path
4
 
5
  from utils.date_utils import get_today_date_as_dd_mm_yyyy
 
6
 
7
 
8
  class Questions(Enum):
 
22
 
23
  class PromptsManager:
24
  def __init__(self, work_categories: dict[str, str] = None):
25
+ self.work_categories = work_categories
 
26
  base_path = Path(__file__).parent
27
  with open(Path(base_path, "system_prompt.txt")) as sysprompt_file:
28
  self.system_prompt: str = sysprompt_file.read()
 
43
 
44
  def get_work_category(self, work_description: str) -> str:
45
  return (
46
+ f"The work to do is {work_description}: choose the most accurate categories among the following:"
47
+ f"{', '.join(self.work_categories.values())}\n"
48
  f"Only return the categories, separated by a semicolon")
49
 
50
  @staticmethod
repository/__init__.py CHANGED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import abc
2
+
3
+
4
+ class ModelRoles:
5
+ def __init__(self, system_role: str, user_role: str, ai_role: str):
6
+ self.system_role: str = system_role
7
+ self.user_role: str = user_role
8
+ self.ai_role: str = ai_role
9
+
10
+
11
+ class Model:
12
+ def __init__(self, model_name: str, model_roles: ModelRoles):
13
+ self.name = model_name
14
+ self.roles = model_roles
15
+
16
+
17
+ class Repository(abc.ABC):
18
+
19
+ def get_model_info(self) -> Model:
20
+ pass
21
+
22
+ def get_model_roles(self) -> ModelRoles:
23
+ pass
24
+
25
+ def get_message_history(self) -> list[dict[str, str]]:
26
+ pass
27
+
28
+ def send_prompt(self, prompt: str, add_to_history: bool) -> dict[str, str]:
29
+ pass
30
+
31
+ def set_message_for_role(self, role: str, message: str):
32
+ self.get_message_history().append({"role": role, "content": message})
33
+
34
+ def init(self):
35
+ pass
repository/intel_npu.py CHANGED
@@ -6,7 +6,7 @@ from intel_npu_acceleration_library.compiler import CompilerConfig
6
  from intel_npu_acceleration_library.dtypes import float32, float64, bfloat16
7
  from transformers import AutoTokenizer
8
 
9
- from repository.repository_abc import Repository, Model
10
 
11
 
12
  class IntelNpuRepository(Repository):
 
6
  from intel_npu_acceleration_library.dtypes import float32, float64, bfloat16
7
  from transformers import AutoTokenizer
8
 
9
+ from repository import Model, Repository
10
 
11
 
12
  class IntelNpuRepository(Repository):
repository/ollama.py CHANGED
@@ -1,7 +1,7 @@
1
  import ollama
2
  from ollama import Options
3
 
4
- from repository.repository_abc import Repository, Model
5
 
6
 
7
  class OllamaRepository(Repository):
 
1
  import ollama
2
  from ollama import Options
3
 
4
+ from repository import Model, Repository
5
 
6
 
7
  class OllamaRepository(Repository):
repository/ondemand.py CHANGED
@@ -3,7 +3,7 @@ from pathlib import Path
3
 
4
  import requests
5
 
6
- from repository.repository_abc import Repository, Model, ModelRoles
7
 
8
 
9
  class OndemandRepository(Repository):
 
3
 
4
  import requests
5
 
6
+ from repository import ModelRoles, Model, Repository
7
 
8
 
9
  class OndemandRepository(Repository):
repository/repository.py CHANGED
@@ -1,15 +1,16 @@
1
  import os
2
  from pathlib import Path
3
 
 
4
  from utils.env_utils import in_hf
5
 
6
  if not in_hf():
7
  from repository.intel_npu import IntelNpuRepository
8
  from repository.ollama import OllamaRepository
9
  from repository.ondemand import OndemandRepository
10
- from repository.repository_abc import Model
11
  from repository.testing_repo import TestingRepository
12
 
 
13
  def build_repo_from_environment(system_prompt: str):
14
  implementation = os.getenv("implementation")
15
  model_name = os.getenv("model_name")
@@ -18,10 +19,13 @@ def build_repo_from_environment(system_prompt: str):
18
  return get_repository(implementation, Model(model_name, ModelRoles("system",
19
  "user",
20
  "assistant")),
21
- system_prompt)
22
  else:
23
  return None
24
 
 
 
 
25
  def get_repository(implementation: str, model: Model, system_msg: str = None, log_to_file: Path = None):
26
  known_implementations = ["ollama", "intel_npu", "testing", "ondemand"]
27
  if not implementation or implementation.lower() not in known_implementations:
@@ -40,7 +44,9 @@ def get_repository(implementation: str, model: Model, system_msg: str = None, lo
40
  "content": "What is my full name?\n\nnull\n\nWhat is the nature of the work I need to do?\n\nPest control\n\nIn which community is the work taking place?\n\nJBR\n\nIn which building?\n\nnull\n\nIn which unit/apartment number?\n\nnull\n\nAm I the owner or the tenant?\n\nTenant\n\nIn which date is the work taking place?\n\n12/09/2024\n\nIn which date will the work finish?\n\n12/09/2024\n\nWhat is my contact number?\n\nnull\n\nWhat is the name of the contracting company?\n\nnull\n\nWhat is the contact number of the contracting company?\n\nnull\n\nWhat is the email of the contracting company?\n\nnull"
41
  },
42
  {
43
- "role":"assistant",
44
- "content":"pest_control"
45
  }
46
  ])
 
 
 
1
  import os
2
  from pathlib import Path
3
 
4
+ from repository import ModelRoles, Model
5
  from utils.env_utils import in_hf
6
 
7
  if not in_hf():
8
  from repository.intel_npu import IntelNpuRepository
9
  from repository.ollama import OllamaRepository
10
  from repository.ondemand import OndemandRepository
 
11
  from repository.testing_repo import TestingRepository
12
 
13
+
14
  def build_repo_from_environment(system_prompt: str):
15
  implementation = os.getenv("implementation")
16
  model_name = os.getenv("model_name")
 
19
  return get_repository(implementation, Model(model_name, ModelRoles("system",
20
  "user",
21
  "assistant")),
22
+ system_prompt)
23
  else:
24
  return None
25
 
26
+
27
+
28
+
29
  def get_repository(implementation: str, model: Model, system_msg: str = None, log_to_file: Path = None):
30
  known_implementations = ["ollama", "intel_npu", "testing", "ondemand"]
31
  if not implementation or implementation.lower() not in known_implementations:
 
44
  "content": "What is my full name?\n\nnull\n\nWhat is the nature of the work I need to do?\n\nPest control\n\nIn which community is the work taking place?\n\nJBR\n\nIn which building?\n\nnull\n\nIn which unit/apartment number?\n\nnull\n\nAm I the owner or the tenant?\n\nTenant\n\nIn which date is the work taking place?\n\n12/09/2024\n\nIn which date will the work finish?\n\n12/09/2024\n\nWhat is my contact number?\n\nnull\n\nWhat is the name of the contracting company?\n\nnull\n\nWhat is the contact number of the contracting company?\n\nnull\n\nWhat is the email of the contracting company?\n\nnull"
45
  },
46
  {
47
+ "role": "assistant",
48
+ "content": "pest_control"
49
  }
50
  ])
51
+
52
+
repository/repository_abc.py DELETED
@@ -1,35 +0,0 @@
1
- import abc
2
-
3
-
4
- class ModelRoles:
5
- def __init__(self, system_role: str, user_role: str, ai_role: str):
6
- self.system_role: str = system_role
7
- self.user_role: str = user_role
8
- self.ai_role: str = ai_role
9
-
10
-
11
- class Model:
12
- def __init__(self, model_name: str, model_roles: ModelRoles):
13
- self.name = model_name
14
- self.roles = model_roles
15
-
16
-
17
- class Repository(abc.ABC):
18
-
19
- def get_model_info(self) -> Model:
20
- pass
21
-
22
- def get_model_roles(self) -> ModelRoles:
23
- pass
24
-
25
- def get_message_history(self) -> list[dict[str, str]]:
26
- pass
27
-
28
- def send_prompt(self, prompt: str, add_to_history: bool) -> dict[str, str]:
29
- pass
30
-
31
- def set_message_for_role(self, role: str, message: str):
32
- self.get_message_history().append({"role": role, "content": message})
33
-
34
- def init(self):
35
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
repository/testing_repo.py CHANGED
@@ -1,7 +1,6 @@
1
  from typing import Tuple
2
 
3
-
4
- from repository.repository_abc import Repository, Model, ModelRoles
5
 
6
 
7
  class TestingRepository(Repository):
 
1
  from typing import Tuple
2
 
3
+ from repository import ModelRoles, Model, Repository
 
4
 
5
 
6
  class TestingRepository(Repository):
ui_manager.py CHANGED
@@ -9,7 +9,7 @@ from llm_manager.llm_parser import LlmParser
9
  from local_storage.entities import PersonalDetails, LocationDetails, ContractorDetails
10
  from prompts.prompts_manager import PromptsManager, Questions as Q
11
  from repository.repository import get_repository
12
- from repository.repository_abc import Model, ModelRoles
13
  from utils.parsing_utils import check_for_missing_answers
14
 
15
  ls: LocalStorage = LocalStorage()
 
9
  from local_storage.entities import PersonalDetails, LocationDetails, ContractorDetails
10
  from prompts.prompts_manager import PromptsManager, Questions as Q
11
  from repository.repository import get_repository
12
+ from repository import ModelRoles, Model
13
  from utils.parsing_utils import check_for_missing_answers
14
 
15
  ls: LocalStorage = LocalStorage()