47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
using Seasoned.Backend.DTOs;
|
|
using Mscc.GenerativeAI; // Add this!
|
|
|
|
namespace Seasoned.Backend.Services;
|
|
|
|
public class RecipeService : IRecipeService
|
|
{
|
|
private readonly string _apiKey;
|
|
|
|
public RecipeService(IConfiguration config)
|
|
{
|
|
// Get the key from your user-secrets
|
|
_apiKey = config["GeminiApiKey"] ?? throw new ArgumentNullException("API Key missing");
|
|
}
|
|
|
|
public async Task<RecipeResponseDto> ParseRecipeImageAsync(IFormFile image)
|
|
{
|
|
// 1. Initialize Gemini
|
|
var googleAI = new GoogleAI(_apiKey);
|
|
var model = googleAI.GenerativeModel(Model.Gemini15Flash);
|
|
|
|
// 2. Convert the uploaded image to a format Gemini understands
|
|
using var ms = new MemoryStream();
|
|
await image.CopyToAsync(ms);
|
|
var base64Image = Convert.ToBase64String(ms.ToArray());
|
|
|
|
// 3. The "Magic" Prompt
|
|
var prompt = "Extract the recipe from this image. Return ONLY a JSON object with: title (string), description (string), ingredients (array of strings), and instructions (array of strings).";
|
|
|
|
// 4. Call Gemini
|
|
var response = await model.GenerateContent(new List<Part> {
|
|
new TextPart { Text = prompt },
|
|
new InlineDataPart { MimeType = "image/jpeg", Data = base64Image }
|
|
});
|
|
|
|
// 5. Parse the AI's response (simplified for now)
|
|
var aiText = response.Text;
|
|
|
|
// For now, let's return the AI text in our DTO
|
|
return new RecipeResponseDto
|
|
{
|
|
Title = "AI Decoded Recipe",
|
|
Description = aiText, // The raw AI response
|
|
Ingredients = new List<string> { "Check description for details" }
|
|
};
|
|
}
|
|
} |