Spaces:
Sleeping
Sleeping
luanpoppe
commited on
Commit
·
c66a7e7
1
Parent(s):
c625f4c
feat: adicionando app de ragas
Browse files- ragas_api/__init__.py +0 -0
- ragas_api/admin.py +3 -0
- ragas_api/apps.py +6 -0
- ragas_api/migrations/__init__.py +0 -0
- ragas_api/models.py +3 -0
- ragas_api/serializer.py +15 -0
- ragas_api/tests.py +3 -0
- ragas_api/views.py +124 -0
- setup/settings.py +29 -24
- setup/urls.py +6 -3
ragas_api/__init__.py
ADDED
File without changes
|
ragas_api/admin.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from django.contrib import admin
|
2 |
+
|
3 |
+
# Register your models here.
|
ragas_api/apps.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from django.apps import AppConfig
|
2 |
+
|
3 |
+
|
4 |
+
class RagasApiConfig(AppConfig):
|
5 |
+
default_auto_field = 'django.db.models.BigAutoField'
|
6 |
+
name = 'ragas_api'
|
ragas_api/migrations/__init__.py
ADDED
File without changes
|
ragas_api/models.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from django.db import models
|
2 |
+
|
3 |
+
# Create your models here.
|
ragas_api/serializer.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from rest_framework import serializers
|
2 |
+
from resumos.serializer import ResumoCursorCompeltoSerializer
|
3 |
+
|
4 |
+
|
5 |
+
class RagasSerializer(ResumoCursorCompeltoSerializer):
|
6 |
+
files = serializers.ListField(child=serializers.FileField(), required=True)
|
7 |
+
id_modelo_do_usuario = serializers.IntegerField(required=False)
|
8 |
+
hf_embedding = serializers.CharField(required=False, default="all-MiniLM-L6-v2")
|
9 |
+
|
10 |
+
|
11 |
+
class RagasFromTextSerializer(ResumoCursorCompeltoSerializer):
|
12 |
+
files = None
|
13 |
+
id_modelo_do_usuario = serializers.IntegerField(required=False, default=9)
|
14 |
+
user_message = serializers.CharField(required=True)
|
15 |
+
context_provided = serializers.CharField(required=False)
|
ragas_api/tests.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from django.test import TestCase
|
2 |
+
|
3 |
+
# Create your tests here.
|
ragas_api/views.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from rest_framework.views import APIView
|
2 |
+
import tempfile, os
|
3 |
+
from rest_framework.response import Response
|
4 |
+
|
5 |
+
from _utils.resumo_completo_cursor import (
|
6 |
+
get_llm_summary_answer_by_cursor_complete,
|
7 |
+
test_ragas,
|
8 |
+
)
|
9 |
+
from .serializer import (
|
10 |
+
RagasFromTextSerializer,
|
11 |
+
RagasSerializer,
|
12 |
+
)
|
13 |
+
from rest_framework.parsers import MultiPartParser
|
14 |
+
from drf_spectacular.utils import extend_schema
|
15 |
+
|
16 |
+
|
17 |
+
class RagasView(APIView):
|
18 |
+
parser_classes = [MultiPartParser]
|
19 |
+
|
20 |
+
@extend_schema(
|
21 |
+
request=RagasSerializer,
|
22 |
+
)
|
23 |
+
def post(self, request):
|
24 |
+
serializer = RagasSerializer(data=request.data)
|
25 |
+
print("\n\n\n")
|
26 |
+
print("\n\n\n")
|
27 |
+
print("serializer.data: ", serializer)
|
28 |
+
listaPDFs = []
|
29 |
+
if serializer.is_valid(raise_exception=True):
|
30 |
+
for file in serializer.validated_data["files"]:
|
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 |
+
|
46 |
+
result = test_ragas(serializer, listaPDFs)
|
47 |
+
|
48 |
+
for file in listaPDFs:
|
49 |
+
os.remove(file)
|
50 |
+
|
51 |
+
return Response({"msg": result})
|
52 |
+
|
53 |
+
|
54 |
+
class RagasFromTextView(APIView):
|
55 |
+
def post(self, request):
|
56 |
+
serializer = RagasFromTextSerializer(data=request.data)
|
57 |
+
if serializer.is_valid(raise_exception=True):
|
58 |
+
from datasets import Dataset
|
59 |
+
from ragas import evaluate
|
60 |
+
from ragas.metrics import (
|
61 |
+
faithfulness,
|
62 |
+
answer_relevancy,
|
63 |
+
answer_correctness,
|
64 |
+
context_precision,
|
65 |
+
context_recall,
|
66 |
+
)
|
67 |
+
import os
|
68 |
+
from datasets import load_dataset
|
69 |
+
import pandas as pd
|
70 |
+
|
71 |
+
os.environ.get("OPENAI_API_KEY")
|
72 |
+
|
73 |
+
df_pandas = pd.read_csv(
|
74 |
+
"D:/repositorios/projetos-pessoais/projeto-y-backend-hugginf-face-teste-01/vella-backend/_utils/files/ragas_testset.csv"
|
75 |
+
)
|
76 |
+
# print(df_pandas["position"]) # Print a specific column
|
77 |
+
data = {
|
78 |
+
"user_input": [
|
79 |
+
"What is the capital of France?",
|
80 |
+
],
|
81 |
+
"response": [],
|
82 |
+
"retrieved_contexts": [],
|
83 |
+
}
|
84 |
+
|
85 |
+
reference = [
|
86 |
+
"Paris is the capital of France. It is a major European city known for its culture."
|
87 |
+
]
|
88 |
+
|
89 |
+
for x in df_pandas["user_input"]:
|
90 |
+
data["user_input"].append(x)
|
91 |
+
|
92 |
+
for x in df_pandas["reference"]:
|
93 |
+
reference.append(x)
|
94 |
+
|
95 |
+
print("data: ", reference)
|
96 |
+
|
97 |
+
for i in range(len(reference)):
|
98 |
+
serializer.validated_data["user_message"] = data["user_input"][i]
|
99 |
+
resposta_llm = get_llm_summary_answer_by_cursor_complete(
|
100 |
+
serializer.validated_data, contexto=reference[i]
|
101 |
+
)
|
102 |
+
data["response"].append(resposta_llm["texto_completo"])
|
103 |
+
lista_reference_contexts = []
|
104 |
+
for x in resposta_llm["resultado"]:
|
105 |
+
lista_reference_contexts.append(x["source"]["text"])
|
106 |
+
data["retrieved_contexts"].append(lista_reference_contexts)
|
107 |
+
|
108 |
+
# Convert the data to a Hugging Face Dataset
|
109 |
+
dataset = Dataset.from_dict(data)
|
110 |
+
|
111 |
+
# Define the metrics you want to evaluate
|
112 |
+
metrics = [
|
113 |
+
faithfulness,
|
114 |
+
# answer_relevancy,
|
115 |
+
# answer_correctness,
|
116 |
+
# context_precision,
|
117 |
+
# context_recall,
|
118 |
+
]
|
119 |
+
|
120 |
+
# Evaluate the dataset using the selected metrics
|
121 |
+
results = evaluate(dataset, metrics)
|
122 |
+
|
123 |
+
# results.to_pandas().to_csv("./result.csv")
|
124 |
+
return Response({"resposta": results.to_pandas().to_string()})
|
setup/settings.py
CHANGED
@@ -26,7 +26,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
26 |
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
27 |
|
28 |
# SECURITY WARNING: keep the secret key used in production secret!
|
29 |
-
SECRET_KEY = os.environ.get(
|
30 |
|
31 |
# SECURITY WARNING: don't run with debug turned on in production!
|
32 |
DEBUG = True
|
@@ -51,6 +51,7 @@ INSTALLED_APPS = [
|
|
51 |
"drf_spectacular",
|
52 |
"resumos",
|
53 |
"modelos_usuarios",
|
|
|
54 |
]
|
55 |
|
56 |
MIDDLEWARE = [
|
@@ -62,10 +63,10 @@ MIDDLEWARE = [
|
|
62 |
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
63 |
"django.contrib.messages.middleware.MessageMiddleware",
|
64 |
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
65 |
-
"whitenoise.middleware.WhiteNoiseMiddleware"
|
66 |
]
|
67 |
|
68 |
-
STATICFILES_STORAGE =
|
69 |
|
70 |
ROOT_URLCONF = "setup.urls"
|
71 |
|
@@ -92,16 +93,16 @@ WSGI_APPLICATION = "setup.wsgi.application"
|
|
92 |
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
|
93 |
|
94 |
DATABASES = {
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
}
|
105 |
}
|
106 |
}
|
107 |
|
@@ -149,7 +150,7 @@ USE_TZ = True
|
|
149 |
# https://docs.djangoproject.com/en/4.2/howto/static-files/
|
150 |
|
151 |
STATIC_URL = "static/"
|
152 |
-
STATIC_ROOT = os.path.join(BASE_DIR,
|
153 |
|
154 |
# Default primary key field type
|
155 |
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
@@ -159,21 +160,25 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
|
159 |
CORS_ORIGIN_WHITELIST = [
|
160 |
"http://localhost",
|
161 |
"https://luanpoppe-projeto-y-teste-01.hf.space",
|
162 |
-
"https://*"
|
163 |
]
|
164 |
|
165 |
REST_FRAMEWORK = {
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
"DEFAULT_PARSER_CLASSES": [
|
|
|
|
|
|
|
|
|
170 |
}
|
171 |
|
172 |
SPECTACULAR_SETTINGS = {
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
# OTHER SETTINGS
|
179 |
}
|
|
|
26 |
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
27 |
|
28 |
# SECURITY WARNING: keep the secret key used in production secret!
|
29 |
+
SECRET_KEY = os.environ.get("SECRET_KEY")
|
30 |
|
31 |
# SECURITY WARNING: don't run with debug turned on in production!
|
32 |
DEBUG = True
|
|
|
51 |
"drf_spectacular",
|
52 |
"resumos",
|
53 |
"modelos_usuarios",
|
54 |
+
"ragas_api",
|
55 |
]
|
56 |
|
57 |
MIDDLEWARE = [
|
|
|
63 |
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
64 |
"django.contrib.messages.middleware.MessageMiddleware",
|
65 |
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
66 |
+
"whitenoise.middleware.WhiteNoiseMiddleware",
|
67 |
]
|
68 |
|
69 |
+
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
|
70 |
|
71 |
ROOT_URLCONF = "setup.urls"
|
72 |
|
|
|
93 |
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
|
94 |
|
95 |
DATABASES = {
|
96 |
+
"default": {
|
97 |
+
"ENGINE": "django.db.backends.postgresql",
|
98 |
+
"NAME": "vella",
|
99 |
+
"USER": "vella_owner",
|
100 |
+
"PASSWORD": os.environ.get("DATABASE_PASSWORD"),
|
101 |
+
"HOST": "ep-black-tooth-a510h8oe.us-east-2.aws.neon.tech",
|
102 |
+
"PORT": 5432,
|
103 |
+
"OPTIONS": {
|
104 |
+
"sslmode": "require",
|
105 |
+
},
|
106 |
}
|
107 |
}
|
108 |
|
|
|
150 |
# https://docs.djangoproject.com/en/4.2/howto/static-files/
|
151 |
|
152 |
STATIC_URL = "static/"
|
153 |
+
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
|
154 |
|
155 |
# Default primary key field type
|
156 |
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
|
|
160 |
CORS_ORIGIN_WHITELIST = [
|
161 |
"http://localhost",
|
162 |
"https://luanpoppe-projeto-y-teste-01.hf.space",
|
163 |
+
"https://*",
|
164 |
]
|
165 |
|
166 |
REST_FRAMEWORK = {
|
167 |
+
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
|
168 |
+
"PAGE_SIZE": 10,
|
169 |
+
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
|
170 |
+
"DEFAULT_PARSER_CLASSES": [
|
171 |
+
"rest_framework.parsers.JSONParser",
|
172 |
+
"rest_framework.parsers.MultiPartParser",
|
173 |
+
"rest_framework.parsers.FormParser",
|
174 |
+
],
|
175 |
}
|
176 |
|
177 |
SPECTACULAR_SETTINGS = {
|
178 |
+
"TITLE": "Your Project API",
|
179 |
+
"DESCRIPTION": "Your project description",
|
180 |
+
"VERSION": "1.0.0",
|
181 |
+
"SERVE_INCLUDE_SCHEMA": False,
|
182 |
+
"COMPONENT_SPLIT_REQUEST": True,
|
183 |
# OTHER SETTINGS
|
184 |
}
|
setup/urls.py
CHANGED
@@ -6,13 +6,16 @@ from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView
|
|
6 |
|
7 |
from pdfs.views import getPDF
|
8 |
from resumos.views import (
|
9 |
-
RagasFromTextView,
|
10 |
-
RagasView,
|
11 |
ResumoView,
|
12 |
ResumoSimplesCursorView,
|
13 |
ResumoSimplesCursorCompletoView,
|
14 |
)
|
15 |
-
from
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
router = routers.DefaultRouter()
|
18 |
# router.register("endpoint-teste", EndpointTesteViewSet, basename="Basename do endpoint-teste")
|
|
|
6 |
|
7 |
from pdfs.views import getPDF
|
8 |
from resumos.views import (
|
|
|
|
|
9 |
ResumoView,
|
10 |
ResumoSimplesCursorView,
|
11 |
ResumoSimplesCursorCompletoView,
|
12 |
)
|
13 |
+
from ragas_api.views import RagasFromTextView, RagasView
|
14 |
+
from modelos_usuarios.views import (
|
15 |
+
ListCreateModeloUsuarioView,
|
16 |
+
CreateUpdateDeleteModeloUsuarioView,
|
17 |
+
ListModelosPorUsuarioView,
|
18 |
+
)
|
19 |
|
20 |
router = routers.DefaultRouter()
|
21 |
# router.register("endpoint-teste", EndpointTesteViewSet, basename="Basename do endpoint-teste")
|