Upload 2 files
Browse files- app.py +140 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
from reportlab.pdfgen import canvas
|
4 |
+
|
5 |
+
# FUNCTION
|
6 |
+
|
7 |
+
|
8 |
+
def storyGPT(key, name, situation, direction):
|
9 |
+
messages = [{"role": "system", "content": "You are a inspiring storyteller and therapist who understand young people and students' struggle."},
|
10 |
+
{"role": "user", "content": "Write the first part amongst 5 of a story about " + name + "who is "+situation+". Include a bold heading above the top starting with 'First part:'. Limit your answer to 4 sentences or less."}]
|
11 |
+
|
12 |
+
openai.api_key = key
|
13 |
+
first_part_story = openai.ChatCompletion.create(
|
14 |
+
model="gpt-3.5-turbo", messages=messages)
|
15 |
+
|
16 |
+
system_message = first_part_story["choices"][0]['message']
|
17 |
+
messages.append(system_message)
|
18 |
+
|
19 |
+
for i in ["Second"]:
|
20 |
+
new_story_first_half = {
|
21 |
+
"role": "user", "content": f"Write the {i} part of the story among 5 continuing from the previous part. Include a bold heading above the top starting with '{i} part:'. Limit your answer to 4 sentences or less."}
|
22 |
+
messages.append(new_story_first_half)
|
23 |
+
story = openai.ChatCompletion.create(
|
24 |
+
model="gpt-3.5-turbo", messages=messages)
|
25 |
+
system_message = story["choices"][0]["message"]
|
26 |
+
messages.append(system_message)
|
27 |
+
|
28 |
+
for i in ["Third", "Final"]:
|
29 |
+
new_story_second_half = {
|
30 |
+
"role": "user", "content": f"Write the {i} part of the story among 5 continuing from the previous part"+"in the "+direction+". Include a bold heading above the top starting with '{i} part:'. Limit your answer to 4 sentences or less"}
|
31 |
+
messages.append(new_story_second_half)
|
32 |
+
story = openai.ChatCompletion.create(
|
33 |
+
model="gpt-3.5-turbo", messages=messages)
|
34 |
+
system_message = story["choices"][0]["message"]
|
35 |
+
messages.append(system_message)
|
36 |
+
|
37 |
+
story_parts = []
|
38 |
+
for message in messages:
|
39 |
+
if message['role'] == 'assistant':
|
40 |
+
story_parts.append(message['content'])
|
41 |
+
|
42 |
+
illustration_parts = []
|
43 |
+
|
44 |
+
for story_part in story_parts:
|
45 |
+
illustration = openai.Image.create(
|
46 |
+
prompt=story_part,
|
47 |
+
n=1,
|
48 |
+
size="256x256")
|
49 |
+
image_url = illustration['data'][0]['url']
|
50 |
+
illustration_parts.append(image_url)
|
51 |
+
|
52 |
+
for i in range(len(story_parts)):
|
53 |
+
story_parts[i] = story_parts[i].replace(". ", ".\n\n")
|
54 |
+
|
55 |
+
return (story_parts[0], illustration_parts[0], story_parts[1], illustration_parts[1], story_parts[2], illustration_parts[2], story_parts[3], illustration_parts[3])
|
56 |
+
|
57 |
+
|
58 |
+
# EXAMPLE
|
59 |
+
|
60 |
+
key1 = "Please get your own key"
|
61 |
+
name1 = "Jimmy"
|
62 |
+
situation1 = "broke college student recently graduated from HKU with no job. His girlfriend dumped him and parents disowned him. Living on the streets and see no hope for future"
|
63 |
+
direction1 = "gain motivation from unexpected kindness of people in society around him and eventually find a path to pursue his dream"
|
64 |
+
|
65 |
+
# INTERFACE
|
66 |
+
|
67 |
+
with gr.Blocks(title="StoryGPT", css="#button{background-color:#4CAF50} #title{text-align: center} footer{visibility: hidden}") as interface:
|
68 |
+
|
69 |
+
gr.Markdown(
|
70 |
+
"""
|
71 |
+
<img src="https://lean.social/wp-content/uploads/2023/02/logo_notext-1.svg"
|
72 |
+
alt="Markdown Monster icon"
|
73 |
+
style="margin-left:auto; margin-right:auto;margin-top:20px; max-width: 100px; padding-bottom:0px; margin-bottom:-50px" />,
|
74 |
+
""", elem_id="image")
|
75 |
+
|
76 |
+
gr.Markdown(
|
77 |
+
"""
|
78 |
+
<h1 style="margin-bottom:0px"> StoryGPT</h1>
|
79 |
+
""", elem_id="title")
|
80 |
+
|
81 |
+
with gr.Row(elem_id="main"):
|
82 |
+
|
83 |
+
with gr.Column(scale=1):
|
84 |
+
|
85 |
+
key = gr.Textbox(label="Your OPENAI api key")
|
86 |
+
name = gr.Textbox(label="Character's name")
|
87 |
+
situation = gr.Textbox(
|
88 |
+
label="What is the character's life situation?")
|
89 |
+
direction = gr.Textbox(
|
90 |
+
label="What direction do you want the story to go?")
|
91 |
+
Generate_btn = gr.Button(value="Generate", elem_id="button")
|
92 |
+
examples = gr.Examples(examples=[[key1, name1, situation1, direction1]], inputs=[
|
93 |
+
key, name, situation, direction], label="Click here to test with our example")
|
94 |
+
|
95 |
+
with gr.Column(scale=7, container=False):
|
96 |
+
|
97 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
98 |
+
with gr.Column(scale=3):
|
99 |
+
story1 = gr.Textbox(show_label=False, lines=9).style(
|
100 |
+
container=False)
|
101 |
+
|
102 |
+
with gr.Column(scale=1):
|
103 |
+
illustration1 = gr.Image(
|
104 |
+
show_label=False, brush_radius=0).style(height=200)
|
105 |
+
|
106 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
107 |
+
with gr.Column(scale=3):
|
108 |
+
story2 = gr.Textbox(show_label=False, lines=9).style(
|
109 |
+
container=False)
|
110 |
+
|
111 |
+
with gr.Column(scale=1):
|
112 |
+
illustration2 = gr.Image(
|
113 |
+
show_label=False, brush_radius=0, lines=9).style(height=200)
|
114 |
+
|
115 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
116 |
+
with gr.Column(scale=3):
|
117 |
+
story3 = gr.Textbox(show_label=False, lines=9).style(
|
118 |
+
container=False)
|
119 |
+
|
120 |
+
with gr.Column(scale=1):
|
121 |
+
illustration3 = gr.Image(
|
122 |
+
show_label=False, brush_radius=0).style(height=200)
|
123 |
+
|
124 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
125 |
+
with gr.Column(scale=3):
|
126 |
+
story4 = gr.Textbox(show_label=False, lines=9).style(
|
127 |
+
container=False)
|
128 |
+
|
129 |
+
with gr.Column(scale=1):
|
130 |
+
illustration4 = gr.Image(
|
131 |
+
show_label=False, brush_radius=0).style(height=200)
|
132 |
+
|
133 |
+
inputs = [key, name, situation, direction]
|
134 |
+
outputs = [story1, illustration1, story2,
|
135 |
+
illustration2, story3, illustration3, story4, illustration4]
|
136 |
+
|
137 |
+
Generate_btn.click(storyGPT, inputs=inputs, outputs=outputs)
|
138 |
+
|
139 |
+
|
140 |
+
interface.launch(favicon_path="Logo round bg.png")
|
requirements.txt
ADDED
Binary file (2.67 kB). View file
|
|