Skip to content

Instantly share code, notes, and snippets.

@YuchenJin
Last active August 26, 2024 00:39
Show Gist options
  • Save YuchenJin/bd6892f791a02cee4862bdaa293fa51f to your computer and use it in GitHub Desktop.
Save YuchenJin/bd6892f791a02cee4862bdaa293fa51f to your computer and use it in GitHub Desktop.
import requests
import os
from github import Github
from dotenv import load_dotenv
from datetime import datetime, timezone
load_dotenv()
github_token = os.getenv("GITHUB_TOKEN")
g = Github(github_token)
llm_api_url = "https://api.hyperbolic.xyz/v1/chat/completions"
llm_api_key = os.getenv("HYPERBOLIC_API_KEY")
def get_commit_info(repo_name, author_filter=None, limit=100):
"""Fetch commit messages, authors, and dates from a GitHub repository."""
repo = g.get_repo(repo_name)
commits = repo.get_commits()[:limit]
commit_info = [
{
"message": commit.commit.message,
"author": commit.commit.author.name,
"date": commit.commit.author.date.astimezone(timezone.utc).strftime(
"%Y-%m-%d %H:%M:%S UTC"
),
}
for commit in commits
]
if author_filter:
return [
info
for info in commit_info
if author_filter.lower() in info["author"].lower()
]
return commit_info
def is_funny(message):
"""Use the custom LLM API to determine if a commit message is funny."""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {llm_api_key}",
}
data = {
"messages": [
{
"role": "system",
"content": (
"You are an expert in programming culture and humor, with a deep understanding of "
"what makes programmers laugh. You have been trained on programming memes, inside jokes, "
"pop culture references, and the specific challenges developers face daily. Your task is to judge "
"whether a given Git commit message would be considered funny by programmers. When evaluating the "
"commit message, consider the following criteria:\n"
"1. Humor and Wit: Does the message contain clever wordplay, sarcasm, or jokes?\n"
"2. Relatability: Does the message reflect common experiences, frustrations, or humorous situations that "
"programmers often encounter?\n"
"3. Self-Deprecation: Does the message show humility or acknowledge mistakes in a funny way?\n"
"4. Pop Culture or Meme References: Does the message reference popular culture, internet memes, or anything "
"well-known in programmer circles?\n"
"5. Unexpected Playfulness: Does the message surprise the reader with humor in a place they might not expect it?\n"
"Respond with 'yes' if the commit message is funny, and 'no' if it's not."
),
},
{
"role": "user",
"content": f"Is the following git commit message funny? Answer with 'yes' or 'no':\n\n{message}",
},
],
"model": "meta-llama/Meta-Llama-3.1-70B-Instruct",
"max_tokens": 1024,
"temperature": 0.7,
"top_p": 0.9,
}
response = requests.post(llm_api_url, headers=headers, json=data)
if response.status_code == 200:
content = response.json()["choices"][0]["message"]["content"].strip().lower()
return content == "yes"
else:
print(f"Error: API request failed with status code {response.status_code}")
return False
def main():
repo_name = input("Enter the GitHub repository name (e.g., 'owner/repo'): ")
author_filter = input(
"Enter an author name to filter by (or press Enter to skip): "
).strip()
commit_info = get_commit_info(repo_name, author_filter)
print(f"Analyzing {len(commit_info)} commits...")
funny_commits = []
for info in commit_info:
if is_funny(info["message"]):
funny_commits.append(info)
print(f"\nFound {len(funny_commits)} funny commits:")
for info in funny_commits:
print(f"{info['date']} - {info['message'].strip()}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment