-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_gen.py
60 lines (45 loc) · 1.6 KB
/
image_gen.py
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
import streamlit as st
from openai import OpenAI
import os
import textwrap
def translate_text_for_image(text):
client = OpenAI(
api_key = st.secrets["open_spi_key"]
)
user_content = f"Translate the following Korean sentences into English.\n{text}"
message=[{"role":"user","content":user_content}]
response = client.chat.completions.create(
model = "gpt-4-turbo-2024-04-09",
messages = message,
max_tokens=1000,
temperature=0.8,
n=1
)
assistant_reply = response.choices[0].message.content
return assistant_reply
def generate_image_from_text(text_for_image, image_num=1, image_size="512x512"):
client = OpenAI(
api_key = st.secrets["open_spi_key"]
)
shorten_text_for_image = textwrap.shorten(text_for_image,1000)
response = client.images.generate(prompt=shorten_text_for_image,n=image_num,size=image_size)
image_urls = []
for data in response.data:
image_url = data.url
image_urls.append(image_url)
return image_urls
def generate_text_for_image(text):
client = OpenAI(
api_key = st.secrets["open_spi_key"]
)
user_content = f"Describe the following in 1000 characters to create an image.\n{text}"
message=[{"role":"user","content":user_content}]
response = client.chat.completions.create(
model = "gpt-4-turbo-2024-04-09",
messages = message,
max_tokens=1000,
temperature=0.8,
n=1
)
assistant_reply = response.choices[0].message.content
return assistant_reply