Spaces:
Build error
Build error
Commit
•
ca2997d
1
Parent(s):
ace6254
Upload 3 files
Browse files- Logo round bg.png +0 -0
- app.py +150 -0
- requirements.txt +0 -0
Logo round bg.png
ADDED
![]() |
app.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = "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"
|
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 by LEAN Social 😿</h1>
|
79 |
+
<h5 style="font-weight:normal; margin-top:0px">Visit <a href="https://lean.social">www.lean.social</a> to learn how we help young people study<h/5>
|
80 |
+
<h4 style="margin-bottom:-30px; font-weight: normal"> Are you a student struggling with <strong><mark style="background-color:white; color:black">  peer, family, and society pressure?  </mark></strong></h3>
|
81 |
+
<h4 style="font-weight: normal"> Paste in your life's struggles to see how it can be improved!</h3>
|
82 |
+
""", elem_id="title")
|
83 |
+
|
84 |
+
with gr.Row(elem_id="main"):
|
85 |
+
|
86 |
+
with gr.Column(scale=1):
|
87 |
+
gr.Markdown(
|
88 |
+
"""
|
89 |
+
<h3>Start Here</h3>
|
90 |
+
To find your OPEN API key, login to your OPENAI account and go <a href="https://platform.openai.com/account/api-keys">here</a>.<br><br>
|
91 |
+
You can also click the example below to use my key for trial. <br><br>
|
92 |
+
Only 1 story per user can be generated in 1 minute. Please use your API key if you can.
|
93 |
+
"""
|
94 |
+
)
|
95 |
+
key = gr.Textbox(label="Your OPENAI api key")
|
96 |
+
name = gr.Textbox(label="Character's name")
|
97 |
+
situation = gr.Textbox(
|
98 |
+
label="What is the character's life situation?")
|
99 |
+
direction = gr.Textbox(
|
100 |
+
label="What direction do you want the story to go?")
|
101 |
+
Generate_btn = gr.Button(value="Generate", elem_id="button")
|
102 |
+
examples = gr.Examples(examples=[[key1, name1, situation1, direction1]], inputs=[
|
103 |
+
key, name, situation, direction], label="Click here to test with our example")
|
104 |
+
|
105 |
+
with gr.Column(scale=7, container=False):
|
106 |
+
|
107 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
108 |
+
with gr.Column(scale=3):
|
109 |
+
story1 = gr.Textbox(show_label=False, lines=9).style(
|
110 |
+
container=False)
|
111 |
+
|
112 |
+
with gr.Column(scale=1):
|
113 |
+
illustration1 = gr.Image(
|
114 |
+
show_label=False, brush_radius=0).style(height=200)
|
115 |
+
|
116 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
117 |
+
with gr.Column(scale=3):
|
118 |
+
story2 = gr.Textbox(show_label=False, lines=9).style(
|
119 |
+
container=False)
|
120 |
+
|
121 |
+
with gr.Column(scale=1):
|
122 |
+
illustration2 = gr.Image(
|
123 |
+
show_label=False, brush_radius=0, lines=9).style(height=200)
|
124 |
+
|
125 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
126 |
+
with gr.Column(scale=3):
|
127 |
+
story3 = gr.Textbox(show_label=False, lines=9).style(
|
128 |
+
container=False)
|
129 |
+
|
130 |
+
with gr.Column(scale=1):
|
131 |
+
illustration3 = gr.Image(
|
132 |
+
show_label=False, brush_radius=0).style(height=200)
|
133 |
+
|
134 |
+
with gr.Row(variant="compact").style(equal_height=True):
|
135 |
+
with gr.Column(scale=3):
|
136 |
+
story4 = gr.Textbox(show_label=False, lines=9).style(
|
137 |
+
container=False)
|
138 |
+
|
139 |
+
with gr.Column(scale=1):
|
140 |
+
illustration4 = gr.Image(
|
141 |
+
show_label=False, brush_radius=0).style(height=200)
|
142 |
+
|
143 |
+
inputs = [key, name, situation, direction]
|
144 |
+
outputs = [story1, illustration1, story2,
|
145 |
+
illustration2, story3, illustration3, story4, illustration4]
|
146 |
+
|
147 |
+
Generate_btn.click(storyGPT, inputs=inputs, outputs=outputs)
|
148 |
+
|
149 |
+
|
150 |
+
interface.launch(favicon_path="Logo round bg.png")
|
requirements.txt
ADDED
Binary file (2.67 kB). View file
|
|