Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,34 +3,30 @@ import openai
|
|
3 |
import gradio as gr
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
6 |
-
import nest_asyncio
|
7 |
-
import asyncio
|
8 |
|
9 |
-
from playwright.sync_api import sync_playwright
|
10 |
-
|
11 |
-
nest_asyncio.apply()
|
12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
13 |
|
14 |
-
# Synchronous version for HF Spaces compatibility
|
15 |
def extract_text_from_url(url):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def extract_keywords(text):
|
29 |
prompt = f"""
|
30 |
Extract up to 10 concise, relevant SEO keywords suitable for an automotive advertisement from the following content:
|
31 |
-
|
32 |
{text}
|
33 |
-
|
34 |
Keywords:
|
35 |
"""
|
36 |
response = openai.ChatCompletion.create(
|
@@ -39,8 +35,13 @@ def extract_keywords(text):
|
|
39 |
temperature=0.6,
|
40 |
max_tokens=100
|
41 |
)
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
def generate_ad_copy(platform, keywords):
|
46 |
prompt = f"""
|
@@ -72,37 +73,35 @@ def generate_ad_image(keywords):
|
|
72 |
|
73 |
def main_workflow(input_mode, url_or_keywords):
|
74 |
error = None
|
75 |
-
|
|
|
|
|
|
|
76 |
if input_mode == "URL":
|
77 |
try:
|
78 |
text = extract_text_from_url(url_or_keywords)
|
79 |
keywords = extract_keywords(text)
|
80 |
except Exception as e:
|
81 |
-
return None, None, None, f"
|
82 |
else:
|
83 |
keywords = [kw.strip() for kw in url_or_keywords.split(",") if kw.strip()]
|
84 |
if not keywords:
|
85 |
return None, None, None, "Please provide at least one keyword."
|
86 |
-
|
87 |
-
# Step 2: Generate ad copies
|
88 |
platforms = ["Facebook", "Instagram", "X (Twitter)", "Google Search"]
|
89 |
-
ad_copies = {}
|
90 |
for platform in platforms:
|
91 |
ad_copies[platform] = generate_ad_copy(platform, keywords)
|
92 |
-
|
93 |
-
# Step 3: Generate ad image
|
94 |
try:
|
95 |
image_path = generate_ad_image(keywords)
|
96 |
except Exception as e:
|
97 |
-
image_path = None
|
98 |
error = f"Image generation error: {e}"
|
99 |
|
100 |
-
#
|
101 |
output_txt = "generated_ads.txt"
|
102 |
with open(output_txt, "w", encoding="utf-8") as f:
|
103 |
for platform, content in ad_copies.items():
|
104 |
f.write(f"--- {platform} Ad Copy ---\n{content}\n\n")
|
105 |
-
|
106 |
return keywords, ad_copies, image_path, error
|
107 |
|
108 |
def run_space(input_mode, url, keywords):
|
|
|
3 |
import gradio as gr
|
4 |
from bs4 import BeautifulSoup
|
5 |
import requests
|
|
|
|
|
6 |
|
|
|
|
|
|
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
|
|
|
9 |
def extract_text_from_url(url):
|
10 |
+
"""Extracts text from HTML for static pages. Warns if content is very thin."""
|
11 |
+
try:
|
12 |
+
resp = requests.get(url, timeout=30, headers={
|
13 |
+
"User-Agent": "Mozilla/5.0 (compatible; Bot/1.0)"
|
14 |
+
})
|
15 |
+
soup = BeautifulSoup(resp.content, "html.parser")
|
16 |
+
# Try to get rich descriptive content
|
17 |
+
candidates = soup.find_all(['h1','h2','h3','h4','p','span','li'])
|
18 |
+
text = ' '.join([c.get_text(strip=True) for c in candidates])
|
19 |
+
text = text[:4000]
|
20 |
+
if len(text) < 100:
|
21 |
+
raise ValueError("Could not extract enough content (site may require JavaScript). Please enter keywords manually.")
|
22 |
+
return text
|
23 |
+
except Exception as e:
|
24 |
+
raise ValueError(f"URL extraction error: {e}")
|
25 |
|
26 |
def extract_keywords(text):
|
27 |
prompt = f"""
|
28 |
Extract up to 10 concise, relevant SEO keywords suitable for an automotive advertisement from the following content:
|
|
|
29 |
{text}
|
|
|
30 |
Keywords:
|
31 |
"""
|
32 |
response = openai.ChatCompletion.create(
|
|
|
35 |
temperature=0.6,
|
36 |
max_tokens=100
|
37 |
)
|
38 |
+
# Handles both comma or newline separation
|
39 |
+
output = response.choices[0].message.content.strip()
|
40 |
+
if ',' in output:
|
41 |
+
keywords = output.split(',')
|
42 |
+
else:
|
43 |
+
keywords = output.split('\n')
|
44 |
+
return [kw.strip() for kw in keywords if kw.strip()]
|
45 |
|
46 |
def generate_ad_copy(platform, keywords):
|
47 |
prompt = f"""
|
|
|
73 |
|
74 |
def main_workflow(input_mode, url_or_keywords):
|
75 |
error = None
|
76 |
+
keywords = []
|
77 |
+
ad_copies = {}
|
78 |
+
image_path = None
|
79 |
+
|
80 |
if input_mode == "URL":
|
81 |
try:
|
82 |
text = extract_text_from_url(url_or_keywords)
|
83 |
keywords = extract_keywords(text)
|
84 |
except Exception as e:
|
85 |
+
return None, None, None, f"{e}"
|
86 |
else:
|
87 |
keywords = [kw.strip() for kw in url_or_keywords.split(",") if kw.strip()]
|
88 |
if not keywords:
|
89 |
return None, None, None, "Please provide at least one keyword."
|
90 |
+
# Generate ad copies
|
|
|
91 |
platforms = ["Facebook", "Instagram", "X (Twitter)", "Google Search"]
|
|
|
92 |
for platform in platforms:
|
93 |
ad_copies[platform] = generate_ad_copy(platform, keywords)
|
94 |
+
# Generate image
|
|
|
95 |
try:
|
96 |
image_path = generate_ad_image(keywords)
|
97 |
except Exception as e:
|
|
|
98 |
error = f"Image generation error: {e}"
|
99 |
|
100 |
+
# Save ads to txt
|
101 |
output_txt = "generated_ads.txt"
|
102 |
with open(output_txt, "w", encoding="utf-8") as f:
|
103 |
for platform, content in ad_copies.items():
|
104 |
f.write(f"--- {platform} Ad Copy ---\n{content}\n\n")
|
|
|
105 |
return keywords, ad_copies, image_path, error
|
106 |
|
107 |
def run_space(input_mode, url, keywords):
|