46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using Seasoned.Backend.DTOs;
|
|
using Mscc.GenerativeAI;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System.IO;
|
|
|
|
namespace Seasoned.Backend.Services;
|
|
|
|
public class RecipeService : IRecipeService
|
|
{
|
|
private readonly string _apiKey;
|
|
|
|
public RecipeService(IConfiguration config)
|
|
{
|
|
_apiKey = config["GeminiApiKey"] ?? throw new ArgumentNullException("API Key missing");
|
|
}
|
|
|
|
public async Task<RecipeResponseDto> ParseRecipeImageAsync(IFormFile image)
|
|
{
|
|
var googleAI = new GoogleAI(_apiKey);
|
|
var model = googleAI.GenerativeModel(Model.Gemini25Flash);
|
|
|
|
using var ms = new MemoryStream();
|
|
await image.CopyToAsync(ms);
|
|
var imageBytes = ms.ToArray();
|
|
|
|
var prompt = "Extract the recipe from this image. Return Title and Description.";
|
|
|
|
// 1. Create the request with just the text prompt first
|
|
var request = new GenerateContentRequest(prompt);
|
|
|
|
// 2. Use the built-in AddMedia helper.
|
|
// This automatically handles the IPart/InlineData wrapping for you!
|
|
request.AddMedia(imageBytes, "image/png");
|
|
|
|
// 3. Send the request
|
|
var response = await model.GenerateContent(request);
|
|
|
|
return new RecipeResponseDto
|
|
{
|
|
Title = "Gemini 2.5 Analysis",
|
|
Description = response.Text ?? "No text returned",
|
|
Ingredients = new List<string>(),
|
|
Instructions = new List<string>()
|
|
};
|
|
}
|
|
} |