Spaces:
Running
Running
update language
Browse files- trauma/api/data/model.py +1 -0
- trauma/api/data/prepare_data.py +61 -7
- trauma/api/message/ai/prompts.py +28 -0
trauma/api/data/model.py
CHANGED
@@ -40,4 +40,5 @@ class EntityModel(MongoBaseModel):
|
|
40 |
ageGroups: list[AgeGroup]
|
41 |
treatmentAreas: list[str]
|
42 |
treatmentMethods: list[str]
|
|
|
43 |
contactDetails: ContactDetails
|
|
|
40 |
ageGroups: list[AgeGroup]
|
41 |
treatmentAreas: list[str]
|
42 |
treatmentMethods: list[str]
|
43 |
+
description: str = ''
|
44 |
contactDetails: ContactDetails
|
trauma/api/data/prepare_data.py
CHANGED
@@ -1,18 +1,17 @@
|
|
1 |
import asyncio
|
|
|
|
|
2 |
import re
|
3 |
|
4 |
-
import pandas as pd
|
5 |
-
|
6 |
from trauma.api.data.dto import AgeGroup
|
7 |
from trauma.api.data.model import EntityModel
|
|
|
8 |
from trauma.core.config import settings
|
9 |
|
|
|
10 |
#
|
11 |
#
|
12 |
|
13 |
-
file_path = 'shorted_data.csv'
|
14 |
-
df = pd.read_csv(file_path)
|
15 |
-
|
16 |
|
17 |
async def main():
|
18 |
for _, row in df.iterrows():
|
@@ -63,7 +62,6 @@ async def main():
|
|
63 |
await settings.DB_CLIENT.entities.insert_one(entity_model.to_mongo())
|
64 |
|
65 |
|
66 |
-
#
|
67 |
def prepare_entities_str(entities: list[EntityModel]) -> list[str]:
|
68 |
entities_str = []
|
69 |
for entity in entities:
|
@@ -76,5 +74,61 @@ def prepare_entities_str(entities: list[EntityModel]) -> list[str]:
|
|
76 |
entities_str.append(entity_str)
|
77 |
return entities_str
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
if __name__ == '__main__':
|
80 |
-
asyncio.run(
|
|
|
1 |
import asyncio
|
2 |
+
import csv
|
3 |
+
import json
|
4 |
import re
|
5 |
|
|
|
|
|
6 |
from trauma.api.data.dto import AgeGroup
|
7 |
from trauma.api.data.model import EntityModel
|
8 |
+
from trauma.api.message.ai.prompts import TraumaPrompts
|
9 |
from trauma.core.config import settings
|
10 |
|
11 |
+
|
12 |
#
|
13 |
#
|
14 |
|
|
|
|
|
|
|
15 |
|
16 |
async def main():
|
17 |
for _, row in df.iterrows():
|
|
|
62 |
await settings.DB_CLIENT.entities.insert_one(entity_model.to_mongo())
|
63 |
|
64 |
|
|
|
65 |
def prepare_entities_str(entities: list[EntityModel]) -> list[str]:
|
66 |
entities_str = []
|
67 |
for entity in entities:
|
|
|
74 |
entities_str.append(entity_str)
|
75 |
return entities_str
|
76 |
|
77 |
+
|
78 |
+
def csv_to_dict_list(file_path):
|
79 |
+
with open(file_path, mode='r', encoding='utf-8') as csv_file:
|
80 |
+
reader = csv.DictReader(csv_file)
|
81 |
+
return [row for row in reader]
|
82 |
+
|
83 |
+
|
84 |
+
async def generate_description_with_ai(semaphore: asyncio.Semaphore, clinic: dict) -> str:
|
85 |
+
async with semaphore:
|
86 |
+
messages = [
|
87 |
+
{
|
88 |
+
"role": "system",
|
89 |
+
"content": TraumaPrompts.generate_clinic_description
|
90 |
+
.replace("{entity}", json.dumps(clinic, indent=2))
|
91 |
+
}
|
92 |
+
]
|
93 |
+
completion = await settings.OPENAI_CLIENT.chat.completions.create(
|
94 |
+
messages=messages,
|
95 |
+
model='gpt-4o-mini',
|
96 |
+
temperature=0.8,
|
97 |
+
response_format={"type": "json_object"},
|
98 |
+
n=1
|
99 |
+
)
|
100 |
+
response = json.loads(completion.choices[0].message.content)
|
101 |
+
return response['description']
|
102 |
+
|
103 |
+
|
104 |
+
async def generate_descriptions():
|
105 |
+
data = csv_to_dict_list(settings.BASE_DIR / 'translated_output.csv')
|
106 |
+
# semaphore = asyncio.Semaphore(value=10)
|
107 |
+
# descriptions = await asyncio.gather(*[generate_description_with_ai(semaphore, clinic=clinic) for clinic in data])
|
108 |
+
with open('test.json', 'r') as f:
|
109 |
+
descriptions = json.loads(f.read())
|
110 |
+
for clinic, description in zip(data, descriptions['descriptions']):
|
111 |
+
email = re.search(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", clinic['Email'])
|
112 |
+
if email:
|
113 |
+
email = email.group(0)
|
114 |
+
else:
|
115 |
+
email = None
|
116 |
+
query = {
|
117 |
+
"name": clinic['Organization'].strip().strip('\n').strip(),
|
118 |
+
"contactDetails.email": email,
|
119 |
+
"contactDetails.address": clinic['Location'].strip().strip('\n').strip(),
|
120 |
+
# "contactDetails.postalCode": clinic['Postal code'].strip().strip('\n').strip()
|
121 |
+
}
|
122 |
+
document = await settings.DB_CLIENT.entities.find_one(query)
|
123 |
+
if not document:
|
124 |
+
continue
|
125 |
+
await settings.DB_CLIENT.entities.update_one(
|
126 |
+
{"id": document["id"]},
|
127 |
+
{
|
128 |
+
"$set": {"description": description}
|
129 |
+
}
|
130 |
+
)
|
131 |
+
|
132 |
+
|
133 |
if __name__ == '__main__':
|
134 |
+
asyncio.run(generate_descriptions())
|
trauma/api/message/ai/prompts.py
CHANGED
@@ -109,3 +109,31 @@ Je moet een antwoord genereren aan de gebruiker waarin je aangeeft dat je geschi
|
|
109 |
## Voorbeeld van antwoorden
|
110 |
|
111 |
- Gefeliciteerd! Hier is een lijst van klinieken die perfect passen bij deze aandoening. Ik heb deze klinieken aanbevolen omdat ze voldoen aan de gevraagde leeftijdsbeperkingen en gespecialiseerd zijn in de behandeling van deze aandoening met behulp van dergelijke methoden."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
## Voorbeeld van antwoorden
|
110 |
|
111 |
- Gefeliciteerd! Hier is een lijst van klinieken die perfect passen bij deze aandoening. Ik heb deze klinieken aanbevolen omdat ze voldoen aan de gevraagde leeftijdsbeperkingen en gespecialiseerd zijn in de behandeling van deze aandoening met behulp van dergelijke methoden."""
|
112 |
+
|
113 |
+
generate_clinic_description = """## Taak
|
114 |
+
|
115 |
+
Je bent verplicht om een beschrijving voor een kliniek te genereren op basis van de gegevens over deze kliniek [clinic data]. Je antwoord moet worden opgeslagen in het JSON-veld “description”.
|
116 |
+
|
117 |
+
## Gegevens
|
118 |
+
|
119 |
+
**clinic data**:
|
120 |
+
|
121 |
+
{entity}
|
122 |
+
|
123 |
+
## JSON-antwoordformaat
|
124 |
+
|
125 |
+
```
|
126 |
+
{
|
127 |
+
"description": "string"
|
128 |
+
}
|
129 |
+
```
|
130 |
+
|
131 |
+
[INST]
|
132 |
+
|
133 |
+
## Instructies voor het genereren van de beschrijving
|
134 |
+
|
135 |
+
- De beschrijving van de kliniek moet creatief en promotioneel zijn. Schrijf de beschrijving vanuit het perspectief van de kliniek, waarbij soms de naam van de kliniek en het voorzetsel “Wij” worden gebruikt.
|
136 |
+
- De beschrijving moet worden gepresenteerd in twee alinea's.
|
137 |
+
- De beschrijving moet beknopt en bondig zijn.
|
138 |
+
|
139 |
+
[/INST]"""
|