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

51 lines
1.8 KiB
C#

using Seasoned.Backend.DTOs;
using Mscc.GenerativeAI;
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);
var base64Image = Convert.ToBase64String(ms.ToArray());
var prompt = "Extract the recipe from this image. Return Title and Description.";
// We create the request using the library's internal 'Content' structure
// but we add the parts as simple objects to avoid the IPart cast crash.
var content = new Content(Role.User);
content.Parts = new List<IPart>();
// Instead of 'new Part', we use the specific Part types provided by the library
// that are GUARANTEED to implement IPart correctly.
content.Parts.Add(new TextPart { Text = prompt });
content.Parts.Add(new InlineDataPart { MimeType = "image/png", Data = base64Image });
var request = new GenerateContentRequest
{
Contents = new List<Content> { content }
};
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>()
};
}
}