Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video descriptions #423

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 56 additions & 9 deletions ClassTranscribeDatabase/CTDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public static string ConnectionStringBuilder()
// TODO: Max MaxPoolSize and port should be configurable

var configurations = CTDbContext.GetConfigurations();
return "Server=" + configurations["POSTGRES_SERVER_NAME"]
+ ";Port=5432"
+ ";Database=" + configurations["POSTGRES_DB"]
+ ";User Id=" + configurations["ADMIN_USER_ID"]
+ ";Password=" + configurations["ADMIN_PASSWORD"]
+ ";MaxPoolSize=1000;";
string conn = $"Server={configurations["POSTGRES_SERVER_NAME"]};"
+ $"Port={configurations["POSTGRES_SERVER_PORT"] ?? "5432"};"
+ $"Database={configurations["POSTGRES_DB"]};"
+ $"User Id={configurations["ADMIN_USER_ID"]};"
+ $"Password={configurations["ADMIN_PASSWORD"]};"
+ $"MaxPoolSize={configurations["POSTGRES_CLIENT_MAX_POOL_SIZE"] ?? "1000"};";
return conn;
}

/// <summary>
Expand Down Expand Up @@ -112,21 +113,67 @@ public static CTDbContext CreateDbContext()
/// <returns> The configurations </returns>
public static IConfiguration GetConfigurations()
{
var basedir = System.IO.Directory.GetCurrentDirectory();

if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable("POSTGRES_DB")))
{
LoadEnvFileIfExists($"{basedir}/../../../LocalEnvironmentVariables.txt");
}

var configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();

if (configuration.GetValue<string>("DEV_ENV", "NULL") != "DOCKER")
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string appSettingsFileName = "vs_appsettings.json";
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

if (File.Exists(path + Path.DirectorySeparatorChar + appSettingsFileName))
{
return new ConfigurationBuilder().SetBasePath(path).AddJsonFile(appSettingsFileName).Build();
}
}

}

return configuration;
}
public static string DropQuotes(string s)
{
char first = s.Length < 2 ? 'a': s[0];
if (! "\"'".Contains(first))
{
return s;
}
char last = s[^1];
if(first == last)
{
return s[1..^1].Replace($"\\{first}", first.ToString());
}
return s;
}
public static bool LoadEnvFileIfExists(string filePath)
{
if (!File.Exists(filePath)) {
Console.WriteLine($"Env file {filePath} not found - ignoring");
return false;
}
var count = 0;
foreach (var line in File.ReadAllLines(filePath))
{
if (!line.Contains("=") || line.TrimStart().StartsWith("#"))
continue;

var parts = line.Split( '=', 2);
var key = parts[0].Trim();
var val = DropQuotes( parts[1].Trim());
//Console.WriteLine($"{key}:{val.Length} chars");

Environment.SetEnvironmentVariable(key,val);
count += 1;
}
Console.WriteLine($"{count} environment variables set using {filePath}");
return true;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(ConnectionStringBuilder());
Expand Down Expand Up @@ -268,7 +315,7 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess)
return base.SaveChanges(acceptAllChangesOnSuccess);
}

public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
OnBeforeSaving();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
Expand Down
3 changes: 2 additions & 1 deletion ClassTranscribeDatabase/CaptionQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public async Task<List<Caption>> GetCaptionsAsync(string videoId, string languag
{
try
{
var transcriptionId = _context.Transcriptions.Where(t => t.Language == language && t.VideoId == videoId).First().Id;
var transcriptionId = _context.Transcriptions.Where(t => t.Language == language && t.VideoId == videoId
&& t.TranscriptionType == TranscriptionType.Caption).First().Id;
return await GetCaptionsAsync(transcriptionId);
}
catch (System.InvalidOperationException)
Expand Down
6 changes: 5 additions & 1 deletion ClassTranscribeDatabase/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public enum TaskType
BuildElasticIndex = 16,
ExampleTask = 17,
CleanUpElasticIndex = 18,
PythonCrawler = 19
PythonCrawler = 19,

DescribeVideo = 20,
DescribeImage = 21

}

public class Languages
Expand Down
1 change: 1 addition & 0 deletions ClassTranscribeDatabase/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class AppSettings
public string JWT_KEY { get; set; }
public string ALLOWED_HOSTS { get; set; }
public string POSTGRES_SERVER_NAME { get; set; }
public string POSTGRES_SERVER_PORT { get; set; } = "5432";
public string POSTGRES_DB { get; set; }

public string ADMIN_USER_ID { get; set; }
Expand Down
15 changes: 11 additions & 4 deletions ClassTranscribeDatabase/Models/Caption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ public enum CaptionType
TextCaption = 0,
AudioDescription = 1
}

public static class CaptionConstants {
public const string PlaceHolderText = "...Processing...";

}
/// <summary>
/// Each line of caption is stored as a row in the database.
/// </summary>
public class Caption : Entity
{

public bool HasPlaceHolderText() { return this.Text == CaptionConstants.PlaceHolderText; }
public int Index { get; set; }
public TimeSpan Begin { get; set; }
public TimeSpan End { get; set; }
Expand Down Expand Up @@ -115,7 +122,7 @@ public static List<Caption> ToCaptionEntitiesInterpolate(int captionsCount, Time
else
{
caption = tempCaption.Substring(0, index);
tempCaption = tempCaption.Substring(index);
tempCaption = tempCaption[index..];
tempCaption = tempCaption.Trim();
}
curEnd = curBegin.Add(new TimeSpan(0, 0, 0, 0, newDuration));
Expand All @@ -140,8 +147,8 @@ public static List<Caption> ToCaptionEntitiesInterpolate(int captionsCount, Time
End = curEnd,
Text = tempCaption
});
curBegin = curEnd;
curDuration = End.Subtract(curBegin);
// curBegin = curEnd;
// curDuration = End.Subtract(curBegin);
}
return captions;
}
Expand Down Expand Up @@ -204,7 +211,7 @@ public static string GenerateWebVTTString(List<Caption> captions, string languag
//
public static string GenerateDXFPString(List<Caption> captions, string language)
{
string now = DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture);
// string now = DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture);
string header = @"
<?xml version=""1.0"" encoding=""utf-8""?>
<tt xml:lang=""en"" xmlns=""http://www.w3.org/ns/ttml""
Expand Down
8 changes: 5 additions & 3 deletions ClassTranscribeDatabase/Models/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public ResourceType GetResourceType()
case Playlist _: return ResourceType.Playlist;
case Media _: return ResourceType.Media;
case EPub _: return ResourceType.EPub;
default:
break;
}
throw new InvalidOperationException("Invalid Type passed" + this);
}
Expand Down Expand Up @@ -558,22 +560,22 @@ public class TextData : Entity
public string? Text {get; set;}


public void setFromJSON(JToken? o) {
public void SetFromJSON(JToken? o) {
if(o == null) {
Text = null;
return;
}
Text = o.ToString(Newtonsoft.Json.Formatting.None);
}

public JToken? getAsJSON() {
public JToken? GetAsJSON() {
if( string.IsNullOrEmpty(Text)) {
return null;
}
return JToken.Parse(Text);
}

public JArray? getAsJArray()
public JArray? GetAsJArray()
{
if (string.IsNullOrEmpty(Text))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class Key
/// </summary>
public class KeyProvider
{
private AppSettings _appSettings;
private List<Key> Keys;
private HashSet<string> CurrentVideoIds;
private readonly AppSettings _appSettings;
private readonly List<Key> Keys;
private readonly HashSet<string> CurrentVideoIds;

public KeyProvider(AppSettings appSettings)
{
_appSettings = appSettings;
string subscriptionKeys = _appSettings.AZURE_SUBSCRIPTION_KEYS;
string subscriptionKeys = _appSettings.AZURE_SUBSCRIPTION_KEYS ?? "";
Keys = new List<Key>();
CurrentVideoIds = new HashSet<string>();

Expand Down
Loading