feat: wip on implementing gemini analyzer
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using NAudio.CoreAudioApi;
|
||||
|
||||
using StreamShorts.Library.Transcription;
|
||||
|
||||
namespace StreamShorts.Library.Analysis;
|
||||
|
||||
public sealed class GeminiAnalyzer(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
string apiKey
|
||||
) : ITranscriptAnalyzer
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
|
||||
private readonly string _apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
|
||||
|
||||
public async Task<TranscriptAnalysis> AnalyzeAsync(IEnumerable<TranscriptionSegment> segments)
|
||||
{
|
||||
using var client = _httpClientFactory.CreateClient();
|
||||
client.Timeout = TimeSpan.FromMinutes(5);
|
||||
|
||||
// TODO: Load prompt from resource file
|
||||
var requestUrl = $"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key={_apiKey}";
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
|
||||
{
|
||||
Content = new StringContent(
|
||||
JsonSerializer.Serialize(new
|
||||
{
|
||||
contents = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
role = "user",
|
||||
parts = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
text = prompt
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
generationConfig = new
|
||||
{
|
||||
responseMimeType = "application/json",
|
||||
}
|
||||
}),
|
||||
Encoding.UTF8,
|
||||
"application/json"
|
||||
)
|
||||
};
|
||||
var response = await client.SendAsync(request).ConfigureAwait(false);
|
||||
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
var responseJson = JsonSerializer.Deserialize<LLMResponse>(responseContent);
|
||||
var candidatesText = responseJson?
|
||||
.Candidates?
|
||||
.FirstOrDefault()?
|
||||
.Content
|
||||
.Parts?.FirstOrDefault()?
|
||||
.Text;
|
||||
|
||||
var clips = JsonSerializer.Deserialize<List<ShortClip>>(candidatesText ?? string.Empty);
|
||||
return new TranscriptAnalysis(clips ?? []);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Sort this shit out
|
||||
record LLMResponse(
|
||||
[property: JsonPropertyName("candidates")]
|
||||
Candidate[] Candidates
|
||||
);
|
||||
|
||||
record Candidate(
|
||||
[property: JsonPropertyName("content")]
|
||||
Content Content
|
||||
);
|
||||
|
||||
record Content(
|
||||
[property: JsonPropertyName("parts")]
|
||||
Part[] Parts
|
||||
);
|
||||
|
||||
record Part(
|
||||
[property: JsonPropertyName("text")]
|
||||
string Text
|
||||
);
|
||||
@@ -0,0 +1,8 @@
|
||||
using StreamShorts.Library.Transcription;
|
||||
|
||||
namespace StreamShorts.Library.Analysis;
|
||||
|
||||
public interface ITranscriptAnalyzer
|
||||
{
|
||||
Task<TranscriptAnalysis> AnalyzeAsync(IEnumerable<TranscriptionSegment> segments);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace StreamShorts.Library.Analysis;
|
||||
|
||||
public record ShortClip(
|
||||
[property: JsonPropertyName("title")]
|
||||
string Title,
|
||||
[property: JsonPropertyName("description")]
|
||||
string Description,
|
||||
[property: JsonPropertyName("explanation")]
|
||||
string Explanation,
|
||||
[property: JsonPropertyName("start_time")]
|
||||
TimeSpan StartTime,
|
||||
[property: JsonPropertyName("end_time")]
|
||||
TimeSpan EndTime
|
||||
);
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace StreamShorts.Library.Analysis;
|
||||
|
||||
public class TranscriptAnalysis(IEnumerable<ShortClip> shortClips)
|
||||
{
|
||||
public IEnumerable<ShortClip> ShortClips { get; init; } = shortClips;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
I need your help to transform my YouTube live stream transcript into engaging YouTube Shorts. Act as my content editor and pinpoint **all potential candidate segments** that are perfect for short-form video. I'm looking for clips that are:
|
||||
|
||||
- **Funny:** Moments that will make viewers laugh.
|
||||
- **Informative:** Sections packed with valuable information or tips.
|
||||
- **Insightful:** Portions offering unique perspectives or 'aha\!' moments.
|
||||
|
||||
For each suggested short, please provide:
|
||||
|
||||
- The **start time** of the initial segment and the **end time** of the final segment. The duration of each short should be no longer than 3 minutes, but **aim for durations between 15 seconds and 60 seconds**. However, the short **must be as long as necessary to capture the complete thought or idea**, even if it means exceeding the target range or extending slightly to capture all necessary dialogue.
|
||||
- A concise **title** that grabs attention.
|
||||
- A brief **description** highlighting the short's content and its appeal.
|
||||
- An **explanation** of why this particular segment is suitable for a YouTube Short, focusing on its potential for discoverability and engagement.
|
||||
|
||||
Please format your response as a JSON array of objects with the following structure:
|
||||
|
||||
```json
|
||||
{
|
||||
"title": "string",
|
||||
"start_time": "string",
|
||||
"end_time": "string",
|
||||
"description": "string",
|
||||
"explanation": "string"
|
||||
}
|
||||
```
|
||||
|
||||
Here is the transcript of my YouTube live stream:
|
||||
|
||||
{0}
|
||||
@@ -1,6 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FFMpegCore" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" />
|
||||
<PackageReference Include="NAudio" />
|
||||
<PackageReference Include="Whisper.net.AllRuntimes" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user