Spaces:
Runtime error
Runtime error
rodolfoocampo
commited on
Commit
•
b56f684
1
Parent(s):
8dd1629
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
|
4 |
+
os.environ['OPENAI_KEY'] = "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"
|
5 |
+
import gspread
|
6 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
7 |
+
|
8 |
+
# Define the credentials to access the Google Sheets API
|
9 |
+
SCOPE = ["https://www.googleapis.com/auth/drive"]
|
10 |
+
SERVICE_ACCOUNT_FILE = "jj"
|
11 |
+
|
12 |
+
creds = 'hh'
|
13 |
+
|
14 |
+
def continueStory(story, blank):
|
15 |
+
|
16 |
+
start = story.replace("___", blank)
|
17 |
+
|
18 |
+
openai.api_key = os.environ['OPENAI_KEY']
|
19 |
+
messageHistory = []
|
20 |
+
messageHistory.append({"role": "user", "content": f"""Generate a humorous, engaging, creative and witty story continuing from the following starting line:
|
21 |
+
|
22 |
+
{start}
|
23 |
+
|
24 |
+
This will be a short story of at most 5 sentences. Make sure to keep the starting line in the text you write. Write using dry humour and witty style."""})
|
25 |
+
|
26 |
+
|
27 |
+
response = openai.ChatCompletion.create(
|
28 |
+
model="gpt-3.5-turbo",
|
29 |
+
messages = messageHistory
|
30 |
+
)
|
31 |
+
|
32 |
+
story = response["choices"][0]["message"]["content"]
|
33 |
+
messageHistory.append(response["choices"][0]["message"])
|
34 |
+
|
35 |
+
return story, messageHistory
|
36 |
+
|
37 |
+
with gr.Blocks(css='''
|
38 |
+
.gradio-container {
|
39 |
+
display: flex;
|
40 |
+
flex-direction: column;
|
41 |
+
justify-content: center;
|
42 |
+
align-items: center;
|
43 |
+
background-image: url('file=https://cdn.discordapp.com/attachments/941582479117127680/1080730421425344542/rodotcom_colorful_abstract_illustration_for_the_background_of_c_f1b331d7-6493-4a33-9063-345d31a66ddb.png');
|
44 |
+
|
45 |
+
}
|
46 |
+
h1 {
|
47 |
+
font-size: 3rem;
|
48 |
+
font-weight: 700;
|
49 |
+
margin-top: 1rem;
|
50 |
+
margin-bottom: 1rem;
|
51 |
+
color: white;
|
52 |
+
}
|
53 |
+
p {
|
54 |
+
font-size: 1.25rem;
|
55 |
+
margin-bottom: 2rem;
|
56 |
+
color: white;
|
57 |
+
}
|
58 |
+
label {
|
59 |
+
font-size: 1.25rem;
|
60 |
+
font-weight: 500;
|
61 |
+
margin-bottom: 0.5rem;
|
62 |
+
}
|
63 |
+
input[type="text"], textarea {
|
64 |
+
font-size: 1.25rem;
|
65 |
+
padding: 0.5rem;
|
66 |
+
border: 2px solid #ccc;
|
67 |
+
border-radius: 5px;
|
68 |
+
width: 100%;
|
69 |
+
}
|
70 |
+
.gradio-button {
|
71 |
+
background-color: #2196f3;
|
72 |
+
color: #fff;
|
73 |
+
font-size: 1.25rem;
|
74 |
+
font-weight: 500;
|
75 |
+
padding: 0.75rem 1.5rem;
|
76 |
+
border-radius: 5px;
|
77 |
+
cursor: pointer;
|
78 |
+
transition: all 0.2s ease-in-out;
|
79 |
+
}
|
80 |
+
.gradio-button:hover {
|
81 |
+
background-color: #0c7cd5;
|
82 |
+
}
|
83 |
+
|
84 |
+
''') as demo:
|
85 |
+
title = gr.HTML('''
|
86 |
+
<div style="text-align: center;">
|
87 |
+
<h1 style="color: white; font-size: 3rem; font-weight: 700; margin-bottom: 1rem; color: white;">Infinite Stories</h1>
|
88 |
+
|
89 |
+
</div>
|
90 |
+
''')
|
91 |
+
|
92 |
+
messageHistory = gr.State()
|
93 |
+
|
94 |
+
beginBtn = gr.Button("Start!", elem_id="generate-btn")
|
95 |
+
|
96 |
+
story_output = gr.Textbox(label='Story')
|
97 |
+
|
98 |
+
blank = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank')
|
99 |
+
|
100 |
+
with gr.Column(visible=True) as continueStoryCol:
|
101 |
+
continueBtn = gr.Button("Continue Story", elem_id="continue-btn")
|
102 |
+
|
103 |
+
with gr.Column(visible=False) as newStoryCol:
|
104 |
+
newStoryBtn = gr.Button("Create New Story", elem_id="new-story-btn")
|
105 |
+
|
106 |
+
with gr.Column(visible=True) as finishCol:
|
107 |
+
finishBtn = gr.Button("Finish Story", elem_id="finish-btn")
|
108 |
+
|
109 |
+
with gr.Column(visible=False) as mesHistoryCol:
|
110 |
+
|
111 |
+
messageHistoryTextBox = gr.Textbox(label='Fill in the blank?', elem_id = 'blank', value=messageHistory)
|
112 |
+
|
113 |
+
# the submit feedback btn needs to: send the feedback,
|
114 |
+
# hide the elements and provide a message saying thanks for your feedback
|
115 |
+
|
116 |
+
|
117 |
+
def generateStory():
|
118 |
+
|
119 |
+
file_path = '/content/drive/MyDrive/InfiniteStories/Premises/starts.txt'
|
120 |
+
with open(file_path, 'r') as f:
|
121 |
+
lines = f.readlines()
|
122 |
+
|
123 |
+
# Randomly select a line and save it to the variable 'start'
|
124 |
+
start = random.choice(lines).strip()
|
125 |
+
|
126 |
+
return start
|
127 |
+
|
128 |
+
def finishStory(blank, messageHistory):
|
129 |
+
|
130 |
+
openai.api_key = openai.api_key = os.environ['OPENAI_KEY']
|
131 |
+
|
132 |
+
messageHistory.append({"role": "user", "content": blank + "\n\nNow bring the story to a close. Write the necessary paragraphs to make it have a happy or funny ending. No need to leave a blank anymore."})
|
133 |
+
|
134 |
+
response = openai.ChatCompletion.create(
|
135 |
+
model="gpt-3.5-turbo",
|
136 |
+
messages = messageHistory
|
137 |
+
)
|
138 |
+
|
139 |
+
story = response["choices"][0]["message"]["content"]
|
140 |
+
messageHistory.append(response["choices"][0]["message"])
|
141 |
+
|
142 |
+
return story, messageHistory
|
143 |
+
|
144 |
+
def createNewStory():
|
145 |
+
return {character: gr.update(value=""), story_output: gr.update(value=""), blank: gr.update(value="")}
|
146 |
+
|
147 |
+
def hideContinueBtn():
|
148 |
+
return {continueStoryCol: gr.update(visible=False), newStoryCol: gr.update(visible=True), finishCol: gr.update(visible=False)}
|
149 |
+
|
150 |
+
def createNewCharacter():
|
151 |
+
if characterDropdownCol.visible == True:
|
152 |
+
return {characterDropdownCol: gr.update(visible=False), characterTextCol: gr.update(visible=True), createNewCharacterBtn: gr.update(value="Or select a character!")}
|
153 |
+
elif characterDropdownCol.visible == False:
|
154 |
+
return {characterDropdownCol: gr.update(visible=True), characterTextCol: gr.update(visible=False), createNewCharacterBtn: gr.update(value="Or create your own!")}
|
155 |
+
|
156 |
+
beginBtn.click(generateStory, outputs=[story_output])
|
157 |
+
|
158 |
+
continueBtn.click(continueStory, inputs=[story_output, blank], outputs=[story_output, messageHistory])
|
159 |
+
|
160 |
+
#finishBtn.click(finishStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
|
161 |
+
#finishBtn.click(hideContinueBtn, [], [continueStoryCol, newStoryCol, finishCol])
|
162 |
+
|
163 |
+
newStoryBtn.click(createNewStory, [], [story_output, blank])
|
164 |
+
|
165 |
+
|
166 |
+
demo.launch(share=False)
|