-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
67 lines (55 loc) · 1.86 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Authenticators.OAuth2;
using System.Configuration;
using System.Net;
internal class Program
{
static RestClient client;
private static void Main(string[] args)
{
var apiKey = ConfigurationManager.AppSettings["OpenaiAPIKey"];
var options = new RestClientOptions("https://api.openai.com/v1/images/generations")
{
Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(
apiKey, "Bearer"),
};
client = new RestClient(options);
Console.WriteLine("Enter a description for the image:");
var description = Console.ReadLine();
var response = GenerateImage(description);
var imageUrl = GetGeneratedImageURL(response);
Console.WriteLine($"Generated Image URL: {imageUrl}");
// Display the image or integrate it into your application
DisplayGeneratedImage(imageUrl);
}
static string GenerateImage(string description)
{
var request = new RestRequest()
{
Method = Method.Post,
RequestFormat = DataFormat.Json
};
var param = new {
prompt = description,
n = 1,
size = "256x256"
};
request.AddJsonBody(param);
return client.Execute(request).Content;
}
static string GetGeneratedImageURL(string response)
{
dynamic jsonResponse = JObject.Parse(response);
return jsonResponse["data"][0]["url"];
}
static void DisplayGeneratedImage(string imageUrl)
{
using (var client = new WebClient())
{
var imageName = "generated_image.jpg"; // Change this to your preferred name
client.DownloadFile(imageUrl, imageName);
Console.WriteLine($"Image saved as {imageName}");
}
}
}