52 lines
1.7 KiB
C#
52 lines
1.7 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)
|
|
{
|
|
// Use the explicit string for the model to bypass the "Model.Gemini25Flash" error
|
|
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());
|
|
|
|
// Use a dynamic request structure which the library supports for newer models
|
|
var prompt = "Extract the recipe from this image. Return Title and Description.";
|
|
|
|
// This is a more robust way to send the request in the latest version
|
|
var response = await model.GenerateContent(new()
|
|
{
|
|
Contents = new List<Content>
|
|
{
|
|
new Content
|
|
{
|
|
Parts = new List<Part>
|
|
{
|
|
new() { Text = prompt },
|
|
new() { InlineData = new InlineData { MimeType = "image/png", Data = base64Image } }
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
return new RecipeResponseDto
|
|
{
|
|
Title = "Gemini 2.5 Analysis",
|
|
Description = response.Text ?? "No text returned",
|
|
Ingredients = new List<string>(),
|
|
Instructions = new List<string>()
|
|
};
|
|
}
|
|
} |