Skip to content

Commit 95c9c7a

Browse files
authored
Merge pull request #28 from GYFX35/feature/ai-promotion-from-url
Feature/ai promotion from url
2 parents 6590f04 + 0bce0c9 commit 95c9c7a

File tree

6 files changed

+170
-15
lines changed

6 files changed

+170
-15
lines changed

app.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ def get_config():
622622
'metaAppId': os.environ.get('META_APP_ID')
623623
})
624624

625+
@app.route('/promotion')
626+
def promotion():
627+
return render_template('promotion.html')
628+
625629
@app.route('/')
626630
def index():
627631
return render_template('index.html')
@@ -845,6 +849,31 @@ def create_promotion():
845849
promotion_text = google_ai.generate_social_media_post(description)
846850
return jsonify({"promotion_text": promotion_text})
847851

852+
853+
@app.route('/api/v1/promote', methods=['POST'])
854+
@require_api_key
855+
def create_promotion_from_url():
856+
data = request.get_json()
857+
url = data.get('url')
858+
if not url:
859+
return jsonify({"error": _("URL is required")}), 400
860+
861+
try:
862+
response = requests.get(url, timeout=10)
863+
response.raise_for_status()
864+
soup = BeautifulSoup(response.content, 'html.parser')
865+
# Extract text from the body, trying to get meaningful content
866+
text_content = ' '.join(t.strip() for t in soup.body.find_all(string=True) if t.parent.name not in ['style', 'script', 'head', 'title', 'meta', '[document]'])
867+
if not text_content:
868+
return jsonify({"error": _("Could not extract meaningful content from the URL.")}), 400
869+
# Limit the content size to avoid overly large payloads to the AI model
870+
promotion_text = google_ai.generate_promotion_from_content(url, text_content[:4000])
871+
return jsonify({"promotion_text": promotion_text})
872+
except requests.RequestException as e:
873+
return jsonify({"error": _("Error fetching URL: %(error)s", error=str(e))}), 400
874+
except Exception as e:
875+
return jsonify({"error": _("An unexpected error occurred: %(error)s", error=str(e))}), 500
876+
848877
@app.route('/api/v1/payment/create-payment-intent', methods=['POST'])
849878
@require_api_key
850879
def create_payment_intent():

frontend/static/css/promotion.css

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
body {
2+
font-family: sans-serif;
3+
line-height: 1.6;
4+
margin: 0;
5+
padding: 2rem;
6+
background: #f4f4f4;
7+
color: #333;
8+
}
9+
10+
.container {
11+
max-width: 600px;
12+
margin: auto;
13+
background: #fff;
14+
padding: 2rem;
15+
border-radius: 5px;
16+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
17+
}
18+
19+
h1 {
20+
text-align: center;
21+
color: #333;
22+
}
23+
24+
form {
25+
display: flex;
26+
flex-direction: column;
27+
}
28+
29+
input[type="url"] {
30+
padding: 10px;
31+
margin-bottom: 1rem;
32+
border: 1px solid #ccc;
33+
border-radius: 4px;
34+
}
35+
36+
button {
37+
padding: 10px 15px;
38+
background: #333;
39+
color: #fff;
40+
border: none;
41+
border-radius: 4px;
42+
cursor: pointer;
43+
font-size: 1rem;
44+
}
45+
46+
button:hover {
47+
background: #555;
48+
}
49+
50+
#promotion-result {
51+
margin-top: 1.5rem;
52+
padding: 1rem;
53+
background: #e9e9e9;
54+
border-radius: 4px;
55+
}

frontend/static/js/promotion.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const promotionForm = document.getElementById('promotion-form');
3+
const urlInput = document.getElementById('url-input');
4+
const apiKeyInput = document.getElementById('api-key-input');
5+
const resultDiv = document.getElementById('promotion-result');
6+
7+
promotionForm.addEventListener('submit', async (e) => {
8+
e.preventDefault();
9+
const url = urlInput.value.trim();
10+
const apiKey = apiKeyInput.value.trim();
11+
if (!url || !apiKey) {
12+
alert('Please enter a URL and your API Key.');
13+
return;
14+
}
15+
16+
try {
17+
const response = await fetch('/api/v1/promote', {
18+
method: 'POST',
19+
headers: {
20+
'Content-Type': 'application/json',
21+
'X-API-Key': apiKey
22+
},
23+
body: JSON.stringify({ url })
24+
});
25+
26+
if (!response.ok) {
27+
throw new Error('Failed to start promotion.');
28+
}
29+
30+
const data = await response.json();
31+
resultDiv.innerHTML = `<h3>Promotion Campaign Started!</h3><p>${data.promotion_text}</p>`;
32+
} catch (error) {
33+
resultDiv.innerHTML = `<p style="color: red;">${error.message}</p>`;
34+
}
35+
});
36+
});

frontend/templates/promotion.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Promote Your Product</title>
7+
<link rel="stylesheet" href="{{ url_for('static', filename='css/promotion.css') }}">
8+
</head>
9+
<body>
10+
<div class="container">
11+
<h1>Promote Your Product, Startup, or Talent</h1>
12+
<p>Enter the URL of your product, business, or portfolio below, and our AI will generate a promotion campaign for you.</p>
13+
<form id="promotion-form">
14+
<input type="url" id="url-input" placeholder="https://your-product-url.com" required>
15+
<input type="text" id="api-key-input" placeholder="Enter your API Key" required>
16+
<button type="submit">Start Promotion</button>
17+
</form>
18+
<div id="promotion-result"></div>
19+
</div>
20+
<script src="{{ url_for('static', filename='js/promotion.js') }}"></script>
21+
</body>
22+
</html>

google_ai.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,31 @@ def generate_social_media_post(description: str) -> str:
111111
except Exception as e:
112112
print(f"Error generating social media post with Vertex AI: {e}")
113113
return f"Error: {e}"
114+
115+
116+
def generate_promotion_from_content(url: str, content: str) -> str:
117+
"""
118+
Generates a promotion campaign from a URL and its content using Vertex AI.
119+
"""
120+
model = GenerativeModel("gemini-1.5-flash")
121+
122+
generation_prompt = f"""
123+
You are an expert marketing strategist. Your task is to create a compelling promotion campaign
124+
for the product, startup, business, art, or talent described at the URL: {url}.
125+
126+
I have extracted the following text content from the page:
127+
---
128+
{content}
129+
---
130+
131+
Based on this content, generate a short, catchy, and engaging promotional text.
132+
The text should be suitable for social media and include relevant hashtags.
133+
"""
134+
135+
try:
136+
response = model.generate_content(generation_prompt)
137+
return response.text.strip()
138+
139+
except Exception as e:
140+
print(f"Error generating promotion from content with Vertex AI: {e}")
141+
return f"Error: {e}"

jules-scratch/verification/verify_features.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)