File size: 5,295 Bytes
b56f684
 
78afd3d
 
 
 
b56f684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aff2878
b56f684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import random
import gradio as gr
import os
import random
import openai

os.environ['OPENAI_KEY'] = "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"


def continueStory(story, blank):

    start = story.replace("___", blank)

    openai.api_key = os.environ['OPENAI_KEY']
    messageHistory = []
    messageHistory.append({"role": "user", "content": f"""Generate a humorous, engaging, creative and witty story continuing from the following starting line: 

    {start}

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

    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>
        
        </div>
    ''')
          
    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") 
    
    with gr.Column(visible=False) as mesHistoryCol:
      
      messageHistoryTextBox = gr.Textbox(label='Fill in 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 generateStory():

      file_path = './starters.txt'
      with open(file_path, 'r') as f:
        lines = f.readlines()

        # Randomly select a line and save it to the variable 'start'
        start = random.choice(lines).strip()
      
      return start

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

    def createNewCharacter():
      if characterDropdownCol.visible == True:
        return {characterDropdownCol: gr.update(visible=False), characterTextCol: gr.update(visible=True), createNewCharacterBtn: gr.update(value="Or select a character!")}
      elif characterDropdownCol.visible == False: 
        return {characterDropdownCol: gr.update(visible=True), characterTextCol: gr.update(visible=False), createNewCharacterBtn: gr.update(value="Or create your own!")}

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

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

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

    
demo.launch(share=False)