Skip to content

Commit

Permalink
Add Subtitle Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschucki committed Nov 17, 2024
1 parent 136017a commit 57b7743
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ This bot was designed to help users quickly generate concise summaries of length
### OCR
The application is also capable of Optical Character Recognition (OCR) technology to extract text from images and summarize it.

### Video Subtitles
The bot can also process video subtitles to generate a summary of the video content.

## LICENSE
This project is licensed under the AGPLv3 License - see the [LICENSE](LICENSE) file for details
33 changes: 26 additions & 7 deletions app/Console/Commands/SyncMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,37 @@ protected function syncComments(array $comments): void
$this->withProgressBar($comments, function ($comment) {
if ($this->isValidComment($comment)) {
$posts = Pr0grammApi::Post()->get([
"id" => $comment['itemId'],
"flags" => 31
'id' => $comment['itemId'],
'flags' => 31,
]);

$post = collect($posts->json()["items"])->filter(function (array $post) use($comment) {
return $post["id"] === $comment['itemId'];
$post = collect($posts->json()['items'])->filter(function (array $post) use ($comment) {

Check failure on line 55 in app/Console/Commands/SyncMessagesCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Unable to resolve the template type TKey in call to function collect

Check failure on line 55 in app/Console/Commands/SyncMessagesCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Unable to resolve the template type TValue in call to function collect
return $post['id'] === $comment['itemId'];
});

$image = $post->first()["image"];
$image = $post->first()['image'];

if($image !== null) {
$comment["image"] = $image;
$subtitles = $post->first()['subtitles'] ?? null;

if ($image !== null) {
$comment['image'] = $image;
}

if ($subtitles !== null) {
$subtitle = null;

foreach ($subtitles as $sub) {
if ($sub['isDefault']) {
$subtitle = $sub['path'];
break;
}
}

if ($subtitle === null) {
$subtitle = $subtitles[0]['path'] ?? null;
}

$comment['subtitle'] = $subtitle;
}

Message::firstOrCreate(['messageId' => $comment['id']], $comment);
Expand Down
35 changes: 20 additions & 15 deletions app/Jobs/CreateTldrCommentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CreateTldrCommentJob implements ShouldBeUnique, ShouldQueue
'Der Kommentar ist nicht lang genug. Ich konnte zwischen den Zeilen aber lesen, dass es um deine Mama ging.',
'Etzala reichts. Getrollt wird ned.',
'TLDR: https://www.youtube.com/watch?v=8ybW48rKBME',
'TLDR: https://youtu.be/fPaDlNPbDSM?si=oHa7R_hryFNY4kbY&t=114'
'TLDR: https://youtu.be/fPaDlNPbDSM?si=oHa7R_hryFNY4kbY&t=114',
];

protected array $notLongEnoughPersonalTexts = [
Expand All @@ -46,7 +46,7 @@ class CreateTldrCommentJob implements ShouldBeUnique, ShouldQueue

public function uniqueId(): string
{
return (string)$this->message->messageId;
return (string) $this->message->messageId;
}

public function __construct(Message $message)
Expand All @@ -62,7 +62,7 @@ public function handle(): void
$postInfo = Pr0grammApi::Post()->info($this->message->itemId);
$comment = collect($postInfo['comments'])->firstWhere('id', $this->message->messageId);

if (!$comment) {
if (! $comment) {
return;
}

Expand All @@ -75,7 +75,7 @@ public function handle(): void
* */
$commentToSummarize = $this->getCommentToSummarize($comment);

if (!$commentToSummarize || $this->commentIsMine($commentToSummarize)) {
if (! $commentToSummarize || $this->commentIsMine($commentToSummarize)) {
return;
}

Expand All @@ -84,9 +84,9 @@ public function handle(): void
if ($this->commentIsLongEnough($commentToSummarize['content'])) {
$summary = $this->getTldrValue($commentToSummarize['content']);
if ($summary !== null && $summary !== '' && Str::wordCount($summary) > 5) {
$tldrValue = "TLDR: \n" . $summary;
$tldrValue = "TLDR: \n".$summary;
}
} else if (random_int(0, 2) === 0) {
} elseif (random_int(0, 2) === 0) {
$tldrValue = $this->notLongEnoughTexts[array_rand($this->notLongEnoughTexts)];
} else {
$tldrValue = $this->getPersonalizedNotLongEnoughText($this->message['name']);
Expand All @@ -101,7 +101,7 @@ public function handle(): void
->first();

if ($otherTldrComments !== null) {
$tldrValue = ' https://pr0gramm.com/new/' . $this->message->itemId . ':comment' . $otherTldrComments->messageId;
$tldrValue = ' https://pr0gramm.com/new/'.$this->message->itemId.':comment'.$otherTldrComments->messageId;
}

$this->addTldrComment($tldrValue);
Expand All @@ -126,20 +126,19 @@ protected function commentOnlyContainsMention(string $comment): bool
protected function getPersonalizedNotLongEnoughText(string $name): string
{
$randomText = $this->notLongEnoughPersonalTexts[array_rand($this->notLongEnoughPersonalTexts)];

return Str::replace('$name', $name, $randomText);

Check failure on line 130 in app/Jobs/CreateTldrCommentJob.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Jobs\CreateTldrCommentJob::getPersonalizedNotLongEnoughText() should return string but returns iterable<string>|string.

Check failure on line 130 in app/Jobs/CreateTldrCommentJob.php

View workflow job for this annotation

GitHub Actions / phpstan

Unable to resolve the template type TSubject in call to method static method Illuminate\Support\Str::replace()
}

protected function summarizePost(): void
{
// download the image and get the text from ocr if the post is an image
// check
$image = $this->message->image;
$imageUrl = 'https://img.pr0gramm.com/' . $image;
$imageUrl = 'https://img.pr0gramm.com/'.$image;
$imageText = $this->getImageText($imageUrl);
if ($imageText && Str::wordCount($imageText) >= 40) {
$tldrValue = $this->getTldrValue($imageText);
if ($tldrValue !== null && $tldrValue !== '' && Str::wordCount($tldrValue) > 5) {
$this->addTldrComment("TLDR: \n" . $tldrValue);
$this->addTldrComment("TLDR: \n".$tldrValue);
}
}
}
Expand All @@ -153,8 +152,14 @@ protected function getImageText(string $imageUrl): ?string
$fileNameFragments = explode('/', $this->message->image);
$fileName = end($fileNameFragments);

// check if the image is a image
if (!Str::endsWith($fileName, ['.jpg', '.jpeg', '.png', '.tiff'])) {
if (! Str::endsWith($fileName, ['.jpg', '.jpeg', '.png', '.tiff'])) {
if ($this->message->subtitle) {
$response = $client->get('https://pr0gramm.com/data/images/'.$this->message->subtitle);
if ($response->getStatusCode() === 200) {
return $response->getBody()->getContents();
}
}

return null;
}

Expand Down Expand Up @@ -212,7 +217,7 @@ protected function getTldrValue(string $comment): ?string
$response = $client->chat()->create([
'model' => $this->getGptModel($comment),
'messages' => [
['role' => 'user', 'content' => $this->basePrompt . $comment],
['role' => 'user', 'content' => $this->basePrompt.$comment],
],
]);

Expand Down Expand Up @@ -266,6 +271,6 @@ protected function commentEndsWithPattern(string $comment): bool
// Pattern: Comments ends with @tldr but has at least 200 characters
$endsWithPattern = '/^.{200,}.*@tldr\s*$/is';

return (bool)preg_match($endsWithPattern, $comment);
return (bool) preg_match($endsWithPattern, $comment);
}
}
2 changes: 1 addition & 1 deletion app/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Message extends Model
'repliedToComment',
'tldrValue',
'replyCommentId',
'image'
'image',
];

protected $casts = [
Expand Down
22 changes: 22 additions & 0 deletions database/migrations/2024_11_17_140620_add_subtitle_to_messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('messages', static function (Blueprint $table) {
$table->text('subtitle')->nullable();
});
}

public function down(): void
{
Schema::table('messages', static function (Blueprint $table) {
$table->dropColumn('subtitle');
});
}
};

0 comments on commit 57b7743

Please sign in to comment.