rodolfoocampo commited on
Commit
cdeaff9
β€’
1 Parent(s): 62eec3f

Create new file

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import StableDiffusionPipeline
3
+ from torch import autocast
4
+ import numpy as np
5
+ import gradio as gr
6
+ import openai
7
+ import os
8
+
9
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16, use_auth_token=True)
10
+
11
+ pipe = pipe.to("cuda")
12
+
13
+ def generateStory(theme1, theme2):
14
+ openai.api_key = os.environ.get('openai-key')
15
+ prompt_text = "Write the first paragraph of a story integrates the themes \"{}\" and \"{}\" in a creative way.\n\nFirst paragraph of story:\n\n".format(theme1,theme2)
16
+ response = openai.Completion.create(
17
+ engine="text-davinci-002",
18
+ prompt=prompt_text,
19
+ temperature=0.7,
20
+ max_tokens=250,
21
+ top_p=1,
22
+ frequency_penalty=0,
23
+ presence_penalty=0
24
+ )
25
+ story = response["choices"][0]["text"]
26
+
27
+ illustration_response = openai.Completion.create(
28
+ model="text-davinci-002",
29
+ prompt="Transform the following story into a caption of an accompanying illustration. Start with 'Beautiful digital illustration of':\n\nStory:\n\nI stand at the edge of the Blue Mountains and gaze out at the vastness before me. It's a beautiful day, and the sun is shining. I can see for miles and miles, and it feels like I'm standing at the edge of the world. I'm here with the person I love, and we're about to embark on a great adventure. I can't wait to explore every inch of this place with them.\n\nIllustration caption:\n\nBeautiful digital illustration of two people standing by the edge of a mountain holding hands looking out\n\nStory:\n\n{}\n\nIllustration caption:".format(story),
30
+ temperature=0.7,
31
+ max_tokens=256,
32
+ top_p=1,
33
+ frequency_penalty=0,
34
+ presence_penalty=0
35
+ )
36
+
37
+ image_prompt = illustration_response["choices"][0]["text"]
38
+
39
+ content_to_classify = "Your content here"
40
+
41
+ response = openai.Completion.create(
42
+ model="content-filter-alpha",
43
+ prompt = "<|endoftext|>"+story+"\n--\nLabel:",
44
+ temperature=0,
45
+ max_tokens=1,
46
+ top_p=0,
47
+ logprobs=10
48
+ )
49
+
50
+ output_label = response["choices"][0]["text"]
51
+
52
+ # This is the probability at which we evaluate that a "2" is likely real
53
+ # vs. should be discarded as a false positive
54
+ toxic_threshold = -0.355
55
+
56
+ if output_label != "2":
57
+ prompt = image_prompt
58
+
59
+ with autocast("cuda"):
60
+ image = pipe(prompt).images[0] # image here is in [PIL format](https://pillow.readthedocs.io/en/stable/)
61
+ else:
62
+ story='Please generate again'
63
+ image = np.zeros([100,100,3],dtype=np.uint8)
64
+ image.fill(255) # or img[:] = 255
65
+
66
+ if story.startswith('\n\n'):
67
+ story = story[2:]
68
+ return story, image
69
+
70
+ '''
71
+ demo = gr.Interface(
72
+ fn=themes,
73
+
74
+
75
+ inputs=["text", "text"],
76
+ outputs=["text", "image"],
77
+ )
78
+
79
+ demo.launch()
80
+ '''
81
+
82
+
83
+
84
+ with gr.Blocks(css='''
85
+
86
+ .h1 {
87
+
88
+ font-family: HK Grotesk;
89
+ font-style: normal;
90
+ font-weight: bold;
91
+ font-size: 100px;
92
+ line-height: 105%;
93
+ margin: 0;
94
+ }
95
+
96
+
97
+ ''') as demo:
98
+ title = gr.HTML(
99
+ """
100
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
101
+ <div
102
+ style="
103
+ display: inline-flex;
104
+ align-items: center;
105
+ gap: 0.8rem;
106
+ font-size: 3rem;
107
+
108
+ font-style: normal;
109
+ font-weight: bold;
110
+ "
111
+ >
112
+
113
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
114
+ Illustrated Narrative Device
115
+ </h1>
116
+ </div>
117
+ <p style="margin-bottom: 10px; font-size: 94%;">
118
+ Tickle your imagination* by writing two themes that AI will try to connect in a story and illustration. <br><br> *Use as an imperfect tool for imaginative thought through creative curiosity.
119
+
120
+ </p>
121
+
122
+ </div>
123
+ """)
124
+ with gr.Row():
125
+ theme1 = gr.Textbox(label='Theme 1', elem_id = 'theme')
126
+ theme2 = gr.Textbox(label='Theme 2', elem_id = 'theme')
127
+
128
+ b1 = gr.Button("Generate", elem_id="generate-btn")
129
+
130
+ story_output = gr.Textbox(label='Story')
131
+ illustration = gr.Image(label='Illustration')
132
+
133
+
134
+ gr.HTML('<div style="text-align: center; max-width: 650px; margin: 0 auto; "><p style="margin-bottom: 10px; font-size: 94%;">Compute credits are expensive. Please help me keep this experiment running by buying me a coffee <a href="https://www.buymeacoffee.com/jrodolfoocG"> <u><b>here</u></b> :) </a></p></div><br>')
135
+ gr.HTML('<div style="text-align: center; max-width: 650px; margin: 0 auto; "><p style="margin-bottom: 10px; font-size: 70%;">Built with GPT-3, Stable Diffusion, the Diffusers library and Gradio, by <a href="https://research.rodolfoocampo.com"><u><b>Rodolfo Ocampo</u></b></a></p></div>')
136
+
137
+
138
+
139
+ b1.click(generateStory, inputs=[theme1,theme2], outputs=[story_output, illustration])
140
+
141
+ demo.launch()