-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (41 loc) · 1.82 KB
/
Copy pathmain.py
File metadata and controls
51 lines (41 loc) · 1.82 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
import os
import csv
from gmail_api import gmail_authenticate, get_latest_messages, get_message_content, add_label
from perplexity_api import analyze_email_with_perplexity
from label_mapper import map_label_to_id
def main():
PX_API_KEY = os.getenv("PERPLEXITY_API_KEY")
if not PX_API_KEY:
print("Ustaw zmienną środowiskową PERPLEXITY_API_KEY")
return
print("Wybierz tryb pracy:")
print("1 - Automatyczny (AI kategoryzuje, bez pytania użytkownika)")
print("2 - Interaktywny (po każdym mailu pytaj o zatwierdzenie)")
mode = ""
while mode not in ("1", "2"):
mode = input("Twój wybór (1/2): ").strip()
interactive = (mode == "2")
service = gmail_authenticate()
msg_ids = get_latest_messages(service)
for msg_id in msg_ids:
print(f"Przetwarzanie maila {msg_id}...")
text = get_message_content(service, msg_id)
if not text.strip():
print("Brak zawartości tekstowej maila, pomijam.")
continue
label_name = analyze_email_with_perplexity(PX_API_KEY, text)
print(f"AI zasugerowało etykietę: {label_name}")
if interactive:
user_choice = input(f"Wpisz nową kategorię (ENTER = akceptuj '{label_name}'): ").strip()
final_label = user_choice if user_choice else label_name
# Zapisujemy feedback tylko w trybie interaktywnym
with open('feedback.csv', 'a', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([msg_id, label_name, final_label, text[:200]])
else:
final_label = label_name
label_id = map_label_to_id(service, final_label)
add_label(service, msg_id, label_id)
print(f"Dodano etykietę '{final_label}' do maila {msg_id}.\n")
if __name__ == '__main__':
main()