import gradio as gr import openai import os import re from datetime import datetime # Put your OpenAI API key here openai.api_key = os.getenv('OpenAPI') def gpt3_query(prompt, engine='gpt-3.5-turbo-16k', max_tokens=15050, temperature=0.5, retries=5): for i in range(retries): try: response = openai.ChatCompletion.create( model=engine, messages=[{"role": "system", "content": "You are a creative writer who specializes in crafting engaging stories to sell products or services"}, {"role": "user", "content": prompt}], max_tokens=max_tokens, n=1, temperature=temperature ) return response.choices[0].message['content'].strip() except Exception as e: if i < retries - 1: # i is zero indexed continue else: return f"Error in gpt3_query after {retries} attempts: {str(e)}" def read_file(file_path): with open(file_path, 'r') as file: return file.read() def write_file(file_path, content): with open(file_path, 'w') as file: file.write(content) def generate_story(goal_string): writer_path = "writer.txt" editor_path = "editor.txt" writer_description = read_file(writer_path) editor_description = read_file(editor_path) match = re.search(r"Max word count: (\d+)", writer_description) if match: max_word_count = int(match.group(1)) else: return "Error: Max word count not found in writer.txt", None writer_prompt = f"""Create an engaging and persuasive story that promotes a product or service, based on this goal: {goal_string}. Include a 'step-by-step day in the life' format, showing how the product or service impacts the main character's daily life.""" part = gpt3_query(writer_prompt, temperature=0.7) # Adjust temperature for more focused output writer_prompt = f"Expand the outline with a detailed story. Complete each section for this outline: {part} " next_part = gpt3_query(writer_prompt, temperature=0.7) # Adjust temperature for more focused output new_story1 = next_part iteration = 0 story = "" critique = "" while True: iteration += 1 if iteration >= 4: break editor_prompt = f"With the persona of: {editor_description} \n Provide a detailed critique of this story: {new_story1}" critique = gpt3_query(editor_prompt, max_tokens=6050) writer_prompt = f"Please rewrite the story accommodating the editor's critique. If a section has no feedback, include the unedited version in the output. Current story: {new_story1}\n\nEditor's critique:\n{critique}" next_part1 = gpt3_query(writer_prompt, temperature=0.7, max_tokens=6050) # Adjust temperature for more focused output new_story = next_part1 new_word_count = len(new_story.split()) story = new_story1 #Save the optimized story to a file # Generate a unique filename by appending a timestamp timestamp = datetime.now().strftime("%Y%m%d%H%M%S") filename = f"Personal_Story_{timestamp}.txt" write_file("PersonalStory.txt", story) return "\nPersona Day In the Life of Story Saved", "PersonalStory.txt" iface = gr.Interface( fn=generate_story, inputs=gr.inputs.Textbox(default="Provide an easy-to-read and factual sales story answering: How can the 'persona' utilize 'solution or product' in their daily job? Include an overview, background, then the day in the life of."), outputs=["text", gr.components.File()] ) iface.launch()