Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +82 -0
- editor.txt +3 -0
- requirements.txt.txt +2 -0
- writer.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
|
| 8 |
+
def gpt3_query(prompt, engine='gpt-3.5-turbo-16k', max_tokens=15050, temperature=0.5):
|
| 9 |
+
try:
|
| 10 |
+
response = openai.ChatCompletion.create(
|
| 11 |
+
model=engine,
|
| 12 |
+
messages=[{"role": "system", "content": "You are a creative writer who specializes in crafting engaging stories to sell products or services"},
|
| 13 |
+
{"role": "user", "content": prompt}],
|
| 14 |
+
max_tokens=max_tokens,
|
| 15 |
+
n=1,
|
| 16 |
+
temperature=temperature
|
| 17 |
+
)
|
| 18 |
+
return response.choices[0].message['content'].strip()
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error in gpt3_query: {str(e)}", None
|
| 21 |
+
|
| 22 |
+
def read_file(file_path):
|
| 23 |
+
with open(file_path, 'r') as file:
|
| 24 |
+
return file.read()
|
| 25 |
+
|
| 26 |
+
def write_file(file_path, content):
|
| 27 |
+
with open(file_path, 'w') as file:
|
| 28 |
+
file.write(content)
|
| 29 |
+
|
| 30 |
+
def generate_story(goal_string):
|
| 31 |
+
writer_path = "writer.txt"
|
| 32 |
+
editor_path = "editor.txt"
|
| 33 |
+
|
| 34 |
+
writer_description = read_file(writer_path)
|
| 35 |
+
editor_description = read_file(editor_path)
|
| 36 |
+
|
| 37 |
+
match = re.search(r"Max word count: (\d+)", writer_description)
|
| 38 |
+
if match:
|
| 39 |
+
max_word_count = int(match.group(1))
|
| 40 |
+
else:
|
| 41 |
+
return "Error: Max word count not found in writer.txt", None
|
| 42 |
+
|
| 43 |
+
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."""
|
| 44 |
+
part = gpt3_query(writer_prompt, temperature=0.7) # Adjust temperature for more focused output
|
| 45 |
+
|
| 46 |
+
writer_prompt = f"Expand the outline with a detailed story. Complete each section for this outline: {part} "
|
| 47 |
+
next_part = gpt3_query(writer_prompt, temperature=0.7) # Adjust temperature for more focused output
|
| 48 |
+
new_story1 = next_part
|
| 49 |
+
|
| 50 |
+
iteration = 0
|
| 51 |
+
story = ""
|
| 52 |
+
critique = ""
|
| 53 |
+
|
| 54 |
+
while True:
|
| 55 |
+
iteration += 1
|
| 56 |
+
if iteration >= 4:
|
| 57 |
+
break
|
| 58 |
+
|
| 59 |
+
editor_prompt = f"With the persona of: {editor_description} \n Provide a detailed critique of this story: {new_story1}"
|
| 60 |
+
critique = gpt3_query(editor_prompt, max_tokens=6050)
|
| 61 |
+
|
| 62 |
+
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}"
|
| 63 |
+
|
| 64 |
+
next_part1 = gpt3_query(writer_prompt, temperature=0.7, max_tokens=6050) # Adjust temperature for more focused output
|
| 65 |
+
|
| 66 |
+
new_story = next_part1
|
| 67 |
+
|
| 68 |
+
new_word_count = len(new_story.split())
|
| 69 |
+
|
| 70 |
+
story = new_story1
|
| 71 |
+
|
| 72 |
+
#Save the optimized story to a file
|
| 73 |
+
|
| 74 |
+
# Generate a unique filename by appending a timestamp
|
| 75 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 76 |
+
filename = f"Personal_Story_{timestamp}.txt"
|
| 77 |
+
|
| 78 |
+
write_file("PersonalStory.txt", story)
|
| 79 |
+
return "\nPersona Day In the Life of Story Saved", "PersonalStory.txt"
|
| 80 |
+
|
| 81 |
+
iface = gr.Interface(fn=generate_story, inputs="text", outputs=["text", gr.components.File()])
|
| 82 |
+
iface.launch()
|
editor.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Persona: A professional news editor with a background in factual writing, skilled in refining narratives, enhancing the structure and flow of content, and ensuring logical consistency within sales narratives. Possess a keen eye for detail and a deep understanding of the importance of accuracy and fact-checking. Ensure that the content is easily understood by the target audience.
|
| 2 |
+
|
| 3 |
+
Goal: ensure a compelling main character, logical arc of your story, Focus on showing rather than telling with case studies and 'day in the life of'
|
requirements.txt.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==0.27.0
|
| 2 |
+
gradio==2.3.9
|
writer.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Persona: <1> a master story writer who is now using the skills of story crafting to write sales copy
|
| 2 |
+
|
| 3 |
+
Goal: <1> Provide an easy-to-read and factual sales story answering: How can the Global Workplace Operation Lead for HP utilize Cognitive AI Agents in their daily job? Include an overview, background then the day in the life of.
|
| 4 |
+
|
| 5 |
+
Max word count: 3580
|