Spaces:
Sleeping
Sleeping
| 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""" | |
| I need you to help me create a template that will allow us to generate the same style of e-commerce copy as the reference(s) included below. | |
| Step 1: | |
| Analyse what parts the references are created from. Are there specific bullet point? Are features mentioned in a specific order? What kinds of text and features are mentioned? | |
| Step 2: | |
| For each of those parts, break down the tone of voice, the cadence and rythm, and the perspective of the writing. | |
| For each part, also see if there is a common length and strucutre. | |
| Step 3: | |
| Describe each of the parts, so a skilled copywriter could take those and use them as instructions to write about another product | |
| Step 4: | |
| For each part, turn it into our templating language. These are the specifications: | |
| - If something needs to be generated, it should be inside a Generator block. These are defined by being enclosed in double curly braces. | |
| - inside the double curly braces, we have a reference name for the block, followed by a pipe, and then a short description of the tone, then the structure, and then instructions. Like this: | |
| - {{{{ product_feature | upbeat, 3rd person, rythmic. paragraph, 40-60 words. make sure to mention the brand name }}}} | |
| - If we need a control structure (loop, conditional) we wrap these in single curly braces with percentage signs. These should not be written out, but if there is a generator inside, that will be used. some examples: | |
| {{% if the product has a pattern %}} | |
| {{{{ pattern_desciption | direct, monotone. 2 short sentences. Describe the pattern}}}} | |
| {{% end if %}} | |
| {{% for top 5 features of the product %}} | |
| {{{{ feature_bullet | aspirational, humorous. A title for the feature, followed by a single sentence about the feature}}}} | |
| {{% end %}} | |
| Step 5: | |
| Return only the template... no explations or anything... | |
| **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="o3-mini", # 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_completion_tokens=4096 | |
| ) | |
| 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() |