Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,52 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
def generate_template(ref1, ref2, ref3, ref4, ref5):
|
| 8 |
-
# Collect non-empty
|
| 9 |
references = [ref.strip() for ref in [ref1, ref2, ref3, ref4, ref5] if ref.strip()]
|
| 10 |
if not references:
|
| 11 |
return "Please provide at least one piece of reference copy."
|
| 12 |
|
| 13 |
-
# Combine all references into one prompt text
|
| 14 |
combined_references = "\n\n".join(references)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
result = o3_mini(prompt, max_length=512, truncation=True)
|
| 30 |
-
|
| 31 |
-
# The model returns a list of dictionaries; extract the generated text.
|
| 32 |
-
template_output = result[0]['generated_text']
|
| 33 |
return template_output
|
| 34 |
|
| 35 |
iface = gr.Interface(
|
|
@@ -42,11 +59,8 @@ iface = gr.Interface(
|
|
| 42 |
gr.Textbox(label="Reference Copy 5 (optional)", lines=3, placeholder="Paste your reference copy here")
|
| 43 |
],
|
| 44 |
outputs=gr.Textbox(label="Structured Template"),
|
| 45 |
-
title="E-commerce Template Generator using
|
| 46 |
-
description=
|
| 47 |
-
"Provide up to five pieces of reference copy. This app calls the o3 mini model "
|
| 48 |
-
"to analyze your inputs and generate a structured template based on our e-commerce copywriting guidelines."
|
| 49 |
-
)
|
| 50 |
)
|
| 51 |
|
| 52 |
iface.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import openai
|
| 4 |
|
| 5 |
+
# Set your OpenAI API key (ensure this is set in your environment)
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
def generate_template(ref1, ref2, ref3, ref4, ref5):
|
| 9 |
+
# Collect non-empty reference copies
|
| 10 |
references = [ref.strip() for ref in [ref1, ref2, ref3, ref4, ref5] if ref.strip()]
|
| 11 |
if not references:
|
| 12 |
return "Please provide at least one piece of reference copy."
|
| 13 |
|
|
|
|
| 14 |
combined_references = "\n\n".join(references)
|
| 15 |
|
| 16 |
+
# Build a detailed prompt that includes a summary of the guide document directions.
|
| 17 |
+
prompt = f"""
|
| 18 |
+
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:
|
| 19 |
+
|
| 20 |
+
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].
|
| 21 |
+
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.
|
| 22 |
+
3. Use control structures (enclosed in {{% %}}) for iterating over collections (for example, listing product features as a bulleted list).
|
| 23 |
+
4. Include clear instructions and modifiers within the template blocks (e.g., tone, style, length).
|
| 24 |
+
5. Ensure consistency, clarity, and adaptability for various products.
|
| 25 |
+
|
| 26 |
+
Your generated template should include:
|
| 27 |
+
- A header section.
|
| 28 |
+
- An introduction block.
|
| 29 |
+
- An example control structure for features.
|
| 30 |
+
- A conclusion block.
|
| 31 |
+
|
| 32 |
+
Reference Copy:
|
| 33 |
+
{combined_references}
|
| 34 |
+
|
| 35 |
+
Generate the final structured template that adheres to these directions.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
# Call the OpenAI ChatCompletion API with the constructed prompt
|
| 39 |
+
response = openai.ChatCompletion.create(
|
| 40 |
+
model="gpt-3.5-turbo", # or your preferred model
|
| 41 |
+
messages=[
|
| 42 |
+
{"role": "system", "content": "You are a helpful assistant that generates structured e-commerce copywriting templates."},
|
| 43 |
+
{"role": "user", "content": prompt}
|
| 44 |
+
],
|
| 45 |
+
max_tokens=512,
|
| 46 |
+
temperature=0.7,
|
| 47 |
)
|
| 48 |
+
|
| 49 |
+
template_output = response["choices"][0]["message"]["content"].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return template_output
|
| 51 |
|
| 52 |
iface = gr.Interface(
|
|
|
|
| 59 |
gr.Textbox(label="Reference Copy 5 (optional)", lines=3, placeholder="Paste your reference copy here")
|
| 60 |
],
|
| 61 |
outputs=gr.Textbox(label="Structured Template"),
|
| 62 |
+
title="E-commerce Template Generator using OpenAI API",
|
| 63 |
+
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."
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
iface.launch()
|