#455 FillOriginalTextForAICreatedImagesForPoems

This commit is contained in:
Hamid Reza Mohammadi 2024-12-15 15:53:15 +03:30
parent a2d4abcf6e
commit 393119a42f
4 changed files with 84 additions and 0 deletions

View File

@ -2133,6 +2133,28 @@ namespace RMuseum.Controllers
}
}
/// <summary>
/// fill ai created images original text tag value
/// </summary>
/// <returns></returns>
[HttpPut("ai/create/filltag")]
[Authorize(Policy = RMuseumSecurableItem.ArtifactEntityShortName + ":" + SecurableItem.ModifyOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public IActionResult FillOriginalTextForAICreatedImagesForPoems()
{
try
{
_artifactService.FillOriginalTextForAICreatedImagesForPoems();
return Ok();
}
catch (Exception exp)
{
return BadRequest(exp.ToString());
}
}
/// <summary>
/// readonly mode
/// </summary>

View File

@ -643,6 +643,12 @@
<param name="startPoetId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.ArtifactController.FillOriginalTextForAICreatedImagesForPoems">
<summary>
fill ai created images original text tag value
</summary>
<returns></returns>
</member>
<member name="P:RMuseum.Controllers.ArtifactController.ReadOnlyMode">
<summary>
readonly mode
@ -15273,6 +15279,11 @@
<param name="startPoetId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IArtifactService.FillOriginalTextForAICreatedImagesForPoems">
<summary>
fill ai created images original text tag value
</summary>
</member>
<member name="T:RMuseum.Services.IDonationService">
<summary>
donation service

View File

@ -606,5 +606,10 @@ namespace RMuseum.Services
/// <returns></returns>
Task OpenAIStartCreatingImagesForPoemsAsync(int startPoetId);
/// <summary>
/// fill ai created images original text tag value
/// </summary>
void FillOriginalTextForAICreatedImagesForPoems();
}
}

View File

@ -479,5 +479,51 @@ namespace RMuseum.Services.Implementation
return new RServiceResult<bool>(false, exp.ToString());
}
}
public void FillOriginalTextForAICreatedImagesForPoems()
{
_backgroundTaskQueue.QueueBackgroundWorkItem
(
async token =>
{
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC
{
LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context);
var job = (await jobProgressServiceEF.NewJob($"FillOriginalTextForAICreatedImagesForPoems", "Query data")).Result;
try
{
string friendlyUrl = "ai";
RArtifactMasterRecord book = await context.Artifacts.AsNoTracking().Where(b => b.FriendlyUrl == friendlyUrl).SingleAsync();
var ganjoorLinkTag = await context.Tags.AsNoTracking().Where(t => t.NameInEnglish == "Ganjoor Link").SingleAsync();
var originalTextTag = await context.Tags.AsNoTracking().Where(t => t.NameInEnglish == "Original Text").SingleAsync();
var items = await context.Items.AsNoTracking().Include(i => i.Tags).Where(i => i.RArtifactMasterRecordId == book.Id).ToListAsync();
foreach (var item in items)
{
if (!item.Tags.Where(t => t.RTagId == originalTextTag.Id).Any())
{
var ganjoorLink = item.Tags.Where(t => t.RTagId == ganjoorLinkTag.Id).FirstOrDefault();
if (ganjoorLink != null)
{
string url = ganjoorLink.ValueSupplement.Replace("https://ganjoor.net", "");
var poem = await context.GanjoorPoems.AsNoTracking().Where(p => p.FullUrl == url).SingleOrDefaultAsync();
var poemTextTag = await TagHandler.PrepareAttribute(context, "Original Text", poem.PlainText, 1);
RArtifactItemRecord page = await context.Items.Include(i => i.Tags).Where(i => i.Id == item.Id).SingleAsync();
page.Tags.Add(poemTextTag);
context.Update(page);
await jobProgressServiceEF.UpdateJob(job.Id, item.Order);
}
}
}
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
}
catch (Exception exp)
{
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
}
}
}
);
}
}
}