CopyHelper / app.py
peterhartwigCF's picture
Update app.py
be2ecd1 verified
raw
history blame
3.21 kB
import os
import gradio as gr
import openai
# Set your OpenAI API key (ensure this is set in your environment variables)
openai.api_key = os.environ["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 with properly escaped curly braces for literal output.
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. **Header Section:**
- Must be enclosed in square brackets (e.g., `[type: UK website, style=true, language=English]`).
2. **Template Blocks:**
- Enclosed in double curly braces `{{{{ }}}}`.
- Include sections such as an introduction block, feature descriptions, technical specifications, and a conclusion block.
3. **Control Structures:**
- Enclosed in `{{% %}}` tags to iterate over lists (for example, to generate a bulleted list of features).
4. **Instructions and Modifiers:**
- Include clear guidance for tone, style, and length (e.g., `motivational, passionate, 1-2 sentences`).
Your generated template should include:
- A header section.
- An introduction block.
- An example control structure for iterating over features.
- A conclusion block.
**Reference Copy:**
{combined_references}
Generate the final structured template that adheres to these guidelines.
"""
# Call the OpenAI API using the new ChatCompletion interface
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. The app calls the OpenAI API to analyze your inputs and generate a structured template based on the e-commerce copywriting guide."
)
iface.launch()