-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtlabeler.py
More file actions
127 lines (103 loc) · 4.04 KB
/
tlabeler.py
File metadata and controls
127 lines (103 loc) · 4.04 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import sys
from trello import TrelloClient
import google.generativeai as genai
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv(os.path.expanduser("~/.env"))
# Get Trello API credentials from environment variables
TRELLO_API_KEY = os.getenv("TRELLO_API_KEY")
TRELLO_API_SECRET = os.getenv("TRELLO_API_SECRET")
TRELLO_TOKEN = os.getenv("TRELLO_TOKEN")
# Get Gemini API key from environment variables
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
# Configure the Gemini API
genai.configure(api_key=GEMINI_API_KEY)
def get_trello_client():
"""Initializes and returns the Trello client."""
return TrelloClient(
api_key=TRELLO_API_KEY,
api_secret=TRELLO_API_SECRET,
token=TRELLO_TOKEN
)
def get_board_by_id(client, board_id):
"""Finds a Trello board by its ID."""
try:
return client.get_board(board_id)
except Exception as e:
print(f"Error getting board '{board_id}': {e}")
return None
def get_available_labels(board):
"""Fetches all available labels for a given board."""
return {label.name: label for label in board.get_labels()}
def get_unlabeled_cards(board):
"""Fetches all cards with no labels from a given board."""
return [card for card in board.all_cards() if not card.labels]
def choose_labels_with_ai(card, label_names, instructions):
"""
Uses the Gemini API to choose the most appropriate labels for a card.
"""
model = genai.GenerativeModel('gemini-1.5-flash')
prompt = (
f"Please follow these instructions when assigning labels:\n{instructions}\n\n"
f"Given the following Trello card title and description, "
f"which of the following labels are most appropriate?\n\n"
f"Labels: {', '.join(label_names)}\n\n"
f"Card Title: {card.name}\n"
f"Card Description: {card.description}\n\n"
f"Please respond with a comma-separated list of the best label names."
)
try:
response = model.generate_content(prompt)
return [label.strip() for label in response.text.split(',')]
except Exception as e:
print(f"Error generating labels for card '{card.name}': {e}")
return []
def main():
"""
Main function to fetch unlabeled cards, analyze them with AI,
and apply the most appropriate labels.
"""
if len(sys.argv) != 2:
print("Usage: python tlabeler.py 'Your Board ID'")
sys.exit(1)
board_id = sys.argv[1]
client = get_trello_client()
board = get_board_by_id(client, board_id)
if not board:
print(f"Error: Board with ID '{board_id}' not found.")
sys.exit(1)
available_labels = get_available_labels(board)
if not available_labels:
print("No labels found on this board to choose from.")
return
label_names = [name for name in available_labels.keys() if not name.isupper()]
unlabeled_cards = get_unlabeled_cards(board)
if not unlabeled_cards:
print("No unlabeled cards found.")
return
instructions = ""
instructions_path = os.path.join(os.path.dirname(__file__), 'instructions.md')
if os.path.exists(instructions_path):
with open(instructions_path, 'r') as f:
instructions = f.read()
print(f"Found {len(unlabeled_cards)} unlabeled cards. Analyzing...")
for card in unlabeled_cards:
print(f"Analyzing card: '{card.name}'")
chosen_label_names = choose_labels_with_ai(card, label_names, instructions)
if chosen_label_names:
labels_to_apply = [
available_labels[name]
for name in chosen_label_names
if name in available_labels
]
if labels_to_apply:
for label in labels_to_apply:
card.add_label(label)
print(f" Applied labels: {', '.join(chosen_label_names)}")
else:
print(" No matching labels found to apply.")
else:
print(" Could not determine appropriate labels.")
if __name__ == "__main__":
main()