Skip to content

Commit 1c74f04

Browse files
authored
Merge pull request #423 from classtranscribe/VideoDescriptions
Video descriptions
2 parents 198ae63 + d9d9e89 commit 1c74f04

31 files changed

+984
-317
lines changed

ClassTranscribeDatabase/CTDbContext.cs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ public static string ConnectionStringBuilder()
7474
// TODO: Max MaxPoolSize and port should be configurable
7575

7676
var configurations = CTDbContext.GetConfigurations();
77-
return "Server=" + configurations["POSTGRES_SERVER_NAME"]
78-
+ ";Port=5432"
79-
+ ";Database=" + configurations["POSTGRES_DB"]
80-
+ ";User Id=" + configurations["ADMIN_USER_ID"]
81-
+ ";Password=" + configurations["ADMIN_PASSWORD"]
82-
+ ";MaxPoolSize=1000;";
77+
string conn = $"Server={configurations["POSTGRES_SERVER_NAME"]};"
78+
+ $"Port={configurations["POSTGRES_SERVER_PORT"] ?? "5432"};"
79+
+ $"Database={configurations["POSTGRES_DB"]};"
80+
+ $"User Id={configurations["ADMIN_USER_ID"]};"
81+
+ $"Password={configurations["ADMIN_PASSWORD"]};"
82+
+ $"MaxPoolSize={configurations["POSTGRES_CLIENT_MAX_POOL_SIZE"] ?? "1000"};";
83+
return conn;
8384
}
8485

8586
/// <summary>
@@ -112,21 +113,67 @@ public static CTDbContext CreateDbContext()
112113
/// <returns> The configurations </returns>
113114
public static IConfiguration GetConfigurations()
114115
{
116+
var basedir = System.IO.Directory.GetCurrentDirectory();
117+
118+
if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable("POSTGRES_DB")))
119+
{
120+
LoadEnvFileIfExists($"{basedir}/../../../LocalEnvironmentVariables.txt");
121+
}
122+
115123
var configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();
116124

117125
if (configuration.GetValue<string>("DEV_ENV", "NULL") != "DOCKER")
118126
{
119-
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
120127
string appSettingsFileName = "vs_appsettings.json";
128+
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
121129

122130
if (File.Exists(path + Path.DirectorySeparatorChar + appSettingsFileName))
123131
{
124132
return new ConfigurationBuilder().SetBasePath(path).AddJsonFile(appSettingsFileName).Build();
125-
}
133+
}
134+
126135
}
127136

128137
return configuration;
129138
}
139+
public static string DropQuotes(string s)
140+
{
141+
char first = s.Length < 2 ? 'a': s[0];
142+
if (! "\"'".Contains(first))
143+
{
144+
return s;
145+
}
146+
char last = s[^1];
147+
if(first == last)
148+
{
149+
return s[1..^1].Replace($"\\{first}", first.ToString());
150+
}
151+
return s;
152+
}
153+
public static bool LoadEnvFileIfExists(string filePath)
154+
{
155+
if (!File.Exists(filePath)) {
156+
Console.WriteLine($"Env file {filePath} not found - ignoring");
157+
return false;
158+
}
159+
var count = 0;
160+
foreach (var line in File.ReadAllLines(filePath))
161+
{
162+
if (!line.Contains("=") || line.TrimStart().StartsWith("#"))
163+
continue;
164+
165+
var parts = line.Split( '=', 2);
166+
var key = parts[0].Trim();
167+
var val = DropQuotes( parts[1].Trim());
168+
//Console.WriteLine($"{key}:{val.Length} chars");
169+
170+
Environment.SetEnvironmentVariable(key,val);
171+
count += 1;
172+
}
173+
Console.WriteLine($"{count} environment variables set using {filePath}");
174+
return true;
175+
}
176+
130177
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
131178
{
132179
optionsBuilder.UseNpgsql(ConnectionStringBuilder());
@@ -268,7 +315,7 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess)
268315
return base.SaveChanges(acceptAllChangesOnSuccess);
269316
}
270317

271-
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
318+
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
272319
{
273320
OnBeforeSaving();
274321
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);

ClassTranscribeDatabase/CaptionQueries.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public async Task<List<Caption>> GetCaptionsAsync(string videoId, string languag
2525
{
2626
try
2727
{
28-
var transcriptionId = _context.Transcriptions.Where(t => t.Language == language && t.VideoId == videoId).First().Id;
28+
var transcriptionId = _context.Transcriptions.Where(t => t.Language == language && t.VideoId == videoId
29+
&& t.TranscriptionType == TranscriptionType.Caption).First().Id;
2930
return await GetCaptionsAsync(transcriptionId);
3031
}
3132
catch (System.InvalidOperationException)

ClassTranscribeDatabase/CommonUtils.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public enum TaskType
3434
BuildElasticIndex = 16,
3535
ExampleTask = 17,
3636
CleanUpElasticIndex = 18,
37-
PythonCrawler = 19
37+
PythonCrawler = 19,
38+
39+
DescribeVideo = 20,
40+
DescribeImage = 21
41+
3842
}
3943

4044
public class Languages

ClassTranscribeDatabase/Globals.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class AppSettings
1313
public string JWT_KEY { get; set; }
1414
public string ALLOWED_HOSTS { get; set; }
1515
public string POSTGRES_SERVER_NAME { get; set; }
16+
public string POSTGRES_SERVER_PORT { get; set; } = "5432";
1617
public string POSTGRES_DB { get; set; }
1718

1819
public string ADMIN_USER_ID { get; set; }

ClassTranscribeDatabase/Models/Caption.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ public enum CaptionType
1313
TextCaption = 0,
1414
AudioDescription = 1
1515
}
16+
17+
public static class CaptionConstants {
18+
public const string PlaceHolderText = "...Processing...";
19+
20+
}
1621
/// <summary>
1722
/// Each line of caption is stored as a row in the database.
1823
/// </summary>
1924
public class Caption : Entity
2025
{
26+
27+
public bool HasPlaceHolderText() { return this.Text == CaptionConstants.PlaceHolderText; }
2128
public int Index { get; set; }
2229
public TimeSpan Begin { get; set; }
2330
public TimeSpan End { get; set; }
@@ -115,7 +122,7 @@ public static List<Caption> ToCaptionEntitiesInterpolate(int captionsCount, Time
115122
else
116123
{
117124
caption = tempCaption.Substring(0, index);
118-
tempCaption = tempCaption.Substring(index);
125+
tempCaption = tempCaption[index..];
119126
tempCaption = tempCaption.Trim();
120127
}
121128
curEnd = curBegin.Add(new TimeSpan(0, 0, 0, 0, newDuration));
@@ -140,8 +147,8 @@ public static List<Caption> ToCaptionEntitiesInterpolate(int captionsCount, Time
140147
End = curEnd,
141148
Text = tempCaption
142149
});
143-
curBegin = curEnd;
144-
curDuration = End.Subtract(curBegin);
150+
// curBegin = curEnd;
151+
// curDuration = End.Subtract(curBegin);
145152
}
146153
return captions;
147154
}
@@ -204,7 +211,7 @@ public static string GenerateWebVTTString(List<Caption> captions, string languag
204211
//
205212
public static string GenerateDXFPString(List<Caption> captions, string language)
206213
{
207-
string now = DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture);
214+
// string now = DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture);
208215
string header = @"
209216
<?xml version=""1.0"" encoding=""utf-8""?>
210217
<tt xml:lang=""en"" xmlns=""http://www.w3.org/ns/ttml""

ClassTranscribeDatabase/Models/Models.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public ResourceType GetResourceType()
147147
case Playlist _: return ResourceType.Playlist;
148148
case Media _: return ResourceType.Media;
149149
case EPub _: return ResourceType.EPub;
150+
default:
151+
break;
150152
}
151153
throw new InvalidOperationException("Invalid Type passed" + this);
152154
}
@@ -558,22 +560,22 @@ public class TextData : Entity
558560
public string? Text {get; set;}
559561

560562

561-
public void setFromJSON(JToken? o) {
563+
public void SetFromJSON(JToken? o) {
562564
if(o == null) {
563565
Text = null;
564566
return;
565567
}
566568
Text = o.ToString(Newtonsoft.Json.Formatting.None);
567569
}
568570

569-
public JToken? getAsJSON() {
571+
public JToken? GetAsJSON() {
570572
if( string.IsNullOrEmpty(Text)) {
571573
return null;
572574
}
573575
return JToken.Parse(Text);
574576
}
575577

576-
public JArray? getAsJArray()
578+
public JArray? GetAsJArray()
577579
{
578580
if (string.IsNullOrEmpty(Text))
579581
{

ClassTranscribeDatabase/Services/MSTranscription/KeyProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public class Key
2323
/// </summary>
2424
public class KeyProvider
2525
{
26-
private AppSettings _appSettings;
27-
private List<Key> Keys;
28-
private HashSet<string> CurrentVideoIds;
26+
private readonly AppSettings _appSettings;
27+
private readonly List<Key> Keys;
28+
private readonly HashSet<string> CurrentVideoIds;
2929

3030
public KeyProvider(AppSettings appSettings)
3131
{
3232
_appSettings = appSettings;
33-
string subscriptionKeys = _appSettings.AZURE_SUBSCRIPTION_KEYS;
33+
string subscriptionKeys = _appSettings.AZURE_SUBSCRIPTION_KEYS ?? "";
3434
Keys = new List<Key>();
3535
CurrentVideoIds = new HashSet<string>();
3636

0 commit comments

Comments
 (0)