File size: 6,058 Bytes
32d164c af52354 32d164c af52354 e3a7c45 af52354 32d164c af52354 32d164c e3a7c45 32d164c af52354 32d164c e3a7c45 32d164c af52354 e3a7c45 af52354 e3a7c45 af52354 32d164c af52354 32d164c d55c858 e3a7c45 af52354 e3a7c45 af52354 32d164c af52354 32d164c e3a7c45 32d164c e3a7c45 32d164c e3a7c45 32d164c af52354 e3a7c45 32d164c e3a7c45 af52354 32d164c af52354 e3a7c45 af52354 32d164c af52354 e3a7c45 af52354 32d164c af52354 e3a7c45 af52354 32d164c af52354 e3a7c45 |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import os
import fitz
import gradio as gr
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# 1) Extract full text from PDF
def extract_text_from_pdf(pdf_file):
text = ""
with fitz.open(pdf_file.name) as doc:
for page in doc:
text += page.get_text()
return text.strip()
# 2) Summarize the full brief into bullet points
def summarize_brief(full_text):
prompt = f"""
Please summarize the key main points of the following brand brief in bullet points:
---
{full_text}
---
"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
)
return response.choices[0].message.content.strip()
# 3) Generate 3 content ideas (without re-summarizing if summary already exists)
def generate_ideas(brief_text, industry):
prompt = f"""
You are a creative strategist for a digital agency.
A brand provided this brief:
---
{brief_text}
---
The creator's industry is: {industry}
Suggest 3 short, punchy content ideas that align with this brief and are suitable for this industry.
"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0.8,
)
return response.choices[0].message.content.strip()
# 4) Score the creator's draft versus the summarized brief.
def score_draft(brief_text, draft_text):
if not draft_text.strip():
return "No draft submitted."
prompt = f"""
Brand Brief:
---
{brief_text}
---
Creator's Draft:
---
{draft_text}
---
Score how well the creator's draft aligns with the brand brief. Return a score out of 10 and a 2–3 sentence explanation.
"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
)
return response.choices[0].message.content.strip()
# 5) This function checks if there is already a summarized brief.
# If yes, it uses that; otherwise it tries to summarize from text or PDF.
def generate_ideas_from_brief(text_brief, pdf_brief, existing_brief, industry, draft_text):
# Use the existing summarized brief if available.
if existing_brief.strip():
summarized = existing_brief.strip()
else:
# If not, try the text_brief, then PDF
if text_brief.strip():
summarized = summarize_brief(text_brief.strip())
elif pdf_brief is not None:
full_brief = extract_text_from_pdf(pdf_brief)
summarized = summarize_brief(full_brief)
else:
return "No summarized brief found. Please provide a brief.", "", ""
ideas = generate_ideas(summarized, industry)
match_score = score_draft(summarized, draft_text) if draft_text.strip() else ""
return summarized, ideas, match_score
# 6) Clear All: reset all inputs and outputs using gr.update
def clear_all():
return (
gr.update(value=""), # text_brief
gr.update(value=None), # pdf_brief
gr.update(value="Lifestyle"), # industry
gr.update(value=""), # draft_text
gr.update(value=""), # output_brief (summarized brief)
gr.update(value=""), # output_ideas
gr.update(value="") # output_score
)
# --- GRADIO UI ---
industries = ["Lifestyle", "Fitness", "Music", "Fashion", "Food", "Tech", "Travel", "Gaming", "Parenting"]
with gr.Blocks(css="""
body { font-family: 'Inter', sans-serif; background-color: #ffffff; }
h1, h2, .gr-markdown h1 { color: black; font-weight: 700; }
.gr-button { background-color: #00bfa6; color: white; border-radius: 6px; font-weight: bold; }
.gr-button:hover { background-color: #009f8c; }
""") as demo:
gr.Markdown("# <span style='color:black'>Sync’d AI</span>")
gr.Markdown("**Generate punchy content ideas and match creator drafts to briefs.**")
with gr.Row():
text_brief = gr.Textbox(label="Brand Brief (Text Input)", lines=6, placeholder="Paste brand brief here...")
pdf_brief = gr.File(label="Or Upload Brand Brief (PDF)", file_types=[".pdf"], file_count="single")
industry = gr.Dropdown(choices=industries, label="Creator Industry", value="Lifestyle")
draft_text = gr.Textbox(label="Creator's Draft (Optional)", placeholder="Paste the creator's draft here...", lines=4)
with gr.Row():
generate_ideas_btn = gr.Button("Generate Ideas")
submit_draft_btn = gr.Button("Submit Draft (Match Analysis)")
clear_btn = gr.Button("Clear All")
output_brief = gr.Textbox(label="Summarized Brief", lines=6)
output_ideas = gr.Textbox(label="3 Content Ideas", lines=8)
output_score = gr.Textbox(label="Match Score & Analysis", lines=4)
# When a PDF is uploaded, auto-summarize and generate ideas.
pdf_brief.change(
fn=lambda pdf, ind, d: generate_ideas_from_brief("", pdf, "", ind, d),
inputs=[pdf_brief, industry, draft_text],
outputs=[output_brief, output_ideas, output_score],
show_progress="full"
)
# "Generate Ideas" button:
# It checks if there's an existing summarized brief.
generate_ideas_btn.click(
fn=generate_ideas_from_brief,
inputs=[text_brief, pdf_brief, output_brief, industry, draft_text],
outputs=[output_brief, output_ideas, output_score],
show_progress="full"
)
# "Submit Draft" button for match analysis.
submit_draft_btn.click(
fn=score_draft,
inputs=[output_brief, draft_text],
outputs=output_score,
show_progress="full"
)
# "Clear All" button resets everything.
clear_btn.click(
fn=clear_all,
inputs=[],
outputs=[text_brief, pdf_brief, industry, draft_text, output_brief, output_ideas, output_score]
)
demo.launch() |