sitammeur commited on
Commit
4b91f1f
·
verified ·
1 Parent(s): 04831f8

Upload 5 files

Browse files
classytext/__init__.py ADDED
File without changes
classytext/classifier/__init__.py ADDED
File without changes
classytext/classifier/predict.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Necessary imports
2
+ import sys
3
+ from typing import Dict
4
+ from classytext.logger import logging
5
+ from classytext.exception import CustomExceptionHandling
6
+ from transformers import pipeline
7
+
8
+
9
+ # Load the zero-shot classification model
10
+ classifier = pipeline(
11
+ "zero-shot-classification",
12
+ model="MoritzLaurer/ModernBERT-large-zeroshot-v2.0",
13
+ )
14
+
15
+
16
+ def ZeroShotTextClassification(
17
+ text_input: str, candidate_labels: str, multi_label: bool
18
+ ) -> Dict[str, float]:
19
+ """
20
+ Performs zero-shot classification on the given text input.
21
+
22
+ Args:
23
+ - text_input: The input text to classify.
24
+ - candidate_labels: A comma-separated string of candidate labels.
25
+ - multi_label: A boolean indicating whether to allow the model to choose multiple classes.
26
+
27
+ Returns:
28
+ Dictionary containing label-score pairs.
29
+ """
30
+ try:
31
+ # Split and clean the candidate labels
32
+ labels = [label.strip() for label in candidate_labels.split(",")]
33
+
34
+ # Log the classification attempt
35
+ logging.info(f"Attempting classification with {len(labels)} labels")
36
+
37
+ # Perform zero-shot classification
38
+ hypothesis_template = "This text is about {}"
39
+ prediction = classifier(
40
+ text_input,
41
+ labels,
42
+ hypothesis_template=hypothesis_template,
43
+ multi_label=multi_label,
44
+ )
45
+
46
+ # Return the classification results
47
+ logging.info("Classification completed successfully")
48
+ return {
49
+ prediction["labels"][i]: prediction["scores"][i]
50
+ for i in range(len(prediction["labels"]))
51
+ }
52
+
53
+ # Handle exceptions that may occur during the process
54
+ except Exception as e:
55
+ # Custom exception handling
56
+ raise CustomExceptionHandling(e, sys) from e
classytext/exception.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This module defines a custom exception handling class and a function to get error message with details of the error.
3
+ """
4
+
5
+ # Standard Library
6
+ import sys
7
+
8
+ # Local imports
9
+ from classytext.logger import logging
10
+
11
+
12
+ # Function Definition to get error message with details of the error (file name and line number) when an error occurs in the program
13
+ def get_error_message(error, error_detail: sys):
14
+ """
15
+ Get error message with details of the error.
16
+
17
+ Args:
18
+ - error (Exception): The error that occurred.
19
+ - error_detail (sys): The details of the error.
20
+
21
+ Returns:
22
+ str: A string containing the error message along with the file name and line number where the error occurred.
23
+ """
24
+ _, _, exc_tb = error_detail.exc_info()
25
+
26
+ # Get error details
27
+ file_name = exc_tb.tb_frame.f_code.co_filename
28
+ return "Error occured in python script name [{0}] line number [{1}] error message[{2}]".format(
29
+ file_name, exc_tb.tb_lineno, str(error)
30
+ )
31
+
32
+
33
+ # Custom Exception Handling Class Definition
34
+ class CustomExceptionHandling(Exception):
35
+ """
36
+ Custom Exception Handling:
37
+ This class defines a custom exception that can be raised when an error occurs in the program.
38
+ It takes an error message and an error detail as input and returns a formatted error message when the exception is raised.
39
+ """
40
+
41
+ # Constructor
42
+ def __init__(self, error_message, error_detail: sys):
43
+ """Initialize the exception"""
44
+ super().__init__(error_message)
45
+
46
+ self.error_message = get_error_message(error_message, error_detail=error_detail)
47
+
48
+ def __str__(self):
49
+ """String representation of the exception"""
50
+ return self.error_message
classytext/logger.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing the required modules
2
+ import os
3
+ import logging
4
+ from datetime import datetime
5
+
6
+ # Creating a log file with the current date and time as the name of the file
7
+ LOG_FILE = f"{datetime.now().strftime('%m_%d_%Y_%H_%M_%S')}.log"
8
+
9
+ # Creating a logs folder if it does not exist
10
+ logs_path = os.path.join(os.getcwd(), "logs", LOG_FILE)
11
+ os.makedirs(logs_path, exist_ok=True)
12
+
13
+ # Setting the log file path and the log level
14
+ LOG_FILE_PATH = os.path.join(logs_path, LOG_FILE)
15
+
16
+ # Configuring the logger
17
+ logging.basicConfig(
18
+ filename=LOG_FILE_PATH,
19
+ format="[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s - %(message)s",
20
+ level=logging.INFO,
21
+ )