-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (70 loc) · 3.31 KB
/
app.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask, render_template, request
import openai
app = Flask(__name__)
# Set up API key and endpoint URL
openai.api_key = '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'
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
# Get user input from form
input_text = request.form['input_text']
if len(input_text) < 15000:
# Call your existing code to generate output
output = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": "Write a sentence that generates a prompt for an AI art piece based on a story I will share (be sure to mention art style, colors, themes, characters, and setting). Be concise. Here is the story: '/n:" + input_text}
]
)
# Extract the output message from the API response
output_message = output.choices[0].message.content
response = openai.Image.create(
prompt="" + output_message,
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
return render_template('index.html', output_message=output_message, image_url=image_url)
elif len(input_text) > 15000:
if len(input_text) > 30000:
input_text = input_text[0:30000]
input1 = input_text[:len(input_text)//2]
input2 = input_text[len(input_text)//2:]
output1 = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": "Summarize this story:" + input1}
]
)
output2 = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": "Summarize this story:" + input2}
]
)
output_message1 = output1.choices[0].message.content
output_message2 = output2.choices[0].message.content
output3 = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": "Merge these summaries into one prompt for an AI art piece that pulls from elements of both (be sure to mention art style, colors, themes, characters, and setting). Be concise.. Here is the first summary:" + output_message1 + "Here is the second summary:" + output_message2 + "keep it concise."}
]
)
output_message3 = output3.choices[0].message.content
output = openai.Image.create(
prompt="" + output_message3,
n=1,
size="512x512"
)
image_url1 = output['data'][0]['url']
return render_template('index.html', image_url1=image_url1, output_message3=output_message3)
else:
print("Input must be under 30,000 characters. Input is currently " + str(len(input_text)) + " characters")
return render_template('index.html')
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)