import gradio as gr import openai from reportlab.pdfgen import canvas # FUNCTION def storyGPT(key, name, situation, direction): messages = [{"role": "system", "content": "You are a inspiring storyteller and therapist who understand young people and students' struggle."}, {"role": "user", "content": "Write the first part amongst 5 of a story about " + name + "who is "+situation+". Include a bold heading above the top starting with 'First part:'. Limit your answer to 4 sentences or less."}] openai.api_key = key first_part_story = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages) system_message = first_part_story["choices"][0]['message'] messages.append(system_message) for i in ["Second"]: new_story_first_half = { "role": "user", "content": f"Write the {i} part of the story among 5 continuing from the previous part. Include a bold heading above the top starting with '{i} part:'. Limit your answer to 4 sentences or less."} messages.append(new_story_first_half) story = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages) system_message = story["choices"][0]["message"] messages.append(system_message) for i in ["Third", "Final"]: new_story_second_half = { "role": "user", "content": f"Write the {i} part of the story among 5 continuing from the previous part"+"in the "+direction+". Include a bold heading above the top starting with '{i} part:'. Limit your answer to 4 sentences or less"} messages.append(new_story_second_half) story = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages) system_message = story["choices"][0]["message"] messages.append(system_message) story_parts = [] for message in messages: if message['role'] == 'assistant': story_parts.append(message['content']) illustration_parts = [] for story_part in story_parts: illustration = openai.Image.create( prompt=story_part, n=1, size="256x256") image_url = illustration['data'][0]['url'] illustration_parts.append(image_url) for i in range(len(story_parts)): story_parts[i] = story_parts[i].replace(". ", ".\n\n") return (story_parts[0], illustration_parts[0], story_parts[1], illustration_parts[1], story_parts[2], illustration_parts[2], story_parts[3], illustration_parts[3]) # EXAMPLE key1 = "Please get your own key" name1 = "Jimmy" situation1 = "broke college student recently graduated from HKU with no job. His girlfriend dumped him and parents disowned him. Living on the streets and see no hope for future" direction1 = "gain motivation from unexpected kindness of people in society around him and eventually find a path to pursue his dream" # INTERFACE with gr.Blocks(title="StoryGPT", css="#button{background-color:#4CAF50} #title{text-align: center} footer{visibility: hidden}") as interface: gr.Markdown( """ , """, elem_id="image") gr.Markdown( """