Spaces:
Sleeping
Sleeping
Download src/ClipForge.Infrastructure/Services/GeminiClient.cs from clipeforge/app: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/spaces/clipeforge/app/resolve/main/src/ClipForge.Infrastructure/Services/GeminiClient.cs
- Command line
-
hf download hf://spaces/clipeforge/app/src/ClipForge.Infrastructure/Services/GeminiClient.cs
-
curl -L -o GeminiClient.cs https://huggingface.co/spaces/clipeforge/app/resolve/main/src/ClipForge.Infrastructure/Services/GeminiClient.cs
2.62 kB
| using System.Text; | |
| using System.Text.Json; | |
| using ClipForge.Core.Interfaces.Services; | |
| using ClipForge.Infrastructure.Configuration; | |
| using Microsoft.Extensions.Logging; | |
| using Microsoft.Extensions.Options; | |
| namespace ClipForge.Infrastructure.Services; | |
| public class GeminiClient : IOpenAiClient | |
| { | |
| private readonly HttpClient _httpClient; | |
| private readonly OpenAiSettings _settings; | |
| private readonly ILogger<GeminiClient> _logger; | |
| public GeminiClient(HttpClient httpClient, IOptions<OpenAiSettings> settings, ILogger<GeminiClient> logger) | |
| { | |
| _httpClient = httpClient; | |
| _settings = settings.Value; | |
| _logger = logger; | |
| } | |
| public async Task<string> GenerateChatCompletionAsync(string systemPrompt, string userPrompt, CancellationToken ct = default) | |
| { | |
| var url = $"https://generativelanguage.googleapis.com/v1beta/models/{_settings.Model}:generateContent?key={_settings.ApiKey}"; | |
| var requestBody = new | |
| { | |
| contents = new[] | |
| { | |
| new | |
| { | |
| parts = new[] | |
| { | |
| new { text = $"{systemPrompt}\n\n{userPrompt}" } | |
| } | |
| } | |
| }, | |
| generationConfig = new | |
| { | |
| temperature = 0.9, | |
| maxOutputTokens = 4000 | |
| } | |
| }; | |
| var json = JsonSerializer.Serialize(requestBody); | |
| var content = new StringContent(json, Encoding.UTF8, "application/json"); | |
| _logger.LogInformation("Sending request to Gemini model {Model}", _settings.Model); | |
| var response = await _httpClient.PostAsync(url, content, ct); | |
| var responseBody = await response.Content.ReadAsStringAsync(ct); | |
| if (!response.IsSuccessStatusCode) | |
| { | |
| _logger.LogError("Gemini API error {Status}: {Body}", response.StatusCode, responseBody[..Math.Min(responseBody.Length, 300)]); | |
| throw new HttpRequestException($"Gemini API error: {response.StatusCode}"); | |
| } | |
| using var doc = JsonDocument.Parse(responseBody); | |
| var text = doc.RootElement | |
| .GetProperty("candidates")[0] | |
| .GetProperty("content") | |
| .GetProperty("parts")[0] | |
| .GetProperty("text") | |
| .GetString(); | |
| return text ?? string.Empty; | |
| } | |
| public Task<byte[]> GenerateSpeechAsync(string text, string voice = "alloy", CancellationToken ct = default) | |
| { | |
| // Not used - Edge TTS handles speech | |
| throw new NotImplementedException("Use Edge TTS for speech generation"); | |
| } | |
| } | |