Spaces:
Sleeping
Sleeping
Commit
·
d015f0c
1
Parent(s):
8d06b39
first integration with local ollama server
Browse files- test.json +41 -0
- app.py +28 -6
- questions.txt +10 -0
- repository/__init__.py +0 -0
- repository/ollama.py +20 -0
- requirements.txt +1 -1
- schema.py +88 -13
- system_prompt.txt +6 -0
- verification_prompt.txt +13 -0
test.json
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
[{
|
3 |
+
"question": "1) What do you need to do?",
|
4 |
+
"answer": "Pest control treatment"
|
5 |
+
},
|
6 |
+
{
|
7 |
+
"question": "2) In which community is the work taking place?",
|
8 |
+
"answer": "JBR"
|
9 |
+
},
|
10 |
+
{
|
11 |
+
"question": "3) In which building?",
|
12 |
+
"answer": "Sadaf"
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"question": "4) In which unit/apartment number?",
|
16 |
+
"answer": "2408"
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"question": "5) Am I an owner or a tenant?",
|
20 |
+
"answer": "Owner"
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"question": "6) In which date is the work taking place?",
|
24 |
+
"answer": "7/8/2024"
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"question": "7) In which date will the work finish?",
|
28 |
+
"answer": "Unclear" (assuming it's a one-day job, but not specified)
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"question": "8) What is my contact number?",
|
32 |
+
"answer": "Unclear"
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"question": "9) What is the name of the contracting company?",
|
36 |
+
"answer": "Breathe Maintenance"
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"question": "10) What is the contact number of the contracting company?",
|
40 |
+
"answer": "Unclear"
|
41 |
+
}]
|
app.py
CHANGED
@@ -1,10 +1,32 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
2 |
|
3 |
if __name__ == '__main__':
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
form = PdfReader("form.pdf")
|
9 |
-
checkboxes = [v.name for k,v in form.get_fields().items() if v.field_type == "/Btn" and v.value == "/Yes"]
|
10 |
-
print(checkboxes)
|
|
|
1 |
+
import json
|
2 |
+
from json import JSONDecodeError
|
3 |
+
|
4 |
+
from repository.ollama import OllamaRepository
|
5 |
+
from schema import ModelRoles
|
6 |
|
7 |
if __name__ == '__main__':
|
8 |
+
with open("questions.txt") as questions_file:
|
9 |
+
questions = questions_file.read()
|
10 |
+
with open("system_prompt.txt") as system_prompt_file:
|
11 |
+
system_prompt = system_prompt_file.read()
|
12 |
+
with open("verification_prompt.txt") as verification_prompt_file:
|
13 |
+
verification_prompt = verification_prompt_file.read()
|
14 |
+
verification_prompt = verification_prompt.replace("{questions}", questions)
|
15 |
+
user_prompt = input(f"Please describe what you need to do. To get the best results "
|
16 |
+
f"try to answer the following questions:\n{questions}\n\n>")
|
17 |
+
|
18 |
+
ollama_repository = OllamaRepository("llama3.1", system_prompt,
|
19 |
+
ModelRoles("system", "user", "assistant"))
|
20 |
+
|
21 |
+
ollama_repository.send_prompt(f"Ingest the following information: {user_prompt}")
|
22 |
+
answer = ollama_repository.send_prompt(verification_prompt)
|
23 |
+
json_data_start = answer["content"].index("{")
|
24 |
+
json_data_stop = answer["content"].rindex("}")
|
25 |
|
26 |
+
answers = "[" + answer["content"][json_data_start:json_data_stop+1] + "]"
|
27 |
+
try:
|
28 |
+
json_answers = json.loads(answers)
|
29 |
+
print(json_answers)
|
30 |
+
except JSONDecodeError as e:
|
31 |
+
print(f"unable to parse \n {answers}")
|
32 |
|
|
|
|
|
|
questions.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
1) What do you need to do?
|
2 |
+
2) In which community is the work taking place?
|
3 |
+
3) In which building?
|
4 |
+
4) In which unit/apartment number?
|
5 |
+
5) Am I an owner or a tenant?
|
6 |
+
6) In which date is the work taking place?
|
7 |
+
7) In which date will the work finish?
|
8 |
+
8) What is my contact number?
|
9 |
+
9) What is the name of the contracting company?
|
10 |
+
10) What is the contact number of the contracting company?
|
repository/__init__.py
ADDED
File without changes
|
repository/ollama.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ollama
|
2 |
+
from ollama import Options
|
3 |
+
|
4 |
+
from schema import ModelRoles
|
5 |
+
|
6 |
+
|
7 |
+
class OllamaRepository:
|
8 |
+
def __init__(self, model, system_msg, roles: ModelRoles):
|
9 |
+
self.model = model
|
10 |
+
self.system_msg = system_msg
|
11 |
+
self.roles = roles
|
12 |
+
self.message_history: list[dict[str, str]] = [{"role": self.roles.system_role, "content": system_msg}]
|
13 |
+
|
14 |
+
def send_prompt(self, prompt):
|
15 |
+
options: Options = Options(temperature=0.1)
|
16 |
+
self.message_history.append({"role": self.roles.user_role, "content":prompt})
|
17 |
+
response = ollama.chat(self.model, self.message_history, options=options)
|
18 |
+
answer = {"role": self.roles.ai_role, "content": response["message"]["content"]}
|
19 |
+
self.message_history.append(answer)
|
20 |
+
return answer
|
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
PyPDFForm
|
2 |
-
|
|
|
1 |
PyPDFForm
|
2 |
+
ollama
|
schema.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
from typing import TypedDict
|
2 |
|
3 |
|
4 |
-
class FormFields(TypedDict):
|
5 |
start_date: str
|
6 |
end_date: str
|
7 |
signed_by: str
|
@@ -16,21 +16,96 @@ class FormFields(TypedDict):
|
|
16 |
contractor_contact_number: str
|
17 |
occupant_name: str
|
18 |
civil: str
|
19 |
-
electrical_AC:str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
|
22 |
form_fields_map: FormFields = {"start_date": "From", "end_date": "to", "signed_by": "name-10",
|
23 |
"signature_date": "From--1", "community": "Name-2", "email": "Name-3",
|
24 |
"unit_no": "Name-4", "contact_number": "Name-5", "building_name": "Name-6",
|
25 |
"contractor_name": "Name-7", "contractor_email": "name-8",
|
26 |
-
"contractor_contact_number": "name-9", "occupant_name":"name-1",
|
27 |
-
"
|
28 |
-
"
|
29 |
-
"
|
30 |
-
"
|
31 |
-
"
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
"
|
36 |
-
|
|
|
|
1 |
from typing import TypedDict
|
2 |
|
3 |
|
4 |
+
class FormFields(TypedDict, total=True):
|
5 |
start_date: str
|
6 |
end_date: str
|
7 |
signed_by: str
|
|
|
16 |
contractor_contact_number: str
|
17 |
occupant_name: str
|
18 |
civil: str
|
19 |
+
electrical_AC: str
|
20 |
+
furniture_delivery: str
|
21 |
+
handyman: str
|
22 |
+
pest_control: str
|
23 |
+
sail_shade: str
|
24 |
+
wardrobes: str
|
25 |
+
wooden_flooring: str
|
26 |
+
plumbing_sanitary: str
|
27 |
+
safety_systems: str
|
28 |
+
soft_landscaping: str
|
29 |
+
balcony_tiles: str
|
30 |
+
bathroom_toilet_refurbish: str
|
31 |
+
staircase_railing: str
|
32 |
+
storage_box: str
|
33 |
+
tiling_works: str
|
34 |
+
doors_door_frames: str
|
35 |
+
gypsum_ceiling: str
|
36 |
+
kitchen_refurbish: str
|
37 |
+
lights_and_sockets: str
|
38 |
+
painting_wallpapering: str
|
39 |
+
seating_area_barbecuing_fountain: str
|
40 |
+
other_work_checkbox: str
|
41 |
+
owner: str
|
42 |
+
tenant: str
|
43 |
+
signature: str
|
44 |
+
minor_work: str
|
45 |
+
|
46 |
+
|
47 |
+
class MinorWorkForm:
|
48 |
+
def __init__(self):
|
49 |
+
self.start_date: str
|
50 |
+
self.end_date: str
|
51 |
+
self.signed_by: str
|
52 |
+
self.signature_date: str
|
53 |
+
self.community: str
|
54 |
+
self.email: str
|
55 |
+
self.unit_no: str
|
56 |
+
self.contact_number: str
|
57 |
+
self.building_name: str
|
58 |
+
self.contractor_name: str
|
59 |
+
self.contractor_email: str
|
60 |
+
self.contractor_contact_number: str
|
61 |
+
self.occupant_name: str
|
62 |
+
self.civil: bool
|
63 |
+
self.electrical_AC: bool
|
64 |
+
self.furniture_delivery: bool
|
65 |
+
self.handyman: bool
|
66 |
+
self.pest_control: bool
|
67 |
+
self.sail_shade: bool
|
68 |
+
self.wardrobes: bool
|
69 |
+
self.wooden_flooring: bool
|
70 |
+
self.plumbing_sanitary: bool
|
71 |
+
self.safety_systems: bool
|
72 |
+
self.soft_landscaping: bool
|
73 |
+
self.balcony_tiles: bool
|
74 |
+
self.bathroom_toilet_refurbish: bool
|
75 |
+
self.staircase_railing: bool
|
76 |
+
self.storage_box: bool
|
77 |
+
self.tiling_works: bool
|
78 |
+
self.doors_door_frames: bool
|
79 |
+
self.gypsum_ceiling: bool
|
80 |
+
self.kitchen_refurbish: bool
|
81 |
+
self.lights_and_sockets: bool
|
82 |
+
self.painting_wallpapering: bool
|
83 |
+
self.seating_area_barbecuing_fountain: bool
|
84 |
+
self.other_work_checkbox: bool
|
85 |
+
self.owner: bool
|
86 |
+
self.tenant: bool
|
87 |
+
self.signature: str
|
88 |
+
|
89 |
+
class ModelRoles:
|
90 |
+
def __init__(self, system_role, user_role, ai_role):
|
91 |
+
self.system_role = system_role
|
92 |
+
self.user_role = user_role
|
93 |
+
self.ai_role = ai_role
|
94 |
|
95 |
|
96 |
form_fields_map: FormFields = {"start_date": "From", "end_date": "to", "signed_by": "name-10",
|
97 |
"signature_date": "From--1", "community": "Name-2", "email": "Name-3",
|
98 |
"unit_no": "Name-4", "contact_number": "Name-5", "building_name": "Name-6",
|
99 |
"contractor_name": "Name-7", "contractor_email": "name-8",
|
100 |
+
"contractor_contact_number": "name-9", "occupant_name": "name-1",
|
101 |
+
"other_work": "name-19", "civil": "3", "electrical_AC": "10", "furniture_delivery": "14",
|
102 |
+
"handyman": "18", "pest_control": "21", "sail_shade": "12", "wardrobes": "16",
|
103 |
+
"wooden_flooring": "24",
|
104 |
+
"plumbing_sanitary": "4", "safety systems": "4", "soft_landscaping": "9",
|
105 |
+
"balcony_tiles": "13", "bathroom_toilet_refurbish": "20",
|
106 |
+
"staircase_railing": "17", "storage_box": "11", "tiling_works": "15",
|
107 |
+
"doors_door_frames": "6", "gypsum_ceiling": "22", "kitchen_refurbish": "7",
|
108 |
+
"lights_and_sockets": "23", "painting_wallpapering": "19",
|
109 |
+
"seating_area_barbecuing_fountain": "8",
|
110 |
+
"other_work_checkbox": "69", "owner": "Check Box4", "tenant": "Check Box3",
|
111 |
+
"signature": "Signature2", "minor_work": "1"}
|
system_prompt.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
You are a helpful assistant helping tenants and owners when filling minor work permits. Do not answer question outside
|
2 |
+
of this domain.
|
3 |
+
Keep in mind that 'JBR' is a community, and buildings are named after the following names followed by a number:
|
4 |
+
Murjan, Bahar, Shams, Amwaj, Sadaf
|
5 |
+
for example "Murjan 1"
|
6 |
+
Today is the 6th of August 2024
|
verification_prompt.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Please tell if I answered the following questions:
|
2 |
+
|
3 |
+
{questions}
|
4 |
+
|
5 |
+
The answer should be a list of json objects formatted as follows:
|
6 |
+
{
|
7 |
+
"question": "<question>"
|
8 |
+
"answer": "<answer>"
|
9 |
+
}
|
10 |
+
if the answer is a date format it as day/month/year.
|
11 |
+
If the answer contains temporal references such as 'tomorrow', 'in two days' etc. consider that today it is the
|
12 |
+
6th of september 2024.
|
13 |
+
If the answer is not provided say "Unclear"
|