51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using Seasoned.Backend.DTOs;
|
|
using Mscc.GenerativeAI;
|
|
using System.Linq;
|
|
|
|
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.";
|
|
|
|
// Use the library's internal GenerateContentRequest but manually build the Parts list
|
|
var request = new GenerateContentRequest();
|
|
var content = new Content(Role.User);
|
|
|
|
// This is the specific syntax to satisfy the IPart list requirement
|
|
content.Parts = new List<IPart>
|
|
{
|
|
new Part { Text = prompt },
|
|
new Part { InlineData = new InlineData { MimeType = "image/png", Data = base64Image } }
|
|
}.Cast<IPart>().ToList(); // This 'Cast' is the magic bullet
|
|
|
|
request.Contents = new List<Content> { content };
|
|
|
|
// Solve the ambiguity by picking the Request overload
|
|
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>()
|
|
};
|
|
}
|
|
} |