-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding gpt4o-mini and testing dalle for slidev images
- Loading branch information
JAlcocerT
committed
Jul 22, 2024
1 parent
6fb78c1
commit 86e76b5
Showing
4 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import openai #0.28.1 | ||
import requests | ||
import os | ||
|
||
# Set your OpenAI API key here | ||
openai.api_key = 'sk-proj-...' | ||
|
||
# Define the prompt for the image generation | ||
#prompt = "A futuristic cityscape with flying cars and neon lights" | ||
prompt = "A calm city with parks. Realistic look." | ||
|
||
# Generate the image | ||
response = openai.Image.create( | ||
prompt=prompt, | ||
n=1, # Number of images to generate | ||
size="1024x1024", # Size of the image | ||
) | ||
|
||
# Get the URL of the generated image | ||
image_url = response['data'][0]['url'] | ||
|
||
# Download the image | ||
image_data = requests.get(image_url).content | ||
with open('generated_image.png', 'wb') as handler: | ||
handler.write(image_data) | ||
|
||
print("Image generated and saved as 'generated_image.png'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import openai | ||
import requests | ||
import os | ||
import toml | ||
from dotenv import load_dotenv | ||
|
||
# Function to load the API key | ||
def load_openai_api_key(): | ||
# First, check if the API key is provided as an environment variable | ||
api_key = os.getenv('OPENAI_API_KEY') | ||
if api_key: | ||
return api_key | ||
|
||
# If not found, try to load it from the .env file | ||
load_dotenv() | ||
api_key = os.getenv('OPENAI_API_KEY') | ||
if api_key: | ||
return api_key | ||
|
||
# If still not found, try to load it from the .toml file | ||
try: | ||
config = toml.load('../../.streamlit/secrets.toml') | ||
api_key = config['OPENAI_API_KEY'] #config['openai']['api_key'] | ||
return api_key | ||
except FileNotFoundError: | ||
print("Error: secrets.toml file not found.") | ||
except KeyError: | ||
print("Error: API key not found in secrets.toml.") | ||
|
||
# If the API key is still not found, raise an error | ||
raise ValueError("OpenAI API key is not set. Please set it as an environment variable, in the .env file, or in the .toml file.") | ||
|
||
|
||
# Set your OpenAI API key here | ||
#openai.api_key = 'sk-proj-' | ||
# config = toml.load('../../.streamlit/secrets.toml') | ||
# #config['openai']['api_key'] | ||
|
||
# # Create an OpenAI client | ||
# #client = openai.OpenAI() | ||
client = openai.OpenAI(api_key=load_openai_api_key()) | ||
|
||
# Define the prompt for the image generation | ||
prompt = "A futuristic cityscape with flying cars and neon lights" | ||
|
||
# Generate the image using a specific model #https://platform.openai.com/docs/guides/images/generations | ||
response = client.images.generate( | ||
model="dall-e-3", # Specify the model here | ||
prompt=prompt, | ||
size="1024x1024", # Size of the image | ||
quality="standard", # Quality of the image | ||
n=1 # Number of images to generate | ||
) | ||
|
||
# Get the URL of the generated image | ||
image_url = response.data[0].url | ||
|
||
# Download and save the image | ||
image_data = requests.get(image_url).content | ||
with open('generated_image22.png', 'wb') as handler: | ||
handler.write(image_data) | ||
|
||
print("Image generated and saved as 'generated_image22.png'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
* https://platform.openai.com/docs/guides/images/usage?context=python | ||
|
||
```sh | ||
export OPENAI_API_KEY=your-api-key-here | ||
set OPENAI_API_KEY="your-api-key-here" | ||
``` | ||
|
||
* Get it from the server with: | ||
|
||
```sh | ||
sftp [email protected] | ||
get /home/serveruser/dalle/generated_image22.png C:\Users\desktopuser\Desktop\Streamlit-MultiChat\Z_Tests\generated_image.png | ||
``` | ||
|
||
* Or with: | ||
|
||
```sh | ||
python3 -m http.server 8000 | ||
|
||
Invoke-WebRequest -Uri http://192.168.3.200:8000/generated_image22.png -OutFile C:\Users\Jesus_Alcocer\Desktop\Streamlit-MultiChat\Z_Tests\generated_image22.png | ||
|
||
|
||
curl http://192.168.3.200:8000/generated_image22.png --output C:\Users\Jesus_Alcocer\Desktop\Streamlit-MultiChat\Z_Tests\generated_image.png | ||
``` |