-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
20 changed files
with
913 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# El archivo admin.py se utiliza para hacer que tus modelos sean accesibles a través del sitio | ||
# de administración de Django. Esto es muy útil para crear, leer, actualizar y eliminar (CRUD) | ||
# registros de tus modelos directamente desde una interfaz web generada automáticamente por Django | ||
from django.contrib import admin | ||
from .models import Documento, Template # Asegúrate de importar los modelos correctamente | ||
# Si deseas personalizar la interfaz de admin, puedes definir clases ModelAdmin | ||
@admin.register(Documento) # Decorador para registrar el modelo con la clase ModelAdmin personalizada | ||
class DocumentoAdmin(admin.ModelAdmin): | ||
list_display = ('titulo', 'usuario', 'fecha_creacion') # Ejemplo de personalización | ||
search_fields = ('titulo', 'contenido') # Permite buscar por título y contenido | ||
|
||
@admin.register(Template) # Decorador para registrar el modelo con la clase ModelAdmin personalizada | ||
class TemplateAdmin(admin.ModelAdmin): | ||
list_display = ('title', 'fecha_creacion') | ||
search_fields = ('title', 'content') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ApplegalConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'AppLegal' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Este archivo se encarga de las utilidades que se pueden utilizar para extraer texto de archivos PDF y DOCX. | ||
import fitz # PyMuPDF | ||
|
||
def extract_text_from_pdf(pdf_file): | ||
doc = fitz.open(stream=pdf_file.read(), filetype="pdf") | ||
text = "" | ||
for page in doc: | ||
text += page.get_text() | ||
return text | ||
|
||
from docx import Document | ||
|
||
def extract_text_from_docx(docx_path): | ||
doc = Document(docx_path) | ||
text = "" | ||
for para in doc.paragraphs: | ||
text += para.text + "\n" | ||
return text | ||
|
||
"""def extract_text_from_doc(doc_path): | ||
doc = fitz.open(doc_path) | ||
text = "" | ||
for page in doc: | ||
text += page.get_text() | ||
return text | ||
""" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Aqui se definen los modelos de la aplicación AppLegal. | ||
# Los modelos son clases que representan tablas en la base de datos. | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
from django.db import models | ||
class Documento(models.Model): # Define un modelo llamado Documento que hereda de models.Model | ||
titulo = models.CharField(max_length=255) # Define un campo de texto llamado titulo | ||
archivo = models.FileField(upload_to='documentos/', default='documentos/default.txt') # Define un campo de archivo llamado archivo | ||
contenido = models.TextField() # Define un campo de texto llamado contenido | ||
fecha_creacion = models.DateTimeField(auto_now_add=True) # Define un campo de fecha y hora llamado fecha_creacion | ||
fecha_modificacion = models.DateTimeField(auto_now=True) # Define un campo de fecha y hora llamado fecha_modificacion | ||
usuario = models.ForeignKey(User, related_name='documentos', on_delete=models.CASCADE) # Establece una relación de clave foránea con el modelo User, indicando quién creó el documento. | ||
#usuario = models.ForeignKey(User, on_delete=models.CASCADE) # establece una relación de clave foránea con el modelo User, indicando quién creó el documento. | ||
|
||
def __str__(self): # Define un método __str__ que devuelve el título del documento. | ||
return self.titulo | ||
|
||
class Template(models.Model): | ||
id = models.AutoField(primary_key=True) | ||
title = models.CharField(max_length=255) | ||
content = models.TextField() | ||
usuario = models.ForeignKey(User, related_name='templates', on_delete=models.CASCADE, null=True, blank=True) # Opcional, basado en tus necesidades | ||
fecha_creacion = models.DateTimeField(auto_now_add=True) | ||
fecha_modificacion = models.DateTimeField(auto_now=True) | ||
# Omitido: Implementación de categorías o etiquetas para simplicidad | ||
def __str__(self): | ||
return self.title | ||
|
||
class FileU(models.Model): | ||
file = models.FileField(upload_to='uploads/%Y/%m/%d/') | ||
uploaded_at = models.DateTimeField(auto_now_add=True) | ||
usuario = models.ForeignKey(User, related_name='uploaded_files', on_delete=models.CASCADE, null=True, blank=True) # Relaciona cada archivo con un usuario | ||
descripcion = models.TextField(null=True, blank=True) # Campo opcional para describir el archivo | ||
etiquetas = models.CharField(max_length=255, blank=True) # Etiquetas para mejorar la búsqueda de archivos | ||
|
||
def __str__(self): | ||
return f"{self.file.name} ({self.usuario.username})" | ||
|
||
# modelo para almacenar los mensajes de chat | ||
class Message(models.Model): | ||
user = models.ForeignKey(User, related_name='messages', on_delete=models.CASCADE) | ||
content = models.TextField() | ||
timestamp = models.DateTimeField(auto_now_add=True) | ||
|
||
def __str__(self): | ||
return self.user.username + ": " + self.content[:20] | ||
|
||
|
||
"""class User(models.Model): # Define un modelo llamado User que hereda de models.Model | ||
username = models.CharField(max_length=100) # Define un campo de texto llamado username | ||
email = models.EmailField() # Define un campo de correo electrónico llamado email | ||
password = models.CharField(max_length=20) # Define un campo de texto llamado password | ||
is_active = models.BooleanField(default=True) | ||
is_staff = models.BooleanField(default=False) | ||
is_superuser = models.BooleanField(default=False) | ||
date_joined = models.DateTimeField(auto_now_add=True) | ||
def __str__(self): # Define un método __str__ que devuelve el nombre de usuario. | ||
return self.username | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Este archivo contiene las funciones que se encargan de interactuar con los modelos de transformers para generar texto, resumir texto y clasificar texto. | ||
from transformers import AutoModelForTokenClassification, AutoTokenizer,pipeline | ||
import torch | ||
from transformers import pipeline | ||
from transformers import AutoTokenizer, AutoModelForTokenClassification | ||
|
||
# Inicializamos las pipelines de transformers aquí para reutilización | ||
generator = pipeline('text-generation', model='sshleifer/distilbart-cnn-12-6', revision='a4f8f3e') | ||
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | ||
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") | ||
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2", tokenizer="deepset/roberta-base-squad2") | ||
|
||
def generate_text(prompt): | ||
return generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text'] | ||
|
||
#def summarize_text(document_text): | ||
# return summarizer(document_text, max_length=150, min_length=30, do_sample=False)[0]['summary_text'] | ||
def summarize_text(text, max_length=130, min_length=30): | ||
#summarizer = get_summarizer() | ||
summary = summarizer(text, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text'] | ||
return summary | ||
|
||
|
||
def classify_text(text, labels): | ||
return classifier(text, candidate_labels=labels) | ||
|
||
def answer_question(question, context_text): | ||
result = qa_pipeline(question=question, context=context_text) | ||
return result['answer'] | ||
|
||
def get_ner_pipeline(): | ||
model_name = "dccuchile/bert-base-spanish-wwm-cased" # Modelo específico para español | ||
tokenizer = AutoTokenizer.from_pretrained(model_name) | ||
model = AutoModelForTokenClassification.from_pretrained(model_name) | ||
|
||
# Configura la pipeline de NER | ||
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple") | ||
return ner_pipeline | ||
|
||
def extract_entities(text): | ||
ner_pipeline = get_ner_pipeline() | ||
entities = ner_pipeline(text) | ||
return entities | ||
|
||
def get_translation_pipeline(): | ||
model_name = "Helsinki-NLP/opus-mt-es-en" # Modelo para traducción de español a inglés | ||
translation_pipeline = pipeline("translation_es_to_en", model=model_name) | ||
return translation_pipeline | ||
|
||
def translate_text(text, target_language='en'): | ||
translation_pipeline = get_translation_pipeline() | ||
translation = translation_pipeline(text, max_length=512)[0]['translation_text'] | ||
return translation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from rest_framework import permissions | ||
from rest_framework.permissions import BasePermission | ||
from AppLegal.permissions import IsAuthorOrReadOnly | ||
|
||
class IsAuthorOrReadOnly(permissions.BasePermission): | ||
|
||
#Permiso personalizado para permitir solo a los autores de un objeto editarlo. | ||
|
||
def has_object_permission(self, request, view, obj): | ||
# Los permisos de lectura están permitidos para cualquier solicitud, | ||
# por lo que siempre permitiremos solicitudes GET, HEAD o OPTIONS. | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
# Los permisos de escritura solo están permitidos al autor del objeto. | ||
return obj.author == request.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from rest_framework import serializers | ||
from .models import Documento, Template, FileU | ||
from django.contrib.auth.models import User | ||
#from rest_framework import permissions | ||
from .models import Template | ||
from .models import Message | ||
|
||
class MessageSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Message | ||
fields = '__all__' | ||
|
||
|
||
class DocumentoSerializer(serializers.ModelSerializer): # Define una clase DocumentoSerializer que hereda de serializers.ModelSerializer | ||
|
||
fecha_creacion = serializers.DateTimeField(format="%Y-%m-%dT%H:%M:%S.%fZ") | ||
fecha_modificacion = serializers.DateTimeField(format="%Y-%m-%dT%H:%M:%S.%fZ") | ||
class Meta: # Define una clase Meta que contiene la información del modelo Documento | ||
model = Documento | ||
fields = ['id', 'titulo', 'contenido', 'usuario', 'fecha_creacion', 'fecha_modificacion'] # Define los campos que se incluirán en la serialización | ||
|
||
# Para evitar que se repitan los títulos de los documentos, puedes agregar una validación personalizada al campo título: | ||
def validate_titulo(self, value): | ||
if not value: | ||
raise serializers.ValidationError("El título no puede estar vacío.") | ||
if Documento.objects.filter(titulo=value).exists(): | ||
raise serializers.ValidationError("Un documento con este título ya existe.") | ||
return value | ||
|
||
class UserSerializer(serializers.ModelSerializer): # Define una clase UserSerializer que hereda de serializers.ModelSerializer | ||
documentos = serializers.PrimaryKeyRelatedField(many=True, queryset=Documento.objects.all()) # Define un campo documentos que establece una relación de clave foránea con el modelo Documento | ||
#documentos = serializers.PrimaryKeyRelatedField(many=True, read_only=True) | ||
class Meta: | ||
model = User | ||
fields = ['id', 'username', 'documentos'] # fields = ['id', 'username', 'email'] # Ajusta según lo que quieras exponer | ||
|
||
"""Define un serializador para tu modelo de documento que incluya el archivo""" | ||
class FileSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Documento # Podría ser FileU si deseas exponer los archivos subidos | ||
fields = ('titulo', 'archivo') | ||
|
||
class TemplateSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Template | ||
fields = ['id', 'title', 'content'] | ||
|
||
class FileUploadSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = FileU | ||
fields = ['file', 'title', 'content'] | ||
|
||
file = serializers.FileField() # Define un campo file que será un archivo | ||
title = serializers.CharField(max_length=100) # Define un campo title que será una cadena de texto | ||
content = serializers.CharField() # Define un campo content que será una cadena de texto | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from django.urls import path, include # Importamos las funciones path e include. | ||
from rest_framework.routers import DefaultRouter | ||
from .views import ( | ||
DocumentInteractionView, DocumentoViewSet, UserViewSet, TemplateListViewSet, | ||
FileUploadViewSet, GenerateTextView, DocumentSummaryView, | ||
TextClassificationView, ChatView) | ||
from rest_framework.documentation import include_docs_urls # Importamos la función include_docs_urls. | ||
|
||
# Aquí estamos creando un objeto router que nos permitirá generar las rutas de forma automática. | ||
router = DefaultRouter() # Creamos un objeto router. | ||
router.register(r'documentos', DocumentoViewSet) # Registramos la ruta /documentos/ para el viewset DocumentoViewSet. | ||
router.register(r'usuarios', UserViewSet) # Registramos la ruta /usuarios/ para el viewset UserViewSet. | ||
router.register(r'templates', TemplateListViewSet, basename='template') # Registramos la ruta /templates/ para el viewset TemplateListViewSet. | ||
router.register(r'upload', FileUploadViewSet, basename='file-upload') # Registramos la ruta /upload/ para el viewset FileUploadViewSet. | ||
#router.register(r'chat', ChatView, basename='chat') # Registramos la ruta /chat/ para el viewset ChatRoomView. | ||
#router.register(r'messages', ChatMessageView, basename='chat-message') # Registramos la ruta /messages/ para el viewset ChatMessageView. | ||
|
||
# Aquí estamos extendiendo urlpatterns con las nuevas rutas. | ||
urlpatterns = [ | ||
path('', include(router.urls)), # Añadimos las rutas generadas por el router. | ||
path('home/', include(router.urls)), # Añadimos la ruta /home/ para que sea un alias de /. | ||
path('chat/', ChatView.as_view(), name='chat'), | ||
path('generate-text/', GenerateTextView.as_view(), name='generate-text'), # Añadimos la ruta /generate-text/ para la vista GenerateTextView. | ||
path('summarize-document/', DocumentSummaryView.as_view(), name='summarize-document'), # Añadimos la ruta /summarize-document/ para la vista DocumentSummaryView. | ||
path('classify-text/', TextClassificationView.as_view(), name='classify-text'), # Añadimos la ruta /classify-text/ para la vista TextClassificationView. | ||
path('doc/', include_docs_urls(title='Documentación de la API')), # Añadimos la ruta /doc/ para la documentación de la API. | ||
path('interactive-doc/', DocumentInteractionView.as_view(), name='interactive-doc'), # Añadimos la ruta /interactive-doc/ para la vista DocumentInteractiveView. | ||
] |
Oops, something went wrong.