Skip to content

Instantly share code, notes, and snippets.

@radeeyate
Created June 2, 2024 11:47
Show Gist options
  • Save radeeyate/d4924619f4cc7db0f28557a5696283cf to your computer and use it in GitHub Desktop.
Save radeeyate/d4924619f4cc7db0f28557a5696283cf to your computer and use it in GitHub Desktop.
import google.generativeai as genai
from pymongo import MongoClient
import datetime
import time
import secrets
import json
from md2pdf.core import md2pdf
import requests
client = MongoClient(
"mongodb+srv://[redacted]"
)
database = client["womposts"]
postsCollection = database["posts"]
oneWeekAgo = datetime.date.today() - datetime.timedelta(days=7)
timestamp = int(time.mktime(oneWeekAgo.timetuple()) * 1000)
posts = list(postsCollection.find({"time": {"$gt": timestamp}}))
filename = f"posts-{timestamp}.json"
postsFile = open(filename, "x")
postsFile.write(json.dumps(posts))
postsFile.close()
genai.configure(api_key="[redacted]")
generationConfig = {
"temperature": 1,
"top_p": 0.95,
"top_k": 0,
"max_output_tokens": 8192,
}
safetySettings = [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE",
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE",
},
]
systemPrompt = "You have a JSON file containing social media posts. Each post includes the text content, post info, and more. Analyze the data to provide a report summarizing the key topics and trends that emerged over the past week. Additionally, identify the most popular posts (5-10), any prominent sentiment trends, and discuss some of the most substantial users (4-8) in the data (e.g., users with a high number of posts or engagements). By substantial, I mean users who appear to be influential or informative within the context of this data set."
model = genai.GenerativeModel(
model_name="gemini-1.5-flash-latest",
generation_config=generationConfig,
system_instruction=systemPrompt,
safety_settings=safetySettings,
)
response = model.generate_content(
["Analyze the data to provide a report:\n" + open(filename).read()]
)
print(response.text)
md2pdf(f"posts-{timestamp}.pdf", response.text)
requests.put(
"https://ntfy.sh/[redacted]",
data=open(f"posts-{timestamp}.pdf", "rb"),
headers={
"Filename": "Posts Summary.pdf",
"X-Message": "Your summaries are ready!",
"X-Priority": "high",
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment