Skip to content

Commit

Permalink
Refactor Docker setup and enhance result tracking: Update Dockerfile,…
Browse files Browse the repository at this point in the history
… .dockerignore, and .gitignore; add current_file to ResultStatus; implement MeResource and adjust routes
  • Loading branch information
Erik172 committed Dec 8, 2024
1 parent 7428695 commit 4ddcbc1
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 20 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ __pycache__/
tests/
.venv/
app/ai_models/customTrOCR/
app/ai_models/*.pt
app/tests/
.gitingore
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,5 @@ temp/*/*.pdf

# AI models
app/ai_models/customTrOCR/
app/ai_models/*.pt

app/tests/*.http
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ RUN pip install -r requirements.txt

COPY . .

# RUN flask db init
RUN flask db init

# RUN flask db migrate
RUN flask db migrate

# RUN flask db upgrade
RUN flask db upgrade

EXPOSE 5000
EXPOSE 8080

CMD ["python", "app.py"]
CMD ["python", "wsgi.py"]
Binary file added app/ai_models/cude_v1.5.pt
Binary file not shown.
2 changes: 1 addition & 1 deletion app/ai_models/download_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def download_customOCR():
url_sharing = 'https://drive.google.com/file/d/1PdYn97COqsCGUfj5-tXvfM1c9hESebp6/view?usp=sharing'
file_id = url_sharing.split('/')[-2]
url = f'https://drive.google.com/uc?id={file_id}'
output = 'models/customTrOCR.zip'
output = 'app/models/customTrOCR.zip'

if not os.path.exists(output):
print('Downloading model...')
Expand Down
Binary file added app/ai_models/folio-detectV1.pt
Binary file not shown.
Binary file added app/ai_models/rode_v2.pt
Binary file not shown.
Binary file added app/ai_models/tilde_v1.pt
Binary file not shown.
1 change: 1 addition & 0 deletions app/models/result_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ResultStatus(db.Model):
status = db.Column(db.Enum(ResultStatusEnum), nullable=False, default=ResultStatusEnum.PENDING)
total_files = db.Column(db.Integer, nullable=True)
total_files_processed = db.Column(db.Integer, nullable=True)
current_file = db.Column(db.String(250), nullable=True)
last_updated_at = db.Column(db.DateTime, server_default=db.func.now(), server_onupdate=db.func.now())

def __repr__(self):
Expand Down
2 changes: 2 additions & 0 deletions app/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from .auth import *
from .user_resource import UserResource
from .me_resource import MeResource
from .ai_model_resource import AIModelResource
from .desd import DESDResource
from .result import *

__all__ = [
'LoginResource',
'UserResource',
'MeResource',
'AIModelResource',
'DESDResource',
'ExportResultResource',
Expand Down
4 changes: 3 additions & 1 deletion app/resources/desd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .desd_processing import DESDProcessing
from .desd_resource import DESDResource
from .desd_resource import DESDResource

__all__ = ['DESDProcessing', 'DESDResource']
13 changes: 7 additions & 6 deletions app/resources/desd/desd_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,20 @@ def post(self):

# Procesa el archivo según su extensión
try:
processed_files += 1
result_status.current_file = filename
result_status.status = ResultStatusEnum.RUNNING
result_status.total_files_processed = processed_files
result_status.last_updated_at = db.func.now()
db.session.commit()

if filename.lower().endswith('.pdf'):
DESDProcessing().process_pdf(models, results, filename, temp_path)
elif filename.lower().endswith(('.tiff', '.tif')):
DESDProcessing().process_tiff(models, results, filename, temp_path)
else:
DESDProcessing().process_image(models, results, filename, temp_path)

processed_files += 1
result_status.status = ResultStatusEnum.RUNNING
result_status.total_files_processed = processed_files
result_status.last_updated_at = db.func.now()
db.session.commit()

# Guardar resultados en la base de datos después de procesar cada archivo
for model_name in results[filename]:
for i in results[filename][model_name]:
Expand Down
Empty file removed app/resources/desd/desd_utils.py
Empty file.
18 changes: 18 additions & 0 deletions app/resources/me_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask_restful import Resource
from flask_jwt_extended import jwt_required, current_user
from app.models import User
from app.schemas import UserSchema

class MeResource(Resource):
@jwt_required()
def get(self):
"""
Obtiene la información del usuario autenticado.
Requiere autenticación JWT.
Returns:
dict: Información del usuario autenticado en formato JSON.
"""
user = User.query.get(current_user.id)
user_schema = UserSchema(exclude=['password'])

return user_schema.dump(user), 200
4 changes: 2 additions & 2 deletions app/resources/result/result_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ResultResource(Resource):
@jwt_required()
def get(self, result_id: str = None):
if result_id:
result = Result.query.filter_by(result_id=result_id).first()
result = Result.query.filter_by(collection_id=result_id).first()
if not result:
return {'message': 'Result not found'}, 404

Expand Down Expand Up @@ -72,4 +72,4 @@ def delete(self, result_id: str):

return {'message': 'Result deleted'}, 200



3 changes: 2 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

def initialize_routes(api: Api) -> None:
api.add_resource(LoginResource, '/api/v1/auth/login', endpoint='login')
api.add_resource(MeResource, '/api/v1/auth/me', '/api/v1/users/me', endpoint='me')

api.add_resource(UserResource, '/api/v1/users', '/api/v1/users/<int:user_id>', endpoint='users')

Expand All @@ -11,4 +12,4 @@ def initialize_routes(api: Api) -> None:
api.add_resource(DESDResource, '/api/v1/desd', endpoint='desd')

api.add_resource(ResultResource, '/api/v1/results', '/api/v1/results/<string:result_id>', endpoint='results')
api.add_resource(ExportResultResource, '/api/v1/results/<string:collection_name>/export', endpoint='export_result')
api.add_resource(ExportResultResource, '/api/v1/results/<string:collection_name>/export', endpoint='export_result')
1 change: 1 addition & 0 deletions app/schemas/result_status_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Meta:
status = fields.Enum(ResultStatusEnum, required=True, default=ResultStatusEnum.PENDING)
total_files = fields.Integer()
total_files_processed = fields.Integer()
current_file = fields.String()
last_updated_at = fields.DateTime(dump_only=True)

@validates('status')
Expand Down
10 changes: 8 additions & 2 deletions app/schemas/user_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from marshmallow import validates, ValidationError, fields
from app.models import User
from app.models import User, Result
from app import ma

class UserSchema(ma.SQLAlchemyAutoSchema):
Expand All @@ -14,7 +14,7 @@ class Meta:
email = fields.Email(required=True, unique=True)
password = fields.String(required=True)
is_admin = fields.Boolean(required=False, default=False)
results = fields.Nested('ResultSchema', many=True, exclude=['user'])
results = fields.Method('get_results', dump_only=True)
created_at = fields.DateTime(dump_only=True)
updated_at = fields.DateTime(dump_only=True)

Expand All @@ -29,3 +29,9 @@ def validate_email(self, value):
def validate_is_admin(self, value):
if value and not self.context.get('user').is_admin:
raise ValidationError('Only admins can create admin users.')

def get_results(self, obj):
from app.schemas import ResultSchema
results = Result.query.filter_by(user_id=obj.id).all()
schema = ResultSchema(many=True)
return schema.dump(results)

0 comments on commit 4ddcbc1

Please sign in to comment.