Skip to content

Commit 34d6426

Browse files
Merge pull request #1840 from MihirRajeshPanchal/PR1_MihirRajeshPanchal
Added Text to image python script
2 parents 79943a3 + ac28795 commit 34d6426

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

Text-to-Image/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Dalle
2+
3+
This script utilizes the OpenAI API to generate an image based on a given prompt using the DALL-E model.
4+
5+
## Setup instructions
6+
7+
To set up and run the script, follow the instructions below:
8+
9+
1. Install the required dependencies:
10+
- `requests`
11+
- `openai` (Install using `pip install openai`)
12+
13+
2. Obtain an API key from OpenAI:
14+
- Sign up for an account at [OpenAI](https://www.openai.com/).
15+
- Generate an API key.
16+
17+
3. Set the API key:
18+
- Replace `"your-api-key"` in the script with your actual API key obtained from OpenAI.
19+
20+
4. Run the script:
21+
- Execute the script using a Python interpreter.
22+
- Enter a prompt when prompted by the script.
23+
24+
## Detailed explanation of script
25+
26+
The script performs the following steps:
27+
28+
1. Imports the necessary libraries (`requests` and `openai`).
29+
2. Sets up the OpenAI API client using the API key.
30+
3. Defines the DALL-E model engine and the prompt (message) provided by the user.
31+
4. Generates a response by calling the OpenAI API and passing the prompt.
32+
5. Retrieves the URL of the generated image from the response.
33+
6. Downloads the image using the URL and saves it as "image.jpg".
34+
7. Prints the generated image URL.
35+
36+
## Output
37+
38+
The script saves the generated image as "image.jpg" in the current directory. The URL of the generated image is also printed to the console.
39+
40+
## Author(s)
41+
42+
Author: [Mihir Panchal](https://github.com/MihirRajeshPanchal)
43+
44+
## Disclaimers, if any
45+
46+
No specific disclaimers or legal information apply to this script.

Text-to-Image/dalle.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def dalle(message):
2+
import requests
3+
import openai
4+
5+
# Set up the OpenAI API client
6+
openai.api_key = "your-api-key"
7+
8+
# Set up the model and prompt
9+
model_engine = "text-davinci-003"
10+
prompt = message
11+
12+
# Generate a response
13+
response = openai.Image.create(
14+
prompt=prompt,
15+
n=1,
16+
size="1024x1024"
17+
)
18+
image_url = response['data'][0]['url']
19+
response = requests.get(image_url)
20+
with open("image.jpg", "wb") as f:
21+
f.write(response.content)
22+
23+
print(image_url)
24+
25+
dalle(input("Enter something : "))

0 commit comments

Comments
 (0)