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('''

Infinite Stories

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