File size: 3,656 Bytes
544b48f
 
 
 
074e59b
544b48f
074e59b
 
544b48f
35aa0fb
769cc8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35aa0fb
544b48f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bdd57c
 
 
 
 
 
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
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()