This commit is contained in:
Flatlogic Bot 2025-10-26 18:38:54 +00:00
parent 015faff9b1
commit 991acb14d9
2 changed files with 371 additions and 4 deletions

327
api/generate_report.php Normal file
View File

@ -0,0 +1,327 @@
<?php
header('Content-Type: application/json');
// --- Helper Functions for Report Generation ---
/**
* Generates a concise summary of the text.
*/
function generate_summary_report($text) {
$report = "<h1>AI-Generated Summary Report</h1>";
$report .= "<p>A high-level overview of the key points in the document.</p><br/>";
$sentences = preg_split('/(?<=[.?!])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);
$num_sentences = count($sentences);
$summary_sentences = [];
$sentences_to_take = 3;
if ($num_sentences <= ($sentences_to_take * 2)) {
$summary_sentences = $sentences;
} else {
$summary_sentences = array_slice($sentences, 0, $sentences_to_take);
$summary_sentences[] = "[...]";
$summary_sentences = array_merge($summary_sentences, array_slice($sentences, -$sentences_to_take));
}
$report .= "<h2>Key Points:</h2><ul>";
foreach ($summary_sentences as $sentence) {
$report .= "<li>" . htmlspecialchars(trim($sentence)) . "</li>";
}
$report .= "</ul>";
return $report;
}
/**
* Extracts sentences that appear to reference dates or events to create a timeline.
*/
function generate_timeline_report($text) {
$report = "<h1>AI-Generated Timeline of Events</h1>";
$report .= "<p>A chronological list of events and important dates mentioned in the document.</p><br/>";
// Regex to find sentences with dates (e.g., 25/12/2023, Dec 25, 2023) or time-related keywords.
$date_pattern = '/\b(\d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}|\w+\s\d{1,2},\s\d{4})\b/i';
$keyword_pattern = '/\b(on|at|before|after|during|later|yesterday|today|tomorrow|meanwhile)\b/i';
$sentences = preg_split('/(?<=[.?!])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);
$timeline_events = [];
foreach ($sentences as $sentence) {
if (preg_match($date_pattern, $sentence) || preg_match($keyword_pattern, $sentence)) {
$timeline_events[] = $sentence;
}
}
$report .= "<h2>Potential Events:</h2>";
if (empty($timeline_events)) {
$report .= "<p>No specific date or time-based events were detected by the AI.</p>";
} else {
$report .= "<ul>";
foreach ($timeline_events as $event) {
$report .= "<li>" . htmlspecialchars(trim($event)) . "</li>";
}
$report .= "</ul>";
}
return $report;
}
/**
* Extracts names of individuals based on common titles.
*/
function generate_persons_report($text) {
$report = "<h1>AI-Generated List of Key Individuals</h1>";
$report .= "<p>A list of individuals who may be relevant to the document.</p><br/>";
// Regex to find names with titles (Mr., Mrs., Dr., etc.) or potential full names.
$pattern = '/\b(Mr\.|Mrs\.|Ms\.|Dr\.|Prof\.)\s+[A-Z][a-z]+(\s+[A-Z][a-z]+)?/i';
preg_match_all($pattern, $text, $matches);
$persons = array_unique($matches[0]);
$report .= "<h2>Mentioned Individuals:</h2>";
if (empty($persons)) {
$report .= "<p>No individuals with titles (e.g., Mr., Dr.) were automatically detected.</p>";
} else {
$report .= "<ul>";
foreach ($persons as $person) {
$report .= "<li>" . htmlspecialchars(trim($person)) . "</li>";
}
$report .= "</ul>";
}
return $report;
}
/**
* Extracts a log of potential evidence markers.
*/
function generate_evidence_report($text) {
$report = "<h1>AI-Generated Evidence Log</h1>";
$report .= "<p>A log of items marked as potential evidence or exhibits.</p><br/>";
// Regex to find evidence-related keywords.
$pattern = '/\b(Exhibit|Appendix|Figure|Table|Attachment|Document|Photo|Video|Audio)\s+[A-Z0-9\-]+/i';
preg_match_all($pattern, $text, $matches);
$evidence_items = array_unique($matches[0]);
$report .= "<h2>Potential Evidence:</h2>";
if (empty($evidence_items)) {
$report .= "<p>No items matching standard evidence markers (e.g., 'Exhibit A', 'Document 123') were found.</p>";
} else {
$report .= "<ul>";
foreach ($evidence_items as $item) {
$report .= "<li>" . htmlspecialchars(trim($item)) . "</li>";
}
$report .= "</ul>";
}
return $report;
}
/**
* Generates a placeholder for a Case Diary Status Report.
*/
function generate_case_diary_report($text) {
$report = "<h1>Case Diary Status Report</h1>";
$report .= "<p>This is a placeholder for the Case Diary Status Report. Future development will populate this with relevant case diary entries from the text.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a placeholder for a Bail Reply.
*/
function generate_bail_reply_report($text) {
$report = "<h1>Bail Reply</h1>";
$report .= "<p>This is a placeholder for the Bail Reply. Future development will populate this with arguments and evidence against bail from the text.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a placeholder for a High Court Status Report.
*/
function generate_high_court_status_report($text) {
$report = "<h1>High Court Status Report</h1>";
$report .= "<p>This is a placeholder for the High Court Status Report. Future development will populate this with case status details for the High Court.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a placeholder for a Seizure Memo.
*/
function generate_seizure_memo_report($text) {
$report = "<h1>Seizure Memo</h1>";
$report .= "<p>This is a placeholder for the Seizure Memo. Future development will populate this with details of seized items from the text.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a placeholder for a Chargesheet.
*/
function generate_chargesheet_report($text) {
$report = "<h1>Chargesheet</h1>";
$report .= "<p>This is a placeholder for the Chargesheet. Future development will populate this with charges, evidence, and witness lists from the text.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a placeholder for a Disclosure Statement.
*/
function generate_disclosure_statement_report($text) {
$report = "<h1>Disclosure Statement</h1>";
$report .= "<p>This is a placeholder for the Disclosure Statement. Future development will populate this with disclosed information from the text.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a placeholder for an Exemption Request.
*/
function generate_exemption_request_report($text) {
$report = "<h1>Exemption Request</h1>";
$report .= "<p>This is a placeholder for the Exemption Request. Future development will populate this with reasons and justifications for exemption from the text.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a placeholder for an NBW (Non-Bailable Warrant).
*/
function generate_nbw_report($text) {
$report = "<h1>NBW (Non-Bailable Warrant)</h1>";
$report .= "<p>This is a placeholder for the NBW. Future development will populate this with details for the warrant application from the text.</p><br/>";
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
return $report;
}
/**
* Generates a report for a Reply for Release of RC.
*/
function generate_release_of_rc_reply_report($text) {
$report = "<h1>Reply for Release of RC</h1>";
// Extract details using regex
preg_match('/IN THE HONBLE COURT OF (.*)/', $text, $court);
preg_match('/FIR NO.: (.*)/', $text, $fir_no);
preg_match('/U\/S: (.*)/', $text, $us);
preg_match('/P.S.: (.*)/', $text, $ps);
preg_match('/State. vs. (.*)/', $text, $parties);
preg_match('/motorcycle bearing no. (.*?),/', $text, $vehicle_no);
preg_match('/applicant, (.*?)\./', $text, $applicant);
preg_match('/accused, (.*?)[,.]/', $text, $accused);
preg_match('/disposed of by this Hon'ble Court on (.*)\./', $text, $disposal_date);
$report .= "<p><strong>IN THE HONBLE COURT OF " . htmlspecialchars($court[1] ?? '...') . "</strong></p>";
$report .= "<p><strong>FIR NO.:</strong> " . htmlspecialchars($fir_no[1] ?? '...') . "</p>";
$report .= "<p><strong>U/S:</strong> " . htmlspecialchars($us[1] ?? '...') . "</p>";
$report .= "<p><strong>P.S.:</strong> " . htmlspecialchars($ps[1] ?? '...') . "</p>";
$report .= "<p><strong>In the matter of:</strong> State. vs. " . htmlspecialchars($parties[1] ?? '...') . "</p>";
$report .= "<hr>";
$report .= "<p><strong>Subject- Reply for Release of RC</strong></p>";
$report .= "<p>HONBLE SIR,</p>";
$report .= "<p>Most respectfully submitted that present application for the release of the registration certificate (RC) of motorcycle bearing no. <strong>" . htmlspecialchars($vehicle_no[1] ?? '...') . "</strong>, filed by the applicant, <strong>" . htmlspecialchars($applicant[1] ?? '...') . "</strong>.</p>";
$report .= "<p>The applicant stood as surety for the accused, <strong>" . htmlspecialchars($accused[1] ?? '...') . "</strong>, during the pendency of the case.</p>";
$report .= "<p>The case has been disposed of by this Hon'ble Court on <strong>" . htmlspecialchars($disposal_date[1] ?? '...') . "</strong>.</p>";
$report .= "<p>That no other appeal in this regard is pending in any other Honble Court or High Court.</p>";
$report .= "<p>That undersigned have no objection on release of RC.</p>";
$report .= "<p>Submitted Please.</p>";
return $report;
}
/**
* Generates a report for a Request for Cartridges.
*/
function generate_request_for_cartridges_report($text) {
$report = "<h1>Request for Cartridges</h1>";
// Extract details using regex
preg_match('/FIR NO . (.*)/', $text, $fir_no);
preg_match('/PS (.*) DELHI/', $text, $ps);
preg_match('/TO (.*)/', $text, $to);
preg_match('/provide (.*) for test firing/', $text, $cartridges);
preg_match('/sent to (.*) Vide/', $text, $fsl);
preg_match('/Letter No. (.*) Dated/', $text, $letter_no);
preg_match('/Dated (.*) for expert/', $text, $letter_date);
$report .= "<p><strong>FIR NO.:</strong> " . htmlspecialchars($fir_no[1] ?? '...') . "</p>";
$report .= "<p><strong>P.S.:</strong> " . htmlspecialchars($ps[1] ?? '...') . "</p>";
$report .= "<p><strong>TO:</strong> " . htmlspecialchars($to[1] ?? '...') . "</p>";
$report .= "<hr>";
$report .= "<p><strong>Subject- Request to provide " . htmlspecialchars($cartridges[1] ?? '...') . " for test firing</strong></p>";
$report .= "<p>Sir,</p>";
$report .= "<p>Most respectfully submitted that recovered arms and ammunition were sent to <strong>" . htmlspecialchars($fsl[1] ?? '...') . "</strong> Vide Letter No. <strong>" . htmlspecialchars($letter_no[1] ?? '...') . "</strong> Dated <strong>" . htmlspecialchars($letter_date[1] ?? '...') . "</strong> for expert opinion.</p>";
$report .= "<p>That FSL raised objection and asked to provide <strong>" . htmlspecialchars($cartridges[1] ?? '...') . "</strong> for Test firing.</p>";
$report .= "<p>Therefore it is requested that for further proceedings <strong>" . htmlspecialchars($cartridges[1] ?? '...') . "</strong> may kindly be provided.</p>";
$report .= "<p>Submitted please.</p>";
return $report;
}
// --- Main Logic ---
$data = json_decode(file_get_contents('php://input'), true);
$text = $data['text'] ?? '';
$template = $data['template'] ?? 'summary'; // Default to 'summary'
if (empty($text)) {
echo json_encode(['report' => 'No text provided to generate a report.']);
exit;
}
$final_report = '';
switch ($template) {
case 'timeline':
$final_report = generate_timeline_report($text);
break;
case 'persons':
$final_report = generate_persons_report($text);
break;
case 'evidence':
$final_report = generate_evidence_report($text);
break;
case 'case_diary':
$final_report = generate_case_diary_report($text);
break;
case 'bail_reply':
$final_report = generate_bail_reply_report($text);
break;
case 'high_court_status':
$final_report = generate_high_court_status_report($text);
break;
case 'seizure_memo':
$final_report = generate_seizure_memo_report($text);
break;
case 'chargesheet':
$final_report = generate_chargesheet_report($text);
break;
case 'disclosure_statement':
$final_report = generate_disclosure_statement_report($text);
break;
case 'exemption_request':
$final_report = generate_exemption_request_report($text);
break;
case 'nbw':
$final_report = generate_nbw_report($text);
break;
case 'release_of_rc_reply':
$final_report = generate_release_of_rc_reply_report($text);
break;
case 'request_for_cartridges':
$final_report = generate_request_for_cartridges_report($text);
break;
case 'summary':
default:
$final_report = generate_summary_report($text);
break;
}
$final_report .= "<br/><p><strong>--- End of Report ---</strong></p>";
// Return the report as JSON
echo json_encode(['report' => $final_report]);

View File

@ -28,6 +28,7 @@
const [fileContent, setFileContent] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [reportContent, setReportContent] = useState('AI-generated report will appear here.');
const [reportTemplate, setReportTemplate] = useState('summary');
const handleFileChange = (e) => {
const selectedFile = e.target.files[0];
@ -85,8 +86,22 @@
};
const generateReport = () => {
// Placeholder for AI report generation
setReportContent("Generating report... (This is a placeholder, AI integration is not yet implemented). Based on the document, the key points are...");
setReportContent('<div class="flex justify-center items-center h-full"><i class="fas fa-spinner fa-spin text-3xl text-indigo-500"></i></div>');
fetch('/api/generate_report.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: fileContent, template: reportTemplate }),
})
.then(response => response.json())
.then(data => {
setReportContent(data.report);
})
.catch(error => {
console.error('Error generating report:', error);
setReportContent('An error occurred while generating the report.');
});
};
const exportToPdf = () => {
@ -165,13 +180,38 @@
<div className="bg-white rounded-lg shadow p-4 flex flex-col">
<h3 className="text-lg font-semibold text-gray-700 mb-2">Generated Report</h3>
<div id="report-content-wrapper" className="flex-1 overflow-y-auto p-2 border rounded">
<p className="text-sm text-gray-600">{reportContent}</p>
<div className="text-sm text-gray-600" dangerouslySetInnerHTML={{ __html: reportContent }}></div>
</div>
</div>
</div>
{/* Action Buttons */}
<div className="mt-6 flex justify-end space-x-4">
<div className="mt-6 flex justify-end items-center space-x-4">
<div className="relative">
<select
value={reportTemplate}
onChange={(e) => setReportTemplate(e.target.value)}
className="appearance-none bg-white border border-gray-300 rounded-lg py-2 pl-4 pr-8 text-gray-700 focus:outline-none focus:ring-2 focus:ring-indigo-500"
>
<option value="summary">Case Summary</option>
<option value="timeline">Timeline of Events</option>
<option value="persons">Key Individuals</option>
<option value="evidence">Evidence Log</option>
<option value="case_diary">Case Diary Status Report</option>
<option value="bail_reply">Bail Reply</option>
<option value="high_court_status">High Court Status Report</option>
<option value="seizure_memo">Seizure Memo</option>
<option value="chargesheet">Chargesheet</option>
<option value="disclosure_statement">Disclosure Statement</option>
<option value="exemption_request">Exemption Request</option>
<option value="nbw">NBW</option>
<option value="release_of_rc_reply">Release of RC Reply</option>
<option value="request_for_cartridges">Request for Cartridges</option>
</select>
<div className="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
<i className="fas fa-chevron-down text-xs"></i>
</div>
</div>
<button onClick={generateReport} disabled={!fileContent} className="btn-gradient font-bold py-2 px-6 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed">
<i className="fas fa-magic mr-2"></i>
Generate Report