luanpoppe commited on
Commit
9717e5b
·
1 Parent(s): 182e6fa

feat: adicionando possibilidade de enviar arquivo de PDF

Browse files
endpoint_teste/serializer.py CHANGED
@@ -8,7 +8,12 @@ class EndpointTesteSerializer(serializers.ModelSerializer):
8
  fields = "__all__"
9
 
10
  class TesteSerializer(serializers.Serializer):
11
- system_prompt = serializers.CharField(required=True)
12
  history_chat = serializers.ListField(read_only=True, required=False)
 
13
  user_message = serializers.CharField(required=True)
14
- pdf_url = serializers.CharField(required=False)
 
 
 
 
 
 
8
  fields = "__all__"
9
 
10
  class TesteSerializer(serializers.Serializer):
 
11
  history_chat = serializers.ListField(read_only=True, required=False)
12
+ system_prompt = serializers.CharField(required=True)
13
  user_message = serializers.CharField(required=True)
14
+ pdf_url = serializers.CharField(required=False)
15
+
16
+ class PDFUploadSerializer(serializers.Serializer):
17
+ file = serializers.FileField()
18
+ system_prompt = serializers.CharField(required=True)
19
+ user_message = serializers.CharField(required=True)
endpoint_teste/views.py CHANGED
@@ -1,12 +1,13 @@
 
1
  from rest_framework import viewsets, filters
2
  from django_filters.rest_framework import DjangoFilterBackend
3
  from endpoint_teste.models import EndpointTesteModel
4
- from endpoint_teste.serializer import EndpointTesteSerializer
5
 
6
  from rest_framework.decorators import api_view
7
  from rest_framework.response import Response
8
 
9
- from langchain_backend.main import get_llm_answer
10
  from .serializer import TesteSerializer
11
 
12
  class EndpointTesteViewSet(viewsets.ModelViewSet):
@@ -30,4 +31,39 @@ def getTeste(request):
30
  "Resposta": resposta_llm
31
  })
32
  if request.method == "GET":
33
- return Response("Fazendo um GET")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile, os
2
  from rest_framework import viewsets, filters
3
  from django_filters.rest_framework import DjangoFilterBackend
4
  from endpoint_teste.models import EndpointTesteModel
5
+ from endpoint_teste.serializer import EndpointTesteSerializer, PDFUploadSerializer
6
 
7
  from rest_framework.decorators import api_view
8
  from rest_framework.response import Response
9
 
10
+ from langchain_backend.main import get_llm_answer, get_llm_answer_from_pdf_file
11
  from .serializer import TesteSerializer
12
 
13
  class EndpointTesteViewSet(viewsets.ModelViewSet):
 
31
  "Resposta": resposta_llm
32
  })
33
  if request.method == "GET":
34
+ return Response("Fazendo um GET")
35
+
36
+ @api_view(["POST"])
37
+ def getPDF(request):
38
+ if request.method == "POST":
39
+ serializer = PDFUploadSerializer(data=request.data)
40
+ if serializer.is_valid(raise_exception=True):
41
+ # Access the uploaded file
42
+ data = request.data
43
+ print('data: ', data)
44
+ pdf_file = serializer.validated_data['file']
45
+ pdf_file.seek(0)
46
+ # print(dir(pdf_file))
47
+ # print('pdf_file: ', pdf_file.read())
48
+ # pdf_content = pdf_file.read()
49
+ # Save the file or process it as needed
50
+ # For example, you can save it to a specific location
51
+ # with open(f'endpoint_teste/media/uploads/{pdf_file.name}', 'wb+') as destination:
52
+ # for chunk in pdf_file.chunks():
53
+ # destination.write(chunk)
54
+ # return Response({"message": "File uploaded successfully."})
55
+
56
+ # Create a temporary file to save the uploaded PDF
57
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
58
+ # Write the uploaded file content to the temporary file
59
+ for chunk in pdf_file.chunks():
60
+ temp_file.write(chunk)
61
+ temp_file_path = temp_file.name # Get the path of the temporary file
62
+ print('temp_file_path: ', temp_file_path)
63
+ resposta_llm = get_llm_answer(data["system_prompt"], data["user_message"], temp_file_path)
64
+
65
+ os.remove(temp_file_path)
66
+
67
+ return Response({
68
+ "Resposta": resposta_llm
69
+ })
setup/urls.py CHANGED
@@ -2,8 +2,7 @@ from django.contrib import admin
2
  from django.urls import path, include
3
  from rest_framework import routers
4
 
5
- from endpoint_teste.views import EndpointTesteViewSet
6
- from endpoint_teste.views import getTeste
7
 
8
  router = routers.DefaultRouter()
9
  # router.register("endpoint-teste", EndpointTesteViewSet, basename="Basename do endpoint-teste")
@@ -11,6 +10,6 @@ router = routers.DefaultRouter()
11
  urlpatterns = [
12
  path("admin/", admin.site.urls),
13
  path('', include(router.urls)),
14
-
15
- path("teste", getTeste)
16
  ]
 
2
  from django.urls import path, include
3
  from rest_framework import routers
4
 
5
+ from endpoint_teste.views import EndpointTesteViewSet, getTeste, getPDF
 
6
 
7
  router = routers.DefaultRouter()
8
  # router.register("endpoint-teste", EndpointTesteViewSet, basename="Basename do endpoint-teste")
 
10
  urlpatterns = [
11
  path("admin/", admin.site.urls),
12
  path('', include(router.urls)),
13
+ path("teste", getTeste),
14
+ path('pdf', getPDF, name='upload-pdf')
15
  ]