56 lines
1.9 KiB
C#
56 lines
1.9 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);
|
|
// Use a string for the model name to avoid CS0117
|
|
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 constructor that the library likes
|
|
var request = new GenerateContentRequest();
|
|
|
|
// Manually build the structure in a way that avoids the List conversion crash
|
|
var content = new Content(Role.User);
|
|
|
|
// We add the text part using the library's preferred text-first approach
|
|
request.Contents = new List<Content> { content };
|
|
|
|
// Attempting the most universal "Add" method for media
|
|
// If your library doesn't have AddMedia, we use this direct property:
|
|
content.Parts = new List<IPart>
|
|
{
|
|
new TextPart(prompt),
|
|
new InlineDataPart("image/png", 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>()
|
|
};
|
|
}
|
|
} |