CopyHelper / app.py
peterhartwigCF's picture
Update app.py
a867756 verified
raw
history blame
3.25 kB
import os
import gradio as gr
import openai
# Set your OpenAI API key (ensure this is set in your environment)
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_template(ref1, ref2, ref3, ref4, ref5):
# Collect non-empty reference copies
references = [ref.strip() for ref in [ref1, ref2, ref3, ref4, ref5] if ref.strip()]
if not references:
return "Please provide at least one piece of reference copy."
combined_references = "\n\n".join(references)
# Build a detailed prompt that includes a summary of the guide document directions.
prompt = f"""
You are an expert in e-commerce copywriting. Your task is to analyze the following reference copy examples and generate a structured e-commerce copywriting template. Follow these guidelines extracted from our documentation:
1. The template must start with a header enclosed in square brackets. The header includes key-value pairs such as [type: UK website, style=true, language=English].
2. After the header, include template blocks enclosed in double curly braces {{ }}. These blocks define sections like an introduction, feature descriptions, technical specifications, and a conclusion.
3. Use control structures (enclosed in {{% %}}) for iterating over collections (for example, listing product features as a bulleted list).
4. Include clear instructions and modifiers within the template blocks (e.g., tone, style, length).
5. Ensure consistency, clarity, and adaptability for various products.
Your generated template should include:
- A header section.
- An introduction block.
- An example control structure for features.
- A conclusion block.
Reference Copy:
{combined_references}
Generate the final structured template that adheres to these directions.
"""
# Call the OpenAI ChatCompletion API with the constructed prompt
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # or your preferred model
messages=[
{"role": "system", "content": "You are a helpful assistant that generates structured e-commerce copywriting templates."},
{"role": "user", "content": prompt}
],
max_tokens=512,
temperature=0.7,
)
template_output = response["choices"][0]["message"]["content"].strip()
return template_output
iface = gr.Interface(
fn=generate_template,
inputs=[
gr.Textbox(label="Reference Copy 1", lines=3, placeholder="Paste your reference copy here"),
gr.Textbox(label="Reference Copy 2 (optional)", lines=3, placeholder="Paste your reference copy here"),
gr.Textbox(label="Reference Copy 3 (optional)", lines=3, placeholder="Paste your reference copy here"),
gr.Textbox(label="Reference Copy 4 (optional)", lines=3, placeholder="Paste your reference copy here"),
gr.Textbox(label="Reference Copy 5 (optional)", lines=3, placeholder="Paste your reference copy here")
],
outputs=gr.Textbox(label="Structured Template"),
title="E-commerce Template Generator using OpenAI API",
description="Provide up to five pieces of reference copy. This app calls the OpenAI API to analyze your inputs and generate a structured template based on the e-commerce copywriting guide."
)
iface.launch()