CoolFace
Apppublic

Sahil1694/Atlan

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
precompute_classifications.py56 linesDownload Raw Back to scripts
1import os
2import json
3import time
4from tqdm import tqdm
5from pipeline.classifier import classify_ticket
6
7# --- Configuration ---
8INPUT_FILENAME = "data/sample_tickets.json"
9OUTPUT_FILENAME = "data/classified_tickets.json"
10
11# --- Main Execution ---
12
13if __name__ == "__main__":
14    print("๐Ÿš€ Starting Ticket Classification Process (with rate limit handling)...")
15
16    # 1. Read tickets from the input JSON file
17    try:
18        with open(INPUT_FILENAME, 'r', encoding='utf-8') as f:
19            all_tickets = json.load(f)
20        print(f"โœ… Successfully loaded {len(all_tickets)} tickets from '{INPUT_FILENAME}'.")
21    except FileNotFoundError:
22        print(f"๐Ÿšจ Error: Input file '{INPUT_FILENAME}' not found.")
23        exit()
24    except json.JSONDecodeError:
25        print(f"๐Ÿšจ Error: Could not decode JSON from '{INPUT_FILENAME}'.")
26        exit()
27
28    # 2. Classify each ticket with a delay and progress bar
29    classification_results = []
30    # tqdm creates a smart progress bar for the loop
31    for ticket in tqdm(all_tickets, desc="Classifying Tickets"):
32        ticket_body = ticket.get("body")
33        if not ticket_body:
34            continue
35
36        # Call the classification function
37        result = classify_ticket(ticket_body)
38
39        # Combine original ticket info with the new classification
40        classified_ticket = ticket.copy()
41        classified_ticket['classification'] = result
42        classification_results.append(classified_ticket)
43        
44        # --- THIS IS THE CRITICAL FIX ---
45        # Wait for 4 seconds to stay under the 15 requests/minute free tier limit.
46        time.sleep(4)
47
48    # 3. Write the complete results to the output file
49    try:
50        with open(OUTPUT_FILENAME, 'w', encoding='utf-8') as f:
51            json.dump(classification_results, f, indent=4)
52        print(f"\nโœจ Process complete. All classified tickets have been saved to '{OUTPUT_FILENAME}'.")
53    except Exception as e:
54        print(f"\n๐Ÿšจ Error: Could not write results. Reason: {e}")
55
56