finalizare 1.0
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
@page "/doctor/ai"
|
||||
|
||||
|
||||
@attribute [Authorize(Roles = UserRoles.Doctor)]
|
||||
@layout DoctorLayout
|
||||
@inject IDoctorManagementService DoctorManagementService
|
||||
|
||||
<h3>AI Sickness Prediction</h3>
|
||||
|
||||
@if (!string.IsNullOrEmpty(statusMessage))
|
||||
{
|
||||
<div class="alert @(isSuccess ? "alert-success" : "alert-danger")" role="alert">
|
||||
@statusMessage
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="card mt-4">
|
||||
<div class="card-header">
|
||||
Upload PDF Document
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<InputFile OnChange="HandleFileSelected"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (predictions != null && predictions.Count > 0)
|
||||
{
|
||||
<div class="mt-4">
|
||||
<h4>Predictions:</h4>
|
||||
<ul>
|
||||
@foreach (var prediction in predictions)
|
||||
{
|
||||
<li>@prediction.Disease - Probability: @prediction.Probability%</li>
|
||||
}
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
private string statusMessage = string.Empty;
|
||||
private bool isSuccess = false;
|
||||
private List<PredictionResult> predictions;
|
||||
|
||||
private async Task HandleFileSelected(InputFileChangeEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var file = e.File;
|
||||
await using var stream = file.OpenReadStream();
|
||||
using var memoryStream = new MemoryStream();
|
||||
await stream.CopyToAsync(memoryStream);
|
||||
var fileContent = memoryStream.ToArray();
|
||||
|
||||
var text = await ExtractTextFromPdf(fileContent);
|
||||
Console.WriteLine(text);
|
||||
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
var result = await DoctorManagementService.GetSicknessPrediction(text);
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
predictions = result.Data;
|
||||
statusMessage = result.Message;
|
||||
isSuccess = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
statusMessage = "Failed to get predictions from the AI service.";
|
||||
isSuccess = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
statusMessage = "Failed to extract text from PDF.";
|
||||
isSuccess = false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statusMessage = $"Error: {ex.Message}";
|
||||
isSuccess = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> ExtractTextFromPdf(byte[] pdfBytes)
|
||||
{
|
||||
using var reader = new MemoryStream(pdfBytes);
|
||||
using var pdfReader = new PdfReader(reader);
|
||||
using var pdfDoc = new PdfDocument(pdfReader);
|
||||
var text = new StringBuilder();
|
||||
|
||||
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
|
||||
{
|
||||
var strategy = new SimpleTextExtractionStrategy();
|
||||
var pageText = PdfTextExtractor.GetTextFromPage(pdfDoc.GetPage(i), strategy);
|
||||
text.AppendLine(pageText);
|
||||
}
|
||||
|
||||
return text.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user