Spaces:
Running
Running
luanpoppe
commited on
Commit
·
12b0dd7
1
Parent(s):
837e770
feat: adicionando url.py em cada app e mais refatorações
Browse files- _utils/handle_files.py +23 -0
- gerar_documento/urls.py +11 -0
- gerar_documento/views.py +9 -31
- modelos_usuarios/urls.py +13 -0
- ragas_api/urls.py +8 -0
- setup/settings.py +0 -1
- setup/urls.py +7 -20
_utils/handle_files.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tempfile, os
|
2 |
+
|
3 |
+
|
4 |
+
def handle_pdf_files_from_serializer(files):
|
5 |
+
listaPDFs = []
|
6 |
+
for file in files:
|
7 |
+
file.seek(0)
|
8 |
+
with tempfile.NamedTemporaryFile(
|
9 |
+
delete=False, suffix=".pdf"
|
10 |
+
) as temp_file: # Create a temporary file to save the uploaded PDF
|
11 |
+
for (
|
12 |
+
chunk
|
13 |
+
) in file.chunks(): # Write the uploaded file content to the temporary file
|
14 |
+
temp_file.write(chunk)
|
15 |
+
temp_file_path = temp_file.name # Get the path of the temporary file
|
16 |
+
listaPDFs.append(temp_file_path)
|
17 |
+
print("listaPDFs: ", listaPDFs)
|
18 |
+
return listaPDFs
|
19 |
+
|
20 |
+
|
21 |
+
def remove_pdf_temp_files(listaPDFs):
|
22 |
+
for file in listaPDFs:
|
23 |
+
os.remove(file)
|
gerar_documento/urls.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from django.urls import path
|
2 |
+
|
3 |
+
from gerar_documento.views import ResumoSimplesCursorCompletoView
|
4 |
+
|
5 |
+
urlpatterns = [
|
6 |
+
path(
|
7 |
+
"gerar-documento",
|
8 |
+
ResumoSimplesCursorCompletoView.as_view(),
|
9 |
+
name="summary-cursor-completo-pdf",
|
10 |
+
),
|
11 |
+
]
|
gerar_documento/views.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
from rest_framework.views import APIView
|
2 |
from adrf.views import APIView as AsyncAPIView
|
3 |
-
import tempfile, os
|
4 |
from rest_framework.response import Response
|
5 |
|
|
|
6 |
from _utils.resumo_completo_cursor import (
|
7 |
get_llm_summary_answer_by_cursor_complete,
|
8 |
)
|
@@ -11,6 +11,7 @@ from .serializer import (
|
|
11 |
)
|
12 |
from rest_framework.parsers import MultiPartParser
|
13 |
from drf_spectacular.utils import extend_schema
|
|
|
14 |
|
15 |
|
16 |
class ResumoSimplesCursorCompletoView(AsyncAPIView):
|
@@ -20,42 +21,19 @@ class ResumoSimplesCursorCompletoView(AsyncAPIView):
|
|
20 |
request=ResumoCursorCompeltoSerializer,
|
21 |
)
|
22 |
async def post(self, request):
|
|
|
23 |
serializer = ResumoCursorCompeltoSerializer(data=request.data)
|
24 |
if serializer.is_valid(raise_exception=True):
|
25 |
-
print("\n\n\n")
|
26 |
-
listaPDFs = []
|
27 |
data = serializer.validated_data
|
28 |
-
print("\
|
29 |
-
|
30 |
-
|
31 |
-
file.seek(0)
|
32 |
-
with tempfile.NamedTemporaryFile(
|
33 |
-
delete=False, suffix=".pdf"
|
34 |
-
) as temp_file: # Create a temporary file to save the uploaded PDF
|
35 |
-
for (
|
36 |
-
chunk
|
37 |
-
) in (
|
38 |
-
file.chunks()
|
39 |
-
): # Write the uploaded file content to the temporary file
|
40 |
-
temp_file.write(chunk)
|
41 |
-
temp_file_path = (
|
42 |
-
temp_file.name
|
43 |
-
) # Get the path of the temporary file
|
44 |
-
listaPDFs.append(temp_file_path)
|
45 |
-
print("listaPDFs: ", listaPDFs)
|
46 |
|
47 |
resposta_llm = await get_llm_summary_answer_by_cursor_complete(
|
48 |
data, listaPDFs
|
49 |
)
|
|
|
50 |
|
51 |
-
|
52 |
-
print("resposta_llm: ", resposta_llm)
|
53 |
-
|
54 |
-
final = resposta_llm
|
55 |
-
print("\n\n\n")
|
56 |
-
print("final: ", final)
|
57 |
-
|
58 |
-
for file in listaPDFs:
|
59 |
-
os.remove(file)
|
60 |
|
61 |
-
return Response({"resposta":
|
|
|
1 |
from rest_framework.views import APIView
|
2 |
from adrf.views import APIView as AsyncAPIView
|
|
|
3 |
from rest_framework.response import Response
|
4 |
|
5 |
+
from _utils.handle_files import handle_pdf_files_from_serializer, remove_pdf_temp_files
|
6 |
from _utils.resumo_completo_cursor import (
|
7 |
get_llm_summary_answer_by_cursor_complete,
|
8 |
)
|
|
|
11 |
)
|
12 |
from rest_framework.parsers import MultiPartParser
|
13 |
from drf_spectacular.utils import extend_schema
|
14 |
+
from datetime import datetime
|
15 |
|
16 |
|
17 |
class ResumoSimplesCursorCompletoView(AsyncAPIView):
|
|
|
21 |
request=ResumoCursorCompeltoSerializer,
|
22 |
)
|
23 |
async def post(self, request):
|
24 |
+
print(f"\n\nDATA E HORA DA REQUISIÇÃO: {datetime.now()}")
|
25 |
serializer = ResumoCursorCompeltoSerializer(data=request.data)
|
26 |
if serializer.is_valid(raise_exception=True):
|
|
|
|
|
27 |
data = serializer.validated_data
|
28 |
+
print("\n\ndata: ", data)
|
29 |
+
|
30 |
+
listaPDFs = handle_pdf_files_from_serializer(data["files"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
resposta_llm = await get_llm_summary_answer_by_cursor_complete(
|
33 |
data, listaPDFs
|
34 |
)
|
35 |
+
print("\n\nresposta_llm: ", resposta_llm)
|
36 |
|
37 |
+
remove_pdf_temp_files(listaPDFs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
return Response({"resposta": resposta_llm})
|
modelos_usuarios/urls.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from django.urls import path
|
2 |
+
|
3 |
+
from modelos_usuarios.views import (
|
4 |
+
ListCreateModeloUsuarioView,
|
5 |
+
CreateUpdateDeleteModeloUsuarioView,
|
6 |
+
ListModelosPorUsuarioView,
|
7 |
+
)
|
8 |
+
|
9 |
+
urlpatterns = [
|
10 |
+
path("modelo", ListCreateModeloUsuarioView.as_view()),
|
11 |
+
path("modelo/<int:pk>", CreateUpdateDeleteModeloUsuarioView.as_view()),
|
12 |
+
path("usuario/<int:user_id>/modelos", ListModelosPorUsuarioView.as_view()),
|
13 |
+
]
|
ragas_api/urls.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from django.urls import path
|
2 |
+
|
3 |
+
from ragas_api.views import RagasFromTextView, RagasView
|
4 |
+
|
5 |
+
urlpatterns = [
|
6 |
+
path("ragas", RagasView.as_view()),
|
7 |
+
path("ragas/no-pdf", RagasFromTextView.as_view()),
|
8 |
+
]
|
setup/settings.py
CHANGED
@@ -14,7 +14,6 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
14 |
# Quick-start development settings - unsuitable for production
|
15 |
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
16 |
|
17 |
-
# SECURITY WARNING: keep the secret key used in production secret!
|
18 |
SECRET_KEY = os.environ.get("SECRET_KEY")
|
19 |
|
20 |
# SECURITY WARNING: don't run with debug turned on in production!
|
|
|
14 |
# Quick-start development settings - unsuitable for production
|
15 |
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
16 |
|
|
|
17 |
SECRET_KEY = os.environ.get("SECRET_KEY")
|
18 |
|
19 |
# SECURITY WARNING: don't run with debug turned on in production!
|
setup/urls.py
CHANGED
@@ -2,37 +2,24 @@ from django.contrib import admin
|
|
2 |
from django.urls import path, include
|
3 |
from rest_framework import routers
|
4 |
from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView
|
5 |
-
|
6 |
-
|
7 |
-
from gerar_documento.views import ResumoSimplesCursorCompletoView
|
8 |
from _antigos.resumos.views import (
|
9 |
ResumoSimplesCursorView,
|
10 |
) # Não sei por quê, mas se remover esta importação, o endpoint de gerar_documentos para de funcionar
|
11 |
-
from ragas_api.views import RagasFromTextView, RagasView
|
12 |
-
from modelos_usuarios.views import (
|
13 |
-
ListCreateModeloUsuarioView,
|
14 |
-
CreateUpdateDeleteModeloUsuarioView,
|
15 |
-
ListModelosPorUsuarioView,
|
16 |
-
)
|
17 |
|
18 |
router = routers.DefaultRouter()
|
19 |
# router.register("endpoint-teste", EndpointTesteViewSet, basename="Basename do endpoint-teste")
|
20 |
|
21 |
-
|
22 |
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
|
23 |
path(
|
24 |
"swagger/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"
|
25 |
),
|
26 |
path("admin/", admin.site.urls),
|
27 |
path("", include(router.urls)),
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
),
|
33 |
-
path("
|
34 |
-
path("modelo/<int:pk>", CreateUpdateDeleteModeloUsuarioView.as_view()),
|
35 |
-
path("usuario/<int:user_id>/modelos", ListModelosPorUsuarioView.as_view()),
|
36 |
-
path("ragas", RagasView.as_view()),
|
37 |
-
path("ragas/no-pdf", RagasFromTextView.as_view()),
|
38 |
]
|
|
|
2 |
from django.urls import path, include
|
3 |
from rest_framework import routers
|
4 |
from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView
|
|
|
|
|
|
|
5 |
from _antigos.resumos.views import (
|
6 |
ResumoSimplesCursorView,
|
7 |
) # Não sei por quê, mas se remover esta importação, o endpoint de gerar_documentos para de funcionar
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
router = routers.DefaultRouter()
|
10 |
# router.register("endpoint-teste", EndpointTesteViewSet, basename="Basename do endpoint-teste")
|
11 |
|
12 |
+
config_urls = [
|
13 |
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
|
14 |
path(
|
15 |
"swagger/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"
|
16 |
),
|
17 |
path("admin/", admin.site.urls),
|
18 |
path("", include(router.urls)),
|
19 |
+
]
|
20 |
+
|
21 |
+
urlpatterns = config_urls + [
|
22 |
+
path("", include("gerar_documento.urls")),
|
23 |
+
path("", include("ragas_api.urls")),
|
24 |
+
path("", include("modelos_usuarios.urls")),
|
|
|
|
|
|
|
|
|
25 |
]
|