Spaces:
Sleeping
Sleeping
import logging | |
from typing import Dict, Any | |
from fpdf import FPDF | |
import xml.etree.ElementTree as ET | |
logger = logging.getLogger(__name__) | |
def generate_report(analysis_results: Dict[str, Any]) -> Dict[str, Any]: | |
""" | |
Generate a review report from the LLM's analysis results. | |
""" | |
report = { | |
"summary": {}, | |
"issues": [], | |
"recommendations": [], | |
"pdf_content": b"" | |
} | |
# Parse the XML output | |
raw_xml = analysis_results.get("raw_xml", "").strip() | |
if not raw_xml: | |
logger.error("Received empty or missing XML for report generation.") | |
return report | |
try: | |
root = ET.fromstring(raw_xml) | |
# Extract summary | |
summary_node = root.find("summary") | |
if summary_node is not None: | |
report["summary"] = { | |
"overall_assessment": summary_node.findtext("overall_assessment", "N/A"), | |
"total_issues": summary_node.findtext("total_issues", "N/A"), | |
"critical_issues": summary_node.findtext("critical_issues", "N/A"), | |
"warning_issues": summary_node.findtext("warning_issues", "N/A"), | |
} | |
# Extract issues | |
for issue_node in root.findall(".//issue"): | |
issue = { | |
"severity": issue_node.get("severity"), | |
"message": issue_node.findtext("message"), | |
"location": issue_node.findtext("location"), | |
"suggestion": issue_node.findtext("suggestion") | |
} | |
report["issues"].append(issue) | |
# Extract recommendations | |
for rec_node in root.findall(".//recommendation"): | |
report["recommendations"].append(rec_node.text) | |
except ET.ParseError as e: | |
logger.error(f"Failed to parse XML for report generation: {e}", exc_info=True) | |
# Handle XML parsing errors | |
pass | |
# Generate PDF report | |
pdf = FPDF() | |
pdf.add_page() | |
pdf.set_font("Arial", size=12) | |
pdf.cell(200, 10, txt="Dissertation Review Report", ln=True, align="C") | |
# TODO: Add more details to the PDF report | |
report["pdf_content"] = pdf.output(dest='S').encode('latin-1') | |
return report | |
if __name__ == "__main__": | |
# This module is intended to be imported, not run directly. | |
pass | |