Skip to content

Commit

Permalink
new file: .gitignore
Browse files Browse the repository at this point in the history
modified:   digitalseapi/app/infra/config.py
modified:   digitalseapi/app/main.py
modified:   digitalseapi/app/routers/__init__.py
modified:   digitalseapi/app/routers/documents.py
modified:   digitalseapi/requirements.txt
modified:   digitalseapi/tests/conftest.py
modified:   docker-compose.yml
new file:   ocrservice/.dockerignore
modified:   ocrservice/app.py
modified:   ocrservice/requirements.txt
  • Loading branch information
gomesrocha committed Mar 23, 2024
1 parent b1888d7 commit e76d5d8
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
venv
8 changes: 4 additions & 4 deletions digitalseapi/app/infra/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import lru_cache
from pydantic_settings import BaseSettings # Alteração aqui
from minio import Minio

from fastapi import Depends
log = logging.getLogger("uvicorn")


Expand All @@ -16,9 +16,9 @@ class Settings(BaseSettings):
MINIO_SECRET_KEY: str = "digitalse"
MINIO_SECURE: bool = False
SECRET_KEY: str = "digitalse"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
REFRESH_TOKEN_EXPIRE_MINUTES = 600
#ALGORITHM: str = "HS256"
#ACCESS_TOKEN_EXPIRE_MINUTES: str = 30
#REFRESH_TOKEN_EXPIRE_MINUTES: str = 600

class Config:
env_file = ".env"
Expand Down
6 changes: 3 additions & 3 deletions digitalseapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

from app.infra.db import init_db

from app.infra.config import get_settings
from app.infra.config import settings
from app.routers import main_router


app = FastAPI(
title="DigitalSE",
version="0.1.0",
version="0.1.15-rc0.2",
description="Sistema de gestão documental com extração de dados por OCR",
)
allow_origin = ["*"]

settings = get_settings()
#settings = get_settings()


app.add_middleware(
Expand Down
6 changes: 2 additions & 4 deletions digitalseapi/app/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .documents import router as documents_router
from .user import router as user_router
from .auth import router as auth_router
#from .auth import router as auth_router

main_router = APIRouter()

Expand All @@ -14,6 +14,4 @@
prefix="/users",
tags=["Users"])

main_router.include_router(auth_router,
prefix="/auth",
tags=["Auth"])
#main_router.include_router(auth_router, prefix="/auth", tags=["Auth"])
37 changes: 35 additions & 2 deletions digitalseapi/app/routers/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,44 @@
from app.infra.db import get_session
from app.infra.config import get_minio_client
import json
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import BaseModel
from typing import List

class KeywordList(BaseModel):
keywords: List[str]


router = APIRouter()

#Banco mongo, refatorar posteriormente, inserindo na infra de banco de dados
#mongo_client = AsyncIOMotorClient("mongodb://mongo:27017/")

async def get_mongodb_client() -> AsyncIOMotorClient:
return AsyncIOMotorClient("mongodb://mongo:27017/")
#db = mongo_client["document_db"]
#collection = db["documents"]


@router.post("/search")
async def search_documents(keywords: KeywordList,
mongodb_client: AsyncIOMotorClient = Depends(get_mongodb_client)):
try:
# Conecte-se à coleção no MongoDB
collection = mongodb_client["document_db"]["documents"]

# Consulta os documentos que contenham todas as palavras-chave fornecidas
cursor = collection.find({"_id": {"$in": keywords.keywords}})

# Transforma o cursor em uma lista de documentos
documents = await cursor.to_list(length=None)

# Retorna os documentos encontrados
return {"documents": documents}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error searching documents: {e}")

@router.post("/upload/")
@router.post("/")
async def upload_image(*, input_images: List[UploadFile] = File(...),
title: Optional[str],
description: Optional[str],
Expand Down Expand Up @@ -41,7 +74,7 @@ async def upload_image(*, input_images: List[UploadFile] = File(...),


# Endpoint para listar as imagens
@router.get("/images/", response_model=List[GestaoArquivos])
@router.get("/", response_model=List[GestaoArquivos])
async def list_images(session: Session = Depends(get_session)):
try:
# Consulta o PostgreSQL para obter os caminhos das imagens
Expand Down
2 changes: 2 additions & 0 deletions digitalseapi/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ aio-pika==9.3.1
setuptools==69.1.1
#elastic-apm[fastapi]
jose
passlib
motor==3.3.2
24 changes: 24 additions & 0 deletions digitalseapi/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import os

import pytest
from starlette.testclient import TestClient

from app import main
from app.config import get_settings, Settings


def get_settings_override():
return Settings(testing=1, database_url=os.environ.get("DATABASE_TEST_URL"))


@pytest.fixture(scope="module")
def test_app():
# set up
main.app.dependency_overrides[get_settings] = get_settings_override
with TestClient(main.app) as test_client:

# testing
yield test_client

# tear down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,21 @@ services:
- rabbitmq
networks:
- digitalse-network
mongo:
image: mongo
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
networks:
- digitalse-network

networks:
digitalse-network:

volumes:
data_postgres:
minio_data:
local-images:
rabbitmq_data:
mongo_data:
3 changes: 3 additions & 0 deletions ocrservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv
.dockerignore
Dockerfile
26 changes: 26 additions & 0 deletions ocrservice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import json
from pydantic import BaseModel
import re
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient


broker = RabbitBroker("amqp://guest:guest@rabbitmq:5672")
Expand All @@ -19,6 +21,11 @@
secret_key="digitalse",
secure=False)

# Configuração do MongoDB
mongo_client = AsyncIOMotorClient("mongodb://mongo:27017/")
db = mongo_client["document_db"]
collection = db["documents"]


class UploadedFile(BaseModel):
user_id: int
Expand All @@ -33,6 +40,24 @@ def clean_and_tokenize(text):
tokens = cleaned_text.split()
return tokens


async def index_document(document_id, tokens):
for token in tokens:
await collection.update_one(
{"_id": token},
{"$addToSet": {"documents": document_id}},
upsert=True
)

async def search_documents(keyword):
result = await collection.find_one({"_id": keyword})
if result:
return result.get("documents", [])
else:
return []



@broker.subscriber("ocr")
async def handle(message):
print(message)
Expand All @@ -52,5 +77,6 @@ async def handle(message):

# Imprimir o texto extraído
print(tokens)
await index_document(uploaded_file.document_id, tokens)
except Exception as e:
print(f"Erro ao transformar a mensagem em uma instância Pydantic: {e}")
3 changes: 2 additions & 1 deletion ocrservice/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ faststream[rabbit]
fast_depends
minio
pytesseract
pydantic
pydantic
motor==3.3.2

0 comments on commit e76d5d8

Please sign in to comment.