Initial commit

This commit is contained in:
2025-07-11 13:52:19 +00:00
commit e0c489f625
362 changed files with 27286 additions and 0 deletions
View File
Binary file not shown.
+139
View File
@@ -0,0 +1,139 @@
from fastapi import APIRouter, UploadFile, File, HTTPException
from typing import List, Optional, Dict
from pathlib import Path
import shutil
import tempfile
from pydantic import BaseModel
from modules.document_processor import DocumentProcessor
import os
class BatchConvertRequest(BaseModel):
directory: str
output_dir: Optional[str] = None
router = APIRouter()
doc_processor = DocumentProcessor()
@router.post("/convert-to-pdf")
async def convert_to_pdf(
files: List[UploadFile] = File(...),
output_format: str = "pdf"
):
"""
Convert uploaded documents to PDF format
"""
results = []
with tempfile.TemporaryDirectory() as temp_dir:
for file in files:
# Save uploaded file to temp directory
temp_file = Path(temp_dir) / file.filename
with temp_file.open("wb") as buffer:
shutil.copyfileobj(file.file, buffer)
try:
# Process the document
pdf_content = doc_processor.convert_to_pdf(temp_file)
results.append({
"filename": file.filename,
"converted_content": pdf_content,
"status": "success"
})
except Exception as e:
results.append({
"filename": file.filename,
"error": str(e),
"status": "error"
})
return results
@router.post("/batch-convert")
async def batch_convert(
directory: str,
output_format: str = "pdf"
):
"""
Convert all documents in a directory to PDF format
"""
try:
results = doc_processor.batch_convert_directory(directory)
return results
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/batch-convert-recursive")
async def batch_convert_recursive(request_data: BatchConvertRequest):
"""
Convert all documents in a directory and its subdirectories to PDF
"""
try:
directory_path = Path(request_data.directory)
if not directory_path.exists():
raise HTTPException(status_code=404, detail=f"Directory not found: {request_data.directory}")
output_path = None
if request_data.output_dir:
output_path = Path(request_data.output_dir)
output_path.mkdir(parents=True, exist_ok=True)
results = []
supported_extensions = doc_processor.supported_extensions.keys()
# Debug: Print processing info
print(f"Processing directory: {directory_path}")
print(f"Output directory: {output_path}")
print(f"Supported extensions: {list(supported_extensions)}")
# Count files before processing
all_files = []
for ext in supported_extensions:
all_files.extend(list(directory_path.rglob(f"*.{ext}")))
print(f"Found {len(all_files)} files to process")
# Recursively find all documents
for file_path in all_files:
try:
print(f"Processing: {file_path}")
# Convert the document
pdf_content = doc_processor.convert_to_pdf(file_path)
# Determine output path
if output_path:
# Preserve directory structure in output_dir
rel_path = file_path.relative_to(directory_path)
out_path = output_path / rel_path.with_suffix('.pdf')
out_path.parent.mkdir(parents=True, exist_ok=True)
else:
out_path = file_path.with_suffix('.pdf')
# Save the PDF
with open(out_path, 'wb') as f:
f.write(pdf_content)
results.append({
"source_file": str(file_path),
"output_file": str(out_path),
"status": "success"
})
print(f"Successfully converted: {file_path} -> {out_path}")
except Exception as e:
print(f"Error converting {file_path}: {str(e)}")
results.append({
"source_file": str(file_path),
"status": "error",
"error": str(e)
})
response_data = {
"total_files": len(results),
"successful": sum(1 for r in results if r["status"] == "success"),
"failed": sum(1 for r in results if r["status"] == "error"),
"results": results
}
print(f"Conversion complete: {response_data['successful']} successful, {response_data['failed']} failed")
return response_data
except Exception as e:
print(f"Error in batch conversion: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
+46
View File
@@ -0,0 +1,46 @@
from fastapi import APIRouter, UploadFile, File, HTTPException
from typing import Dict
from pathlib import Path
import shutil
import tempfile
from modules.pdf_utils import PDFUtils
router = APIRouter()
@router.post("/extract-text")
async def extract_text(
pdf_file: UploadFile = File(...)
):
"""
Extract text content from a PDF file
"""
try:
with tempfile.TemporaryDirectory() as temp_dir:
temp_file = Path(temp_dir) / pdf_file.filename
with temp_file.open("wb") as buffer:
shutil.copyfileobj(pdf_file.file, buffer)
text = PDFUtils.extract_text_from_pdf(temp_file)
return {"text": text}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/metadata")
async def get_metadata(
pdf_file: UploadFile = File(...)
):
"""
Get metadata from a PDF file
"""
try:
with tempfile.TemporaryDirectory() as temp_dir:
temp_file = Path(temp_dir) / pdf_file.filename
with temp_file.open("wb") as buffer:
shutil.copyfileobj(pdf_file.file, buffer)
metadata = PDFUtils.get_pdf_metadata(temp_file)
return metadata
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
+49
View File
@@ -0,0 +1,49 @@
from fastapi import APIRouter, UploadFile, File, HTTPException, Form
from typing import Dict
import json
from pathlib import Path
import shutil
import tempfile
from modules.test_analyzer import TestAnalyzer, TestAnalysis
from modules.pdf_utils import PDFUtils
router = APIRouter()
@router.post("/analyze", response_model=TestAnalysis)
async def analyze_test(
test_file: UploadFile = File(...),
marks_data: str = Form(...),
api_key: str = Form(...),
mode: str = Form('detailed')
):
"""
Analyze a test PDF and generate feedback based on marks data
"""
try:
print(f"Received request - Mode: {mode}")
marks_data_dict = json.loads(marks_data)
with tempfile.TemporaryDirectory() as temp_dir:
temp_file = Path(temp_dir) / test_file.filename
with temp_file.open("wb") as buffer:
shutil.copyfileobj(test_file.file, buffer)
print("File saved, initializing analyzer...")
analyzer = TestAnalyzer(api_key=api_key)
print("Extracting PDF content...")
pdf_utils = PDFUtils()
pdf_content = pdf_utils.extract_text_from_pdf(temp_file)
print("Analyzing content...")
analysis = analyzer.analyze_test(pdf_content, marks_data_dict, mode)
print("Analysis complete")
return analysis
except json.JSONDecodeError as e:
print(f"JSON decode error: {str(e)}")
raise HTTPException(status_code=422, detail=f"Invalid marks_data JSON format: {str(e)}")
except Exception as e:
print(f"Error in analyze_test: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
View File
+37
View File
@@ -0,0 +1,37 @@
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
import os
from modules.logger_tool import initialise_logger
logger = initialise_logger(__name__, os.getenv("LOG_LEVEL"), os.getenv("LOG_PATH"), 'default', True)
from fastapi import APIRouter
router = APIRouter()
@router.post("/run-pytest-timetable")
async def run_pytest_timetable():
import subprocess
home_dir = os.environ['HOME_DIR']
backend_test_dir = os.environ['BACKEND_TEST_DIR']
logger.debug(f"original home_dir: {home_dir}")
logger.debug(f"original backend_test_dir: {backend_test_dir}")
if backend_test_dir[0] != '/':
backend_test_dir = '/' + backend_test_dir
# Convert backslashes to forward slashes for Windows compatibility
home_dir = home_dir.replace('\\', '/')
backend_test_dir = backend_test_dir.replace('\\', '/')
logger.debug(f"new home_dir: {home_dir}")
logger.debug(f"new backend_test_dir: {backend_test_dir}")
# Join and normalize the path
pytest_dir = os.path.normpath(os.path.join(home_dir, backend_test_dir.lstrip('/'), "pytest_timetable.py"))
pytest_dir = pytest_dir.replace('\\', '/') # Ensure forward slashes
f_string = f"pytest {pytest_dir} --maxfail=1 --disable-warnings -q"
logger.debug(f"f_string: {f_string}")
result = subprocess.run(f_string, capture_output=True, text=True, shell=True)
logger.debug(f"result: {result}")
return {"stdout": result.stdout, "stderr": result.stderr}