diff --git a/OpenAi/ModerationResponse.cs b/OpenAi/ModerationResponse.cs new file mode 100644 index 0000000..0579121 --- /dev/null +++ b/OpenAi/ModerationResponse.cs @@ -0,0 +1,27 @@ +public class ModerationResponse +{ + public string Id { get; set; } + public string Model { get; set; } + public List Results { get; set; } +} + +public class ModerationResult +{ + public Categories Categories { get; set; } + public CategoryScores Category_Scores { get; set; } + public bool Flagged { get; set; } +} + +public class Categories +{ + public bool Hate { get; set; } + public bool Self_Harm { get; set; } + public bool Violence { get; set; } +} + +public class CategoryScores +{ + public float Hate { get; set; } + public float Self_Harm { get; set; } + public float Violence { get; set; } +} \ No newline at end of file diff --git a/OpenAi/OpenAiClient.cs b/OpenAi/OpenAiClient.cs new file mode 100644 index 0000000..d165c71 --- /dev/null +++ b/OpenAi/OpenAiClient.cs @@ -0,0 +1,71 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using System.Text.Json.Serialization; + +public class OpenAIClient +{ + private readonly string _apiKey; + private readonly HttpClient _httpClient; + private readonly string _dalleModel = "dall-e-2"; + private readonly string _moderationModel = "omni-moderation-latest"; + + public OpenAIClient(string apiKey) + { + _apiKey = apiKey; + _httpClient = new HttpClient(); + _httpClient.DefaultRequestHeaders.Authorization = + new AuthenticationHeaderValue("Bearer", _apiKey); + } + + public async Task GenerateImageAsync(string prompt) + { + var url = "https://api.openai.com/v1/images/generations"; + var requestBody = new + { + model = _dalleModel, + prompt = prompt, + n = 1, + size = "1024x1024" + }; + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync(url, content); + response.EnsureSuccessStatusCode(); + + var responseJson = await response.Content.ReadAsStringAsync(); + using var doc = JsonDocument.Parse(responseJson); + var imageUrl = doc.RootElement.GetProperty("data")[0].GetProperty("url").GetString(); + + return imageUrl; + } + public async Task ModerateContentAsync(string input) + { + var url = "https://api.openai.com/v1/moderations"; + var requestBody = new + { + model = _moderationModel, + input = input + }; + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync(url, content); + response.EnsureSuccessStatusCode(); + + var responseJson = await response.Content.ReadAsStringAsync(); + + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + var moderationResponse = JsonSerializer.Deserialize(responseJson, options); + return moderationResponse; + } + +} diff --git a/OpenAi/OpenAiHandler.py b/OpenAi/OpenAiHandler.py new file mode 100644 index 0000000..778e922 --- /dev/null +++ b/OpenAi/OpenAiHandler.py @@ -0,0 +1,37 @@ +import os +import requests + +class OpenAIHandler: + def __init__(self, api_key): + self.headers = { + "Authorization": f"Bearer {api_key}", + } + + def generate_code(self, prompt: str) -> str: + payload = { + "model": "gpt-4o-mini", + "messages": [{"role": "user", "content": prompt}], + "temperature": 0.2, # lower temp for more focused code + } + headers = self.headers.copy() + headers["Content-Type"] = "application/json" + + response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) + response.raise_for_status() + data = response.json() + return data["choices"][0]["message"]["content"] + + def transcribe_audio(self, audio_file_path: str) -> str: + headers = self.headers.copy() + with open(audio_file_path, "rb") as audio_file: + files = { + "file": (audio_file_path, audio_file, "audio/mpeg"), + } + data = { + "model": "whisper-1", + "language": "en" + } + response = requests.post("https://api.openai.com/v1/audio/transcriptions", headers=headers, data=data, files=files) + response.raise_for_status() + data = response.json() + return data["text"]