Skip to content

Commit e6b88f6

Browse files
Merge pull request #12 from Kalgoc/AI
AI
2 parents 72d7dc1 + 564ddb3 commit e6b88f6

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
__pycache__/
33
*.py[cod]
44
*$py.class
5-
5+
*.env
66
# C extensions
77
*.so
8-
8+
*.csv
99
# Distribution / packaging
1010
.Python
1111
build/

AI/AI.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import openai
2+
import os
3+
4+
5+
def setup_api_key():
6+
openai.api_key = os.environ["OPENAI_KEY"]
7+
8+
9+
def classify_text(texto):
10+
response = openai.ChatCompletion.create(
11+
model="gpt-4-turbo",
12+
messages=[
13+
{
14+
"role": "system",
15+
"content": "Eres un modelo que clasifica gastos en \
16+
categorías predefinidas.",
17+
},
18+
{
19+
"role": "user",
20+
"content": f"Clasifica el siguiente gasto en una de las \
21+
categorías: comida, transporte, vivienda, educación, salud, \
22+
entretenimiento,\
23+
ahorro, inversión.\n\nTexto: {texto}\n\nCategoría:",
24+
},
25+
],
26+
max_tokens=15,
27+
temperature=0,
28+
)
29+
category = response.choices[0].message["content"].strip()
30+
return category
31+
32+
33+
def clean_category(category):
34+
if category.lower().startswith("categoría: "):
35+
category = category[11:]
36+
if category.endswith("."):
37+
category = category[:-1]
38+
category = category.capitalize()
39+
categories_allowed = [
40+
"Comida",
41+
"Transporte",
42+
"Vivienda",
43+
"Educación",
44+
"Salud",
45+
"Entretenimiento",
46+
"Ahorro",
47+
"Inversión",
48+
]
49+
if category not in categories_allowed:
50+
raise ValueError(f"Categoría no permitida: {category}")
51+
return category

AI/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# flake8: noqa
2+
from .AI import setup_api_key, classify_text, clean_category

docker-compose.yml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ services:
1212
- "5432:5432"
1313
networks:
1414
- app_network
15+
environment:
16+
POSTGRES_HOST_AUTH_METHOD: trust
1517

1618
web:
1719
build:

0 commit comments

Comments
 (0)