-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from pavel-zhur/feature/dragon-images-direct
Feature/dragon images direct
- Loading branch information
Showing
8 changed files
with
183 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
OneShelf.OneDragon/OneShelf.OneDragon.Processor/Commands/Images.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
using OneShelf.Common.OpenAi.Services; | ||
using OneShelf.OneDragon.Database; | ||
using OneShelf.OneDragon.Database.Model.Enums; | ||
using OneShelf.OneDragon.Processor.Model; | ||
using OneShelf.OneDragon.Processor.Services; | ||
using OneShelf.Telegram.Model.CommandAttributes; | ||
using OneShelf.Telegram.Model.Ios; | ||
using OneShelf.Telegram.Services.Base; | ||
using Telegram.BotAPI; | ||
using Telegram.BotAPI.AvailableMethods; | ||
using Telegram.BotAPI.AvailableTypes; | ||
|
||
namespace OneShelf.OneDragon.Processor.Commands; | ||
|
||
[AdminCommand("images", "Картинки", "Сделать картинки по текстовому описанию")] | ||
public class Images : Command | ||
{ | ||
private readonly DragonDatabase _dragonDatabase; | ||
private readonly DragonScope _scope; | ||
private readonly Availability _availability; | ||
private readonly DialogRunner _dialogRunner; | ||
private readonly TelegramBotClient _api; | ||
|
||
public Images(Io io, DragonDatabase dragonDatabase, DragonScope scope, Availability availability, DialogRunner dialogRunner, IOptions<TelegramOptions> options) | ||
: base(io) | ||
{ | ||
_dragonDatabase = dragonDatabase; | ||
_scope = scope; | ||
_availability = availability; | ||
_dialogRunner = dialogRunner; | ||
_api = new(options.Value.Token); | ||
} | ||
|
||
protected override async Task ExecuteQuickly() | ||
{ | ||
var imagesUnavailableUntil = await _availability.GetImagesUnavailableUntil(DateTime.Now); | ||
if (imagesUnavailableUntil != null) | ||
{ | ||
Io.WriteLine($"Картинок нет до {imagesUnavailableUntil.Value:g} UTC"); | ||
|
||
_dragonDatabase.Interactions.Add(new() | ||
{ | ||
CreatedOn = DateTime.Now, | ||
ChatId = _scope.ChatId, | ||
UserId = Io.UserId, | ||
UpdateId = _scope.UpdateId, | ||
InteractionType = InteractionType.DirectImagesLimit, | ||
Serialized = "reset", | ||
}); | ||
|
||
return; | ||
} | ||
|
||
var query = Io.FreeChoice("Подробное описание:"); | ||
var count = Io.StrictChoice("Сколько?", int.Parse, new[] { "1", "2", "3", "4", "5" }); | ||
|
||
if (count is not (>= 1 and <= 5)) | ||
{ | ||
Io.WriteLine("Многовато или маловато."); | ||
return; | ||
} | ||
|
||
Io.WriteLine("Рисую..."); | ||
|
||
Scheduled(Background(query, count)); | ||
} | ||
|
||
private async Task Background(string query, int count) | ||
{ | ||
var aiParameters = await _dragonDatabase.AiParameters.SingleAsync(); | ||
var images = await _dialogRunner.GenerateImages(Enumerable.Repeat(query, count).ToList(), new() | ||
{ | ||
ImagesVersion = aiParameters.DalleVersion, | ||
UserId = _scope.UserId, | ||
DomainId = -1, | ||
Version = aiParameters.GptVersion, | ||
ChatId = _scope.ChatId, | ||
UseCase = "direct images", | ||
AdditionalBillingInfo = "one dragon", | ||
SystemMessage = "no message", | ||
}); | ||
|
||
_dragonDatabase.Interactions.Add(new() | ||
{ | ||
CreatedOn = DateTime.Now, | ||
ChatId = _scope.ChatId, | ||
UserId = Io.UserId, | ||
UpdateId = _scope.UpdateId, | ||
InteractionType = InteractionType.DirectImagesSuccess, | ||
Serialized = count.ToString(), | ||
ShortInfoSerialized = query, | ||
}); | ||
|
||
await _dragonDatabase.SaveChangesAsync(); | ||
|
||
await _api.SendMediaGroupAsync(new(_scope.ChatId, images.Select(x => new InputMediaPhoto(x.ToString()) {Caption = "1"}))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
OneShelf.OneDragon/OneShelf.OneDragon.Processor/Services/Availability.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using OneShelf.OneDragon.Database; | ||
using OneShelf.OneDragon.Database.Model.Enums; | ||
|
||
namespace OneShelf.OneDragon.Processor.Services; | ||
|
||
public class Availability(DragonDatabase dragonDatabase, DragonScope dragonScope) | ||
{ | ||
public async Task<DateTime?> GetImagesUnavailableUntil(DateTime now) | ||
{ | ||
var user = await dragonDatabase.Users.SingleAsync(x => x.Id == dragonScope.UserId); | ||
if (!user.UseLimits) return null; | ||
|
||
var limits = await dragonDatabase.Limits.Where(x => x.Images.HasValue).ToListAsync(); | ||
if (!limits.Any()) return null; | ||
|
||
DateTime Since(TimeSpan window) => now.Add(-window); | ||
|
||
var imagesSince = Since(limits.Max(x => x.Window)); | ||
var images = (await dragonDatabase.Interactions | ||
.Where(x => x.UserId == dragonScope.UserId && x.ChatId == dragonScope.ChatId) | ||
.Where(x => x.InteractionType == InteractionType.AiImagesSuccess) | ||
.Where(x => x.CreatedOn >= imagesSince) | ||
.ToListAsync()) | ||
.Select(x => (x.CreatedOn, count: int.Parse(x.Serialized))) | ||
.ToList(); | ||
|
||
DateTime? imagesUnavailableUntil = null; | ||
foreach (var limit in limits) | ||
{ | ||
if (images.Where(x => x.CreatedOn >= Since(limit.Window)).Sum(x => x.count) >= limit.Images!.Value) | ||
{ | ||
imagesUnavailableUntil ??= DateTime.MinValue; | ||
var value = images.Min(x => x.CreatedOn).Add(limit.Window); | ||
imagesUnavailableUntil = imagesUnavailableUntil > value ? imagesUnavailableUntil : value; | ||
} | ||
} | ||
|
||
return imagesUnavailableUntil; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters