Spaces:
Sleeping
Sleeping
File size: 3,204 Bytes
a867756 2225e44 a867756 2225e44 8ede8d4 a867756 2225e44 a867756 2225e44 880ce9e a867756 880ce9e 8ede8d4 880ce9e 8ede8d4 880ce9e 8ede8d4 a867756 8ede8d4 a867756 880ce9e a867756 8ede8d4 880ce9e a867756 8ede8d4 a867756 880ce9e a867756 2225e44 a867756 2225e44 a867756 8ede8d4 2225e44 |
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 |
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.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 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() |