File size: 8,360 Bytes
1031360
d40162f
1031360
 
0802045
 
 
 
 
 
 
 
 
 
 
3a159e9
e6630ec
b2bd8fb
1ecf223
3a159e9
1ecf223
3a159e9
61f86b3
 
3a159e9
1ecf223
 
 
 
3a159e9
1ecf223
 
0802045
1ecf223
3a159e9
 
b2bd8fb
 
3a159e9
1ecf223
 
 
 
 
 
b2bd8fb
1ecf223
 
 
 
48d020e
1ecf223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61f86b3
d40162f
 
1ecf223
 
61f86b3
d40162f
 
b2bd8fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0802045
 
 
b2bd8fb
 
 
0802045
b2bd8fb
 
 
 
 
61f86b3
b2bd8fb
 
 
14d2ae1
 
 
0802045
 
 
 
 
 
 
 
 
 
 
 
14d2ae1
 
 
b2bd8fb
14d2ae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2bd8fb
14d2ae1
 
 
 
 
 
 
 
 
 
 
0802045
14d2ae1
 
 
b2bd8fb
14d2ae1
 
 
5dcee28
 
14d2ae1
c555193
14d2ae1
 
 
 
 
 
 
0802045
14d2ae1
b2bd8fb
61f86b3
 
10fdf42
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import numpy as np
import os
import gradio as gr
import openai
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# Define the credentials to access the Google Sheets API
SCOPE = ["https://www.googleapis.com/auth/drive"]
SERVICE_ACCOUNT_FILE = "./check"

os.environ['GOOGLE_SHEETS_TOKEN']
creds = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_FILE, SCOPE)
client = gspread.authorize(creds)
sheet = client.open("Feedback").sheet1



def continueStory(blank, messageHistory):

    openai.api_key = os.environ['OPENAI_KEY']

    messageHistory.append({"role": "user", "content": blank + "\n\nGenerate the next paragraph with a blank in the last line for me to fill. There should only be one and only one blank in the paragraph. The blank should be at the end of the last line of the paragraph. It is very importat that there is only one blank and that it is at the end!"})
  

    response = openai.ChatCompletion.create(
      model="gpt-3.5-turbo",
      messages = messageHistory
    )
    
    story = response["choices"][0]["message"]["content"]
    messageHistory.append(response["choices"][0]["message"])
    
    return story, messageHistory




with gr.Blocks(css='''
  .gradio-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    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');
    
  }
  h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-top: 1rem;
    margin-bottom: 1rem;
    color: white;
  }
  p {
    font-size: 1.25rem;
    margin-bottom: 2rem;
    color: white;
  }
  label {
    font-size: 1.25rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
  }
  input[type="text"], textarea {
    font-size: 1.25rem;
    padding: 0.5rem;
    border: 2px solid #ccc;
    border-radius: 5px;
    width: 100%;
  }
  .gradio-button {
    background-color: #2196f3;
    color: #fff;
    font-size: 1.25rem;
    font-weight: 500;
    padding: 0.75rem 1.5rem;
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
  }
  .gradio-button:hover {
    background-color: #0c7cd5;
  }
  
''') as demo:
    title = gr.HTML('''
        <div style="text-align: center;">
            <h1 style="color: white; font-size: 3rem; font-weight: 700; margin-bottom: 1rem; color: white;">Infinite Stories</h1>
            <p style="color: white; font-size: 1.25rem; margin-bottom: 2rem; color: white;">Let's see where your imagination can take you!</p>
        </div>
    ''')
    with gr.Row(elem_id='row'):        
      character = gr.Textbox(label='Who will be the main character?', elem_id = 'theme', placeholder="i.e. Diego, an 11-year-old pirate")
      
    messageHistory = gr.State()

    beginBtn = gr.Button("Start!", elem_id="generate-btn")

    story_output = gr.Textbox(label='Story')

    blank = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank')

    with gr.Column(visible=True) as continueStoryCol:
      continueBtn = gr.Button("Continue Story", elem_id="continue-btn") 

    with gr.Column(visible=False) as newStoryCol:
      newStoryBtn = gr.Button("Create New Story", elem_id="new-story-btn") 

    with gr.Column(visible=True) as finishCol:
      finishBtn = gr.Button("Finish Story", elem_id="finish-btn") 
    
    provideFeedbackBtn = gr.Button("Provide Feedback", elem_id="feedback-btn")

    with gr.Column(visible=False) as feedbackForm:
        ratingRadio = gr.Radio(["1", "2", "3", "4", "5"], label="How would you rate the story")
        feedback_text = gr.Textbox(label='Any other comments?')
        submitFeedbackBtn = gr.Button("Submit Feedback", elem_id="submit-feedback-btn")

    with gr.Column(visible=False) as thankyou:
      
      feedback_thankyou = gr.HTML("<h2>Thanks for your feeback!</h2>")

    with gr.Column(visible=False) as mesHistoryCol:
      
      messageHistoryTextBox = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank', value=messageHistory)
      

# the submit feedback btn needs to: send the feedback, 
# hide the elements and provide a message saying thanks for your feedback
    
    def show_feedback_text():
        return {feedbackForm: gr.update(visible=True)}

    def submitFeedback(comment, rating, character, messageHistory):
      
      
 
      readableHistory = ''
      for m in messageHistory:
        readableHistory += m['content']
        readableHistory += '\n\n'

      row = [comment, rating, character, readableHistory]
      sheet.append_row(row)
      return {thankyou: gr.update(visible=True), feedbackForm: gr.update(visible=False)}

    def generateStory(character):
   
      openai.api_key = openai.api_key = os.environ['OPENAI_KEY']

      messageHistory=[
              {"role": "system", "content": "You are co-writing assistant that will help me generate an interactive storytelling game. You are going to generate one paragraph at a time but you are going to leave a blank somewhere in the last line of the paragraph. The user will have to fill this blank in order to generate the next paragraph. You have to write in the style of Julia Donaldson and Doctor Seuss. Make the story funny and exciting. For more context, the game works as follows. 1. You generate one, and only one paragraph, with a blank somewhere in the last line of the paragraph. 2. The user tells you how they want to fill the blank. 3. You generate the next paragraph based on the user’s response to continue the story. This next paragraph also has a blank somewhere in the last line of the paragraph. 4. The user tells you how they want to fill the blank. And so on. Please make sure that when you write a paragraph, there is only one blank and it is at the end of the last line."},
              {"role": "user", "content": f"Please write the first paragraph of a story about {character}. The story needs to be set in a magic island. Make sure you only write one paragraph, that is not too long. Also, make sure is has a blank in the last line of the last paragraph for me to fill. There should only be one blank in the paragraph. The blank should be at the end of the last line of the paragraph. It is very important that there is only one blank and that it is at the end, and that you only generate one paragraph!"}
          ]
      response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages = messageHistory
      )
      
      story = response["choices"][0]["message"]["content"]
      messageHistory.append(response["choices"][0]["message"])
    
      return story, messageHistory

    def finishStory(blank, messageHistory):

      openai.api_key = openai.api_key = os.environ['OPENAI_KEY']

      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."})


      response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages = messageHistory
      )
      
      story = response["choices"][0]["message"]["content"]
      messageHistory.append(response["choices"][0]["message"])
      
      return story, messageHistory

    def createNewStory():
        return {character: gr.update(value=""), story_output: gr.update(value=""), blank:  gr.update(value="")}

    def hideContinueBtn():
      return {continueStoryCol: gr.update(visible=False), newStoryCol: gr.update(visible=True), finishCol: gr.update(visible=False)}


    beginBtn.click(generateStory, inputs=[character], outputs=[story_output, messageHistory])
    
    continueBtn.click(continueStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])

    finishBtn.click(finishStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
    finishBtn.click(hideContinueBtn, [], [continueStoryCol, newStoryCol, finishCol])

    provideFeedbackBtn.click(show_feedback_text, [], [feedbackForm])

    submitFeedbackBtn.click(submitFeedback, [feedback_text, ratingRadio, character, messageHistory], [thankyou, feedbackForm])

    newStoryBtn.click(createNewStory, [], [character, story_output, blank])


demo.launch(debug=True, share=False)