54 lines
1.7 KiB
C#
54 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("GeminiApiKey missing in secrets");
|
|
}
|
|
|
|
public async Task<RecipeResponseDto> ParseRecipeImageAsync(IFormFile image)
|
|
{
|
|
var googleAI = new GoogleAI(_apiKey);
|
|
// Using the 2.5 Flash model specifically
|
|
var model = googleAI.GenerativeModel(Model.Gemini25Flash);
|
|
|
|
using var ms = new MemoryStream();
|
|
await image.CopyToAsync(ms);
|
|
var base64Image = Convert.ToBase64String(ms.ToArray());
|
|
|
|
var prompt = "Extract this recipe. Provide a title and description.";
|
|
|
|
// This structure ensures the 2.5 model receives both the text and the image correctly
|
|
var request = new GenerateContentRequest
|
|
{
|
|
Contents = new List<Content>
|
|
{
|
|
new Content
|
|
{
|
|
Role = Role.User,
|
|
Parts = new List<Part>
|
|
{
|
|
new TextPart { Text = prompt },
|
|
new InlineDataPart { MimeType = "image/png", Data = base64Image }
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
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>()
|
|
};
|
|
}
|
|
} |