-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSummarizeController.cs
More file actions
131 lines (116 loc) · 5.63 KB
/
SummarizeController.cs
File metadata and controls
131 lines (116 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using DevExpress.AIIntegration;
using DevExpress.AIIntegration.Docs;
using DevExpress.Docs.Presentation;
using DevExpress.Pdf;
using DevExpress.XtraRichEdit;
using Microsoft.AspNetCore.Mvc;
using RichEditOpenAIWebApi.BusinessObjects;
using Swashbuckle.AspNetCore.Annotations;
using System.Net;
namespace RichEditOpenAIWebApi.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class SummarizeController : ControllerBase
{
private readonly IAIDocProcessingService docProcessingService;
public SummarizeController(IAIDocProcessingService docService)
{
docProcessingService = docService;
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> SummarizeWordDocument(IFormFile document, [FromQuery] SummarizationMode summarizationMode, [FromQuery] RichEditDocumentPart part)
{
try
{
using (var wordProcessor = new RichEditDocumentServer())
{
await RichEditHelper.LoadFile(wordProcessor, document);
string summaryText = string.Empty;
switch (part)
{
case RichEditDocumentPart.FirstPage:
var fixedRange = wordProcessor.DocumentLayout.GetPage(0).MainContentRange;
var pageRange = wordProcessor.Document.CreateRange(fixedRange.Start, fixedRange.Length);
summaryText = await docProcessingService.SummarizeAsync(pageRange, summarizationMode);
break;
case RichEditDocumentPart.FirstSection:
var sectionRange = wordProcessor.Document.Sections[0].Range;
summaryText = await docProcessingService.SummarizeAsync(sectionRange, summarizationMode);
break;
case RichEditDocumentPart.WholeDocument:
summaryText = await docProcessingService.SummarizeAsync(wordProcessor, summarizationMode);
break;
}
return Content(summaryText ?? string.Empty, "text/plain");
}
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> SummarizePdfDocument(IFormFile file, [FromQuery] SummarizationMode summarizationMode, [FromQuery] PdfPart pdfPart)
{
string summaryText = string.Empty;
try
{
using (var pdfDocumentProcessor = new PdfDocumentProcessor())
{
await PdfHelper.LoadPdfDocument(pdfDocumentProcessor, file);
switch (pdfPart)
{
case PdfPart.FirstPage:
var firstPageBox = pdfDocumentProcessor.Document.Pages[0].CropBox;
PdfDocumentPosition pagePosition1 = new PdfDocumentPosition(1, firstPageBox.TopLeft);
PdfDocumentPosition pagePosition2 = new PdfDocumentPosition(1, firstPageBox.BottomRight);
var pageArea = PdfDocumentArea.Create(pagePosition1, pagePosition2);
summaryText = await docProcessingService.SummarizeAsync(pdfDocumentProcessor, pageArea, summarizationMode);
break;
case PdfPart.WholeDocument:
summaryText = await docProcessingService.SummarizeAsync(pdfDocumentProcessor, summarizationMode);
break;
default:
summaryText = string.Empty; // Ensure summaryText is assigned for all cases
break;
}
}
return Content(summaryText ?? string.Empty, "text/plain");
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> SummarizePresentation(IFormFile file, [FromQuery] SummarizationMode summarizationMode, [FromQuery] PresentationPart presentationPart)
{
string summaryText = string.Empty;
try
{
Presentation presentation = await PresentationHelper.LoadPresentation(file);
switch (presentationPart)
{
case PresentationPart.FirstSlide:
var slide = presentation.Slides[0];
summaryText = await docProcessingService.SummarizeAsync(slide, summarizationMode);
break;
case PresentationPart.WholePresentation:
summaryText = await docProcessingService.SummarizeAsync(presentation, summarizationMode);
break;
default:
break;
}
return Content(summaryText ?? string.Empty, "text/plain");
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
}
}