mirror of
https://github.com/thecyberlearn/quantum-ai-v2.git
synced 2026-08-18 14:12:59 +00:00
Optimize data analyzer agent for PDF-only processing
- Restrict file upload to PDF files only in frontend and backend - Add comprehensive PDF MIME type validation - Remove duplicate processing view and URL endpoint - Update model help text and UI labels for PDF-only - Clean up unused imports and code references - Maintain rich results formatting with HTML rendering - Preserve all existing webhook processing functionality 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
31d8929bcb
commit
b2fe16aeaa
@ -10,7 +10,11 @@ class DataAnalysisAgentRequest(BaseAgentRequest):
|
|||||||
"""Data Analysis Agent request tracking"""
|
"""Data Analysis Agent request tracking"""
|
||||||
|
|
||||||
# Agent-specific request fields
|
# Agent-specific request fields
|
||||||
data_file = models.FileField(upload_to='uploads/data_analyzer/', blank=True)
|
data_file = models.FileField(
|
||||||
|
upload_to='uploads/data_analyzer/',
|
||||||
|
blank=True,
|
||||||
|
help_text='PDF file for analysis'
|
||||||
|
)
|
||||||
analysis_type = models.CharField(
|
analysis_type = models.CharField(
|
||||||
max_length=50,
|
max_length=50,
|
||||||
choices=[
|
choices=[
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -5,7 +5,6 @@ app_name = 'data_analyzer'
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.data_analyzer_detail, name='detail'),
|
path('', views.data_analyzer_detail, name='detail'),
|
||||||
path('process/', views.DataAnalysisAgentProcessView.as_view(), name='process'),
|
|
||||||
path('status/<uuid:request_id>/', views.data_analyzer_status, name='status'),
|
path('status/<uuid:request_id>/', views.data_analyzer_status, name='status'),
|
||||||
path('result/<uuid:request_id>/', views.data_analyzer_result, name='result'),
|
path('result/<uuid:request_id>/', views.data_analyzer_result, name='result'),
|
||||||
]
|
]
|
||||||
@ -2,9 +2,6 @@ from django.shortcuts import render, redirect
|
|||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
|
||||||
from django.utils.decorators import method_decorator
|
|
||||||
from django.views import View
|
|
||||||
from agent_base.models import BaseAgent
|
from agent_base.models import BaseAgent
|
||||||
from .models import DataAnalysisAgentRequest, DataAnalysisAgentResponse
|
from .models import DataAnalysisAgentRequest, DataAnalysisAgentResponse
|
||||||
from .processor import DataAnalysisAgentProcessor
|
from .processor import DataAnalysisAgentProcessor
|
||||||
@ -34,10 +31,17 @@ def data_analyzer_detail(request):
|
|||||||
if not request.user.has_sufficient_balance(agent.price):
|
if not request.user.has_sufficient_balance(agent.price):
|
||||||
return JsonResponse({'error': 'Insufficient wallet balance'}, status=400)
|
return JsonResponse({'error': 'Insufficient wallet balance'}, status=400)
|
||||||
|
|
||||||
# Validate file upload
|
# Validate PDF file upload
|
||||||
data_file = files.get('file')
|
data_file = files.get('file')
|
||||||
if not data_file:
|
if not data_file:
|
||||||
return JsonResponse({'error': 'Data file is required'}, status=400)
|
return JsonResponse({'error': 'PDF file is required'}, status=400)
|
||||||
|
|
||||||
|
# Validate file type
|
||||||
|
if not data_file.name.lower().endswith('.pdf'):
|
||||||
|
return JsonResponse({'error': 'Only PDF files are supported'}, status=400)
|
||||||
|
|
||||||
|
if data_file.content_type != 'application/pdf':
|
||||||
|
return JsonResponse({'error': 'Invalid file type. Only PDF files are allowed'}, status=400)
|
||||||
|
|
||||||
# Create request object (no wallet deduction yet - only after successful processing)
|
# Create request object (no wallet deduction yet - only after successful processing)
|
||||||
agent_request = DataAnalysisAgentRequest.objects.create(
|
agent_request = DataAnalysisAgentRequest.objects.create(
|
||||||
@ -93,63 +97,6 @@ def data_analyzer_detail(request):
|
|||||||
return render(request, 'data_analyzer/detail.html', context)
|
return render(request, 'data_analyzer/detail.html', context)
|
||||||
|
|
||||||
|
|
||||||
@method_decorator(csrf_exempt, name='dispatch')
|
|
||||||
class DataAnalysisAgentProcessView(View):
|
|
||||||
"""Process Data Analysis Agent requests"""
|
|
||||||
|
|
||||||
def post(self, request):
|
|
||||||
if not request.user.is_authenticated:
|
|
||||||
return JsonResponse({'error': 'Authentication required'}, status=401)
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Handle multipart form data for file uploads
|
|
||||||
data = request.POST.dict()
|
|
||||||
files = request.FILES
|
|
||||||
|
|
||||||
# Get agent
|
|
||||||
agent = BaseAgent.objects.get(slug='data-analyzer')
|
|
||||||
|
|
||||||
# Check wallet balance
|
|
||||||
if not request.user.has_sufficient_balance(agent.price):
|
|
||||||
return JsonResponse({'error': 'Insufficient wallet balance'}, status=400)
|
|
||||||
|
|
||||||
# Validate file upload
|
|
||||||
data_file = files.get('file')
|
|
||||||
if not data_file:
|
|
||||||
return JsonResponse({'error': 'PDF file is required'}, status=400)
|
|
||||||
|
|
||||||
# Create request object (no wallet deduction yet - only after successful processing)
|
|
||||||
agent_request = DataAnalysisAgentRequest.objects.create(
|
|
||||||
user=request.user,
|
|
||||||
agent=agent,
|
|
||||||
cost=agent.price,
|
|
||||||
data_file=data_file,
|
|
||||||
analysis_type=data.get('analysis_type', 'summary'),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Process request
|
|
||||||
processor = DataAnalysisAgentProcessor()
|
|
||||||
result = processor.process_request(
|
|
||||||
request_obj=agent_request,
|
|
||||||
user_id=request.user.id,
|
|
||||||
data_file_url=agent_request.data_file.url if agent_request.data_file else '',
|
|
||||||
analysis_type=data.get('analysis_type', 'summary'),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Refresh user from database to get updated wallet balance
|
|
||||||
request.user.refresh_from_db()
|
|
||||||
|
|
||||||
return JsonResponse({
|
|
||||||
'success': True,
|
|
||||||
'request_id': str(agent_request.id),
|
|
||||||
'message': 'Data Analysis Agent request processed successfully',
|
|
||||||
'wallet_balance': float(request.user.wallet_balance)
|
|
||||||
})
|
|
||||||
|
|
||||||
except BaseAgent.DoesNotExist:
|
|
||||||
return JsonResponse({'error': 'Data Analysis Agent agent not found'}, status=404)
|
|
||||||
except Exception as e:
|
|
||||||
return JsonResponse({'error': str(e)}, status=500)
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user