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

47 lines
1.5 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);
// Using the 2026 model string
var model = googleAI.GenerativeModel("gemini-2.5-flash");
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. New GenerateContentRequest automatically handles the text
var request = new GenerateContentRequest(prompt);
// 2. AddMedia is now the standard for version 3.x+
// This handles the binary-to-base64 conversion internally!
request.AddMedia(imageBytes, "image/png");
// 3. Send the unified 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>()
};
}
}