-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
114 lines (90 loc) · 3.84 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from llm.generate_recipes import GenerateRecipes
from neo4j import GraphDatabase
import neo4j
from dotenv import dotenv_values
import json
import ast
config = dotenv_values(".conf")
URI = config['URI']
AUTH = ast.literal_eval(config['AUTH'])
"""
Probar en local sin serv ni app
"""
# For testing
# That is the respond I'm waiting for
def export_json(df:object)->object:
"""
df input estructure example:
name brand ... cantidad unit
0 Huevos L docena Gorrotxategi ... 4 unidades
1 Huevos L de suelo docena Dagu ... 4 unidades
2 Huevos L 1/2 docena BM ... 4 unidades
3 Huevos L 1/2 docena Gorrotxategi ... 4 unidades
4 Huevos euskolabel decena Eusko Label ... 4 unidades
5 Huevos L docena BM ... 4 unidades
6 Cebolla ecológica en malla (1 kg aprox) ... ... 1 unidad
7 Cebolla tubo (1 kg aprox) ... ... 1 unida
"""
recetas_json = []
# group by recipes
grouped = df.groupby('receta')
for receta, group in grouped:
receta_dict = {
'receta': receta,
'ingredientes': {}
}
# Agrupar por ingrediente dentro de cada receta
for _, row in group.iterrows():
ingrediente = row['ingrediente']
# Crear el diccionario del ingrediente si no existe
if ingrediente not in receta_dict['ingredientes']:
receta_dict['ingredientes'][ingrediente] = {
'nombre': ingrediente,
'quantity': row['cantidad'],
'unit': row['unit'],
'productos': []
}
# Añadir el producto a la lista de productos
producto_info = {
'product_name': row['name'],
'product_brand': row['brand'].strip(),
'price': row['price'],
}
receta_dict['ingredientes'][ingrediente]['productos'].append(producto_info)
recetas_json.append(receta_dict)
print("recipes file was sucesfully generated")
return recetas_json
def main(user_input):
"""
Get products from neo4j db
"""
recipe_obj = GenerateRecipes()
response_recipes = recipe_obj.generate(user_input)
try:
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.verify_connectivity()
records_df = driver.execute_query(
"UNWIND $response_recipes as receta "
"UNWIND receta.ingredientes as ingrediente "
"MATCH (c:Category {category: ingrediente.categoria})--(p:Product) "
"RETURN p.product_name as name, "
"p.product_brand as brand, "
"p.product_price_centAmount as price,"
"receta.receta as receta,"
"ingrediente.ingrediente as ingrediente,"
"ingrediente.qty as cantidad,"
"ingrediente.unit as unit"
, response_recipes=response_recipes
, database_="neo4j"
, result_transformer_=neo4j.Result.to_df
)
json_export = export_json(records_df)
with open('exported_data.json', 'w', encoding='utf-8') as file:
json.dump(json_export, file, indent=4, ensure_ascii=False)
return json_export
except Exception as e:
print('Oups! something goes wrong, please check: ')
print({e})
if __name__=="__main__":
main('hazme una receta con carne')
print('done!')