sitammeur commited on
Commit
2958307
·
verified ·
1 Parent(s): 2797bb5

Upload 5 files

Browse files
src/__init__.py ADDED
File without changes
src/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 src.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
src/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
+ )
src/modernbert/__init__.py ADDED
File without changes
src/modernbert/classifier.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Necessary imports
2
+ import sys
3
+ from typing import Dict
4
+ from src.logger import logging
5
+ from src.exception import CustomExceptionHandling
6
+ from transformers import pipeline
7
+ import gradio as gr
8
+
9
+
10
+ # Load the zero-shot classification model
11
+ classifier = pipeline(
12
+ "zero-shot-classification",
13
+ model="MoritzLaurer/ModernBERT-large-zeroshot-v2.0",
14
+ )
15
+
16
+
17
+ def ZeroShotTextClassification(
18
+ text_input: str, candidate_labels: str, multi_label: bool
19
+ ) -> Dict[str, float]:
20
+ """
21
+ Performs zero-shot classification on the given text input.
22
+
23
+ Args:
24
+ - text_input: The input text to classify.
25
+ - candidate_labels: A comma-separated string of candidate labels.
26
+ - multi_label: A boolean indicating whether to allow the model to choose multiple classes.
27
+
28
+ Returns:
29
+ Dictionary containing label-score pairs.
30
+ """
31
+ try:
32
+ # Check if the input and candidate labels are valid
33
+ if not text_input or not candidate_labels:
34
+ gr.Warning("Please provide valid input and candidate labels")
35
+
36
+ # Split and clean the candidate labels
37
+ labels = [label.strip() for label in candidate_labels.split(",")]
38
+
39
+ # Log the classification attempt
40
+ logging.info(f"Attempting classification with {len(labels)} labels")
41
+
42
+ # Perform zero-shot classification
43
+ hypothesis_template = "This text is about {}"
44
+ prediction = classifier(
45
+ text_input,
46
+ labels,
47
+ hypothesis_template=hypothesis_template,
48
+ multi_label=multi_label,
49
+ )
50
+
51
+ # Return the classification results
52
+ logging.info("Classification completed successfully")
53
+ return {
54
+ prediction["labels"][i]: prediction["scores"][i]
55
+ for i in range(len(prediction["labels"]))
56
+ }
57
+
58
+ # Handle exceptions that may occur during the process
59
+ except Exception as e:
60
+ # Custom exception handling
61
+ raise CustomExceptionHandling(e, sys) from e