Files
Seasoned/.vscode-server/data/User/History/b03096/gr1X.cs

45 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("gemini-2.5-flash");
using var ms = new MemoryStream();
await image.CopyToAsync(ms);
// THE FIX: Convert bytes to a Base64 string for the AddMedia method
var base64Image = Convert.ToBase64String(ms.ToArray());
var prompt = "Extract the recipe from this image. Return Title and Description.";
var request = new GenerateContentRequest(prompt);
// This matches the (string, string) signature the compiler is looking for
request.AddMedia(base64Image, "image/png");
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>()
};
}
}