-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_ai.py
More file actions
64 lines (50 loc) · 2.08 KB
/
detect_ai.py
File metadata and controls
64 lines (50 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
import os
import google.generativeai as genai
from github import Github
import re
# Retrieve API keys and issue details from GitHub Actions environment
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
GIT_TOKEN = os.getenv("GIT_TOKEN")
REPO_NAME = os.getenv("GITHUB_REPOSITORY")
ISSUE_NUMBER = os.getenv("ISSUE_NUMBER")
ISSUE_BODY = os.getenv("ISSUE_BODY", "")
print(f"🔍 Steps:")
print(f" - GEMINI_API_KEY: {'✅ Set' if GEMINI_API_KEY else '❌ Missing'}")
print(f" - GIT_TOKEN: {'✅ Set' if GIT_TOKEN else '❌ Missing'}")
print(f" - REPO_NAME: {REPO_NAME}")
print(f" - ISSUE_NUMBER: {ISSUE_NUMBER}")
print(f" - ISSUE_BODY: {ISSUE_BODY[:50]}...")
if not GEMINI_API_KEY:
print("❌ ERROR: Missing GEMINI_API_KEY!")
exit(1)
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel("gemini-1.5-pro-latest")
# Skip empty issues
if not ISSUE_BODY.strip():
print("⚠️ Issue body is empty. Skipping detection.")
exit(0)
# Analyze AI probability
response = model.generate_content(
f"Analyze the following text and estimate the probability (0-100%) that it is AI-generated. "
f"Return only the percentage (e.g., 65) without extra text.\n\n{ISSUE_BODY}"
)
# Extract AI probability
try:
match = re.search(r"(\d+)", response.text)
ai_probability = int(match.group(1)) / 100 if match else 0
except ValueError:
ai_probability = 0
print(f"AI Detection Probability: {ai_probability * 100:.2f}%")
# Prepare dynamic comment message with AI probability
comment_message = f"🚨 This issue appears to be AI-generated with a probability of {ai_probability * 100:.2f}%. So, I'm closing this!"
# If AI probability is ≥ 70%, close the issue
if ai_probability >= 0.7:
if not GIT_TOKEN:
print("❌ ERROR: GitHub token is missing or incorrect!")
exit(1)
github_client = Github(GIT_TOKEN.strip()) # ✅ Ensure no accidental whitespace issues
repo = github_client.get_repo(REPO_NAME)
issue = repo.get_issue(int(ISSUE_NUMBER))
issue.create_comment(comment_message)
issue.edit(state="closed")
print("✅ Issue closed successfully!")