Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,95 @@
|
|
| 1 |
import os
|
| 2 |
-
import random
|
| 3 |
import logging
|
| 4 |
-
from datetime import datetime, timedelta
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
import asyncio
|
| 7 |
import requests
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
logging.basicConfig(
|
| 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 |
-
label="Tone", choices=["Casual", "Professional", "Luxury"], value="Professional"
|
| 100 |
-
)
|
| 101 |
-
generate_btn = gr.Button("Generate Content")
|
| 102 |
-
content_output = gr.Textbox(label="Generated Content", interactive=False)
|
| 103 |
-
|
| 104 |
-
# Zapier
|
| 105 |
-
zapier_btn = gr.Button("Send to Zapier")
|
| 106 |
-
zapier_status = gr.Textbox(label="Zapier Status", interactive=False)
|
| 107 |
-
|
| 108 |
-
async def handle_generate(topic, audience, tone):
|
| 109 |
-
return await content_generator.generate_content(topic, audience, tone)
|
| 110 |
-
|
| 111 |
-
async def handle_zapier(content):
|
| 112 |
-
if not content:
|
| 113 |
-
return "No content to send to Zapier."
|
| 114 |
-
result = await zapier.notify_zapier(content)
|
| 115 |
-
return result["message"]
|
| 116 |
-
|
| 117 |
-
generate_btn.click(handle_generate, inputs=[topic, audience, tone], outputs=content_output)
|
| 118 |
-
zapier_btn.click(handle_zapier, inputs=[content_output], outputs=zapier_status)
|
| 119 |
-
|
| 120 |
-
return dashboard
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if __name__ == "__main__":
|
| 124 |
-
dashboard = create_dashboard()
|
| 125 |
-
dashboard.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import logging
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import gradio as gr
|
| 5 |
+
from sentence_transformers import SentenceTransformer, util
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
# Logging configuration
|
| 12 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s")
|
| 13 |
+
|
| 14 |
+
# HuggingFace model for semantic similarity
|
| 15 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 16 |
+
|
| 17 |
+
# API Keys and Webhook URLs
|
| 18 |
+
PHANTOMBUSTER_API_KEY = os.getenv("PHANTOMBUSTER_API_KEY")
|
| 19 |
+
PHANTOMBUSTER_AGENT_ID = os.getenv("PHANTOMBUSTER_AGENT_ID")
|
| 20 |
+
MAKE_WEBHOOK_URL = os.getenv("MAKE_WEBHOOK_URL")
|
| 21 |
+
|
| 22 |
+
# Functions
|
| 23 |
+
def fetch_phantombuster_data():
|
| 24 |
+
"""Fetch hashtags from PhantomBuster output."""
|
| 25 |
+
url = f"https://phantombuster.com/api/v2/agents/{PHANTOMBUSTER_AGENT_ID}/output"
|
| 26 |
+
headers = {"X-Phantombuster-Key-1": PHANTOMBUSTER_API_KEY}
|
| 27 |
+
try:
|
| 28 |
+
response = requests.get(url, headers=headers)
|
| 29 |
+
response.raise_for_status()
|
| 30 |
+
data = response.json()
|
| 31 |
+
hashtags = []
|
| 32 |
+
for post in data.get("data", []):
|
| 33 |
+
if "caption" in post:
|
| 34 |
+
hashtags.extend([tag for tag in post["caption"].split() if tag.startswith("#")])
|
| 35 |
+
return list(set(hashtags)) # Remove duplicates
|
| 36 |
+
except Exception as e:
|
| 37 |
+
logging.error(f"Error fetching PhantomBuster data: {e}")
|
| 38 |
+
return ["Error fetching hashtags."]
|
| 39 |
+
|
| 40 |
+
def filter_relevant_hashtags(hashtags, keywords):
|
| 41 |
+
"""Filter hashtags based on semantic relevance."""
|
| 42 |
+
hashtags_embeddings = model.encode(hashtags, convert_to_tensor=True)
|
| 43 |
+
keywords_embedding = model.encode(keywords, convert_to_tensor=True)
|
| 44 |
+
cosine_scores = util.pytorch_cos_sim(hashtags_embeddings, keywords_embedding)
|
| 45 |
+
relevant = [
|
| 46 |
+
hashtags[i]
|
| 47 |
+
for i in range(len(hashtags))
|
| 48 |
+
if cosine_scores[i].max() > 0.5
|
| 49 |
+
]
|
| 50 |
+
return relevant
|
| 51 |
+
|
| 52 |
+
def send_to_make_webhook(payload):
|
| 53 |
+
"""Send data to Make.com webhook."""
|
| 54 |
+
try:
|
| 55 |
+
response = requests.post(MAKE_WEBHOOK_URL, json=payload, timeout=10)
|
| 56 |
+
response.raise_for_status()
|
| 57 |
+
return "Data sent successfully to Make.com!"
|
| 58 |
+
except Exception as e:
|
| 59 |
+
logging.error(f"Error sending to Make.com: {e}")
|
| 60 |
+
return "Error sending data to Make.com."
|
| 61 |
+
|
| 62 |
+
# Gradio Dashboard
|
| 63 |
+
with gr.Blocks() as dashboard:
|
| 64 |
+
gr.Markdown("# Instagram Hashtag Monitoring Dashboard")
|
| 65 |
+
|
| 66 |
+
# Hashtag Extraction
|
| 67 |
+
competitor_username = gr.Textbox(label="Competitor Username", placeholder="e.g., competitor1")
|
| 68 |
+
extract_btn = gr.Button("Extract Hashtags")
|
| 69 |
+
extracted_hashtags = gr.Textbox(label="Extracted Hashtags", interactive=False)
|
| 70 |
+
|
| 71 |
+
# Hashtag Filtering
|
| 72 |
+
niche_keywords = gr.Textbox(label="Niche Keywords", placeholder="e.g., sustainability, eco-friendly")
|
| 73 |
+
filter_btn = gr.Button("Filter Relevant Hashtags")
|
| 74 |
+
relevant_hashtags = gr.Textbox(label="Relevant Hashtags", interactive=False)
|
| 75 |
+
|
| 76 |
+
# Send to Make.com
|
| 77 |
+
send_btn = gr.Button("Send to Make.com")
|
| 78 |
+
make_status = gr.Textbox(label="Webhook Status", interactive=False)
|
| 79 |
+
|
| 80 |
+
# Handlers
|
| 81 |
+
def handle_extract(username):
|
| 82 |
+
return fetch_phantombuster_data()
|
| 83 |
+
|
| 84 |
+
def handle_filter(hashtags, keywords):
|
| 85 |
+
return filter_relevant_hashtags(hashtags.split(","), keywords.split(","))
|
| 86 |
+
|
| 87 |
+
def handle_send_to_make(hashtags):
|
| 88 |
+
payload = {"hashtags": hashtags.split(","), "action": "monitor"}
|
| 89 |
+
return send_to_make_webhook(payload)
|
| 90 |
+
|
| 91 |
+
extract_btn.click(handle_extract, inputs=[competitor_username], outputs=[extracted_hashtags])
|
| 92 |
+
filter_btn.click(handle_filter, inputs=[extracted_hashtags, niche_keywords], outputs=[relevant_hashtags])
|
| 93 |
+
send_btn.click(handle_send_to_make, inputs=[relevant_hashtags], outputs=[make_status])
|
| 94 |
+
|
| 95 |
+
dashboard.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|