from openai import OpenAI import gradio as gr # create model for story creation client = OpenAI( base_url="https://api-inference.huggingface.co/v1/", api_key=genai_stories ) # the model to utilize model = "mistralai/Mixtral-8x7B-Instruct-v0.1" #------------------------------------------------------------------------------------------------ # connect with the LLM and create responses def chat_with_llm(model, messages): completion = client.chat.completions.create( model=model, messages=messages, max_tokens=2048, temperature=0) return completion.choices[0].message.content #------------------------------------------------------------------------------------------------ # Initialize the story def generate_story(story_type): global model, messages messages = [] messages.append({"role": "system", "content": f"You are a story creator for kids. The player is a kid, so you must use simple words. The player selects this type of story: {story_type}, according to which you will be asked to create a story. Keep the plot interesting and concise, and write the story as if you talk to children, with simple words. If you are asked to create a story, a question and the answers, then you must create these and only these. Do not create anything else. If you are asked to create the ending of the story, then you must create this and only this. Do not create anything else. "}) messages.append({"role": "user", "content": f"You must create and return ONLY a story part, a question and 4 answers. To do that, you must explicitly follow these steps: 1) Create the first part of the story, according to the story_type from your system content, within 50 and 60 words, without switching to new line. Then you must switch line by adding this: '\n\n'. 2) Then create a question, within 10 and 20 words, on how to proceed the story from step 1. Then you must switch line by adding this: '\n\n'. 3) Then create the 4 potential answers for the question in step 2. The answers of every question must be given in the format: '1:... | 2:... | 3:... | 4:...'. Do not change this format and do not add any new lines between the answers. Every answer must be maximum 20 words. All answers must be separated from each other with '|'. Don't explicitly specify 'Story', 'Question', or 'Answer'. You must reply in this format: '[story from step 1]\n\n[question from step 2]\n\n[answers from step 3]'. Do not create any other stuff."}) messages = [messages[0]] + [messages[-1]] response = chat_with_llm(model, messages) lines = response.split("\n\n") story = lines[0] # Everything before the last two lines is the story question = lines[1] # The second to last line is the question answers = [i.strip() for i in lines[2].split('|')] # The last line contains answers separated by '|' messages.append({"role": "assistant", "content": "I am waiting for next command."}) return story, question, gr.Radio(choices=answers, interactive=True), gr.Radio(choices=[story_type], interactive=False), gr.Button(interactive=False) # Continue the story based on what happened so far and the player's latest answer def continue_story(previous_story, selected_option): global model, messages messages.append({"role": "user", "content": f"You must create and return a story part, a question and 4 answers. To do that, you must explicitly follow these steps: 1) Based on this story so far: '{previous_story} {selected_option}', continue the story and create the next part of the story within 50 and 60 words, without changing lines. You must provide ONLY the new part that you created. Then you must switch line by adding this: '\n\n'. 2) Then create a question, within 10 and 20 words, on how to proceed the story from step 1. Then you must switch line by adding this: '\n\n'. 3) Then create the 4 potential answers for the question in step 2. The answers of every question must be given in the format: '1:... | 2:... | 3:... | 4:...'. Do not change this format and do not add any new lines between the answers. Every answer must be maximum 20 words. All answers must be separated from each other with '|' and no other separator. Don't explicitly specify 'Story', 'Question', or 'Answer'. You must reply in this format: '[story from step 1]\n\n[question from step 2]\n\n[answers from step 3]'. Do not create any other stuff."}) messages = [messages[0]] + [messages[-1]] response = chat_with_llm(model, messages) lines = response.split("\n\n") next_story = lines[0] # Everything before the last two lines is the story question = lines[1] # The second to last line is the question answers = [i.strip() for i in lines[2].split('|')] # The last line contains answers separated by '|' messages.append({"role": "assistant", "content": "I am waiting for next command."}) story = previous_story + '\n\n' + next_story return story, question, gr.Radio(choices=answers, interactive=True) # End the story def end_story(previous_story, selected_option): global model, messages messages.append({"role": "user", "content": f"Please create an ending for this story: '{previous_story}' and considering the latest answer: '{selected_option}'. You must provide only the ending of the story in an exciting way!."}) end_story = chat_with_llm(model, messages) # lines = response.split("\n\n") # end_story = lines[0] # Everything before the last two lines is the story messages.append({"role": "assistant", "content": "I ended the story successfully. Now I am not waiting for any more responses from the player."}) story = previous_story + '\n\n' + end_story return story # initialize messages messages = [] # design the UI with gr.Blocks() as game_ui: with gr.Row(): # Left column for the story with gr.Column(scale=1): story = gr.Textbox(label="Story", interactive=False, lines=12) # Right column for the question, answers, and buttons with gr.Column(scale=1): story_type = gr.Radio(label="What story to create?", choices=["A knight who leads an army attacking a castle", "A kid alien who lands on earth and explores around", "Animals who participate in song contest", "A little princess who is saved by a prince"]) start_button = gr.Button("Start Game!") question = gr.Textbox(label="Question", interactive=False) answers = gr.Radio(label="Choose an answer", choices=[]) submit_button = gr.Button("Submit your answer") end_button = gr.Button("End the story") # what the button do start_button.click(fn=generate_story, inputs=[story_type], outputs=[story, question, answers, story_type, start_button]) submit_button.click(fn=continue_story, inputs=[story, answers], outputs=[story, question, answers]) end_button.click(fn=end_story, inputs=[story, answers], outputs=[story]) # Launch the Gradio interface game_ui.launch(share=True)