-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
// Esteban | ||
|
||
namespace App\Interfaces; | ||
|
||
use app\Models\Game; | ||
|
||
interface BalanceGenerator | ||
{ | ||
public function generateBalance(Game $game): string; | ||
} |
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,40 @@ | ||
<?php | ||
|
||
// Esteban | ||
|
||
namespace App\Providers; | ||
|
||
use App\Interfaces\BalanceGenerator; | ||
use App\Util\BalanceGemini; | ||
use App\Util\BalanceHuggingFace; | ||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class BalanceGeneratorServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register any application services. | ||
*/ | ||
public function register(): void | ||
{ | ||
$this->app->bind(BalanceGenerator::class, function (Application $app, array $params): BalanceGenerator { | ||
$type = $params['type'] ?? 'gemini'; | ||
|
||
switch ($type) { | ||
case 'huggingface': | ||
return new BalanceHuggingFace; | ||
case 'gemini': | ||
default: | ||
return new BalanceGemini; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Bootstrap any application services. | ||
*/ | ||
public function boot(): void | ||
{ | ||
// | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
// Esteban | ||
|
||
namespace App\Util; | ||
|
||
use App\Interfaces\BalanceGenerator; | ||
use App\Models\Game; | ||
use Gemini; | ||
|
||
class BalanceGemini implements BalanceGenerator | ||
{ | ||
public function generateBalance(Game $game): string | ||
{ | ||
$reviews = $game->getReviews(); | ||
$comments = $reviews->pluck('comment')->implode(' | '); | ||
|
||
$prompt = "Generate a summary of the balance of the next comments about the game {$game->getName()} that has a rating of {$game->getRating()}, also, say the reasons of the good and bad comments, that's the vital part: {$comments}"; | ||
|
||
$apiKey = env('GEMINI_API_KEY'); | ||
$client = Gemini::client($apiKey); | ||
|
||
$result = $client->geminiPro()->generateContent($prompt); | ||
|
||
$balanceMarkdown = "**Made with Gemini**\n\n".$result->text(); | ||
$balanceHtml = FormattingUtil::convertMarkdownToHtml($balanceMarkdown); | ||
|
||
return $balanceHtml; | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
// Esteban | ||
|
||
namespace App\Util; | ||
|
||
use App\Interfaces\BalanceGenerator; | ||
use App\Models\Game; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class BalanceHuggingFace implements BalanceGenerator | ||
{ | ||
public function generateBalance(Game $game): string | ||
{ | ||
$reviews = $game->getReviews(); | ||
$comments = $reviews->map(fn ($review) => "{$review->comment}")->implode('. '); | ||
|
||
// Configura las preguntas | ||
$positiveQuestion = "From the following reviews, give me all the positive aspects mentioned about the game '{$game->getName()}'. Be so detailed, answer in english and use complete sentences."; | ||
$negativeQuestion = "From the following reviews, give me all the negative aspects mentioned about the game '{$game->getName()}'. Be so detailed, answer in english and use complete sentences."; | ||
|
||
// Obtén respuestas para ambas preguntas | ||
$positiveAnswer = $this->askQuestion($positiveQuestion, $comments); | ||
$negativeAnswer = $this->askQuestion($negativeQuestion, $comments); | ||
|
||
// Genera el resultado en formato Markdown | ||
$balanceMarkdown = "**Made with HuggingFace**\n\n"; | ||
$balanceMarkdown .= "**A Positive Comment:**\n".$positiveAnswer."\n\n"; | ||
$balanceMarkdown .= "**A Negative Comment:**\n".$negativeAnswer; | ||
|
||
// Convierte el Markdown a HTML | ||
return FormattingUtil::convertMarkdownToHtml($balanceMarkdown); | ||
} | ||
|
||
private function askQuestion(string $question, string $context): string | ||
{ | ||
$apiKey = env('HUGGINGFACE_API_KEY'); | ||
|
||
$response = Http::withHeaders([ | ||
'Authorization' => "Bearer $apiKey", | ||
])->post('https://api-inference.huggingface.co/models/deepset/roberta-base-squad2', [ | ||
'inputs' => [ | ||
'question' => $question, | ||
'context' => $context, | ||
], | ||
]); | ||
|
||
return $response->json()['answer'] ?? 'No answer found.'; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
return [ | ||
App\Providers\AppServiceProvider::class, | ||
App\Providers\BalanceGeneratorServiceProvider::class, | ||
]; |
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