-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
172 lines (149 loc) · 5.83 KB
/
Program.cs
File metadata and controls
172 lines (149 loc) · 5.83 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using Azure.Storage.Blobs;
using DocumentApi.Data.content;
using DocumentApi.Services;
using Microsoft.EntityFrameworkCore;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IDocumentClassificationProvider, DocumentClassificationProvider>();
string? blobConnectionString =
Environment.GetEnvironmentVariable("REVISA_BUCKET")
?? builder.Configuration.GetConnectionString("REVISA_BUCKET");
builder.Services.AddSingleton(x => new BlobServiceClient(blobConnectionString));
// Database config setup
string? connectionString =
Environment.GetEnvironmentVariable("REVISA_DB")
?? builder.Configuration.GetConnectionString("REVISA_DB");
Action<DbContextOptionsBuilder> dbConfig = (opt) =>
{
opt.UseSqlServer(connectionString);
// opt.EnableSensitiveDataLogging(true);
};
builder.Services.AddDbContext<ContentContext>(dbConfig);
builder.Services.AddScoped<IPdfSplitter, PdfSplitter>();
builder.Services.AddScoped<ITextExtractionProvider, TextExtractionProvider>();
builder.Services.AddScoped<IContentFieldService, ContentFieldService>();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.MapPost(
"/classify",
async Task<List<Document>> (string documentUri, IDocumentClassificationProvider provider) =>
{
var result = await provider.ClassifyDocumentAsync(documentUri);
return result
.Select(doc => new Document
{
DocType = doc.DocType,
StartPage = doc.StartPage,
EndPage = doc.EndPage
})
.ToList();
}
)
.WithOpenApi();
app.MapPost(
"/split",
async Task (
string documentUri,
List<string> desiredDocTypes,
IDocumentClassificationProvider provider,
IPdfSplitter splitter
) =>
{
var result = await provider.ClassifyDocumentAsync(documentUri);
await splitter.SplitPdfAsync(documentUri, desiredDocTypes, result);
}
)
.WithOpenApi();
app.MapPost(
"/extract/lessons",
async Task<List<DocumentFields>> (string path, ITextExtractionProvider provider) =>
{
var textResults = await provider.ExtractTextFromUrisAsync(
await provider.GetBlobsUrisAsync(path)
);
return
[
.. textResults.OrderBy(r =>
{
var lessonField = r.RawFields.FirstOrDefault(f =>
f.FieldName == "lessonNumber"
);
if (lessonField == null || string.IsNullOrEmpty(lessonField.FieldContentRaw))
{
return int.MaxValue; // Place records without a lesson number at the end
}
// Extract numeric value from FieldContentRaw
string numericContent = new string(
lessonField.FieldContentRaw.Where(char.IsDigit).ToArray()
);
if (int.TryParse(numericContent, out int lessonNumber))
{
lessonField.FieldContentRaw = lessonNumber.ToString(); // Reassign in normalized form
return lessonNumber;
}
return int.MaxValue; // If parsing fails, place it at the end
})
];
}
)
.WithOpenApi();
app.MapPost(
"/extract/module-overview",
async Task<List<DocumentFields>> (
string uri,
ITextExtractionProvider provider,
IContentFieldService contentFieldService
) =>
{
// Check if a record exists with the same SourceContentName
SourceContent existingSourceContent = await contentFieldService.GetSourceContentByPath(
uri
);
if (existingSourceContent != null)
{
// Return the existing record
return new List<DocumentFields>
{
new DocumentFields
{
RawFields = existingSourceContent
.SourceContentFields.Select(f => new FieldBase(
f.FieldName,
f.FieldContent
))
.ToList()
}
};
}
//extract from blob file
var results = await provider.ExtractTextFromUrisAsync(new List<Uri> { new Uri(uri) });
foreach (var result in results)
{
if (result.RawFields.Any(field => field.FieldName == "vocab"))
{
result.ModuleOverviewFields = result
.RawFields.Where(f => f.FieldName == "vocab")
.Select(f => new ModuleOverviewField(
"vocab",
f.FieldContentRaw.Split(" · ")
))
.ToList();
}
// Call PostModuleOverviewFields method
var postResult = await contentFieldService.PostModuleOverviewFields(
result.RawFields,
uri,
"EUREKA"
);
if (postResult != "success")
{
throw new Exception(postResult);
}
}
return results;
}
)
.WithOpenApi();
app.Run();