Upload 5 files
Browse files- src/app/model.py +45 -0
- src/app/response.py +67 -0
- src/config.py +19 -0
- src/exception.py +50 -0
- src/logger.py +21 -0
src/app/model.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Necessary imports
|
| 2 |
+
import sys
|
| 3 |
+
from typing import Any
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModel, AutoTokenizer
|
| 6 |
+
|
| 7 |
+
# Local imports
|
| 8 |
+
from src.logger import logging
|
| 9 |
+
from src.exception import CustomExceptionHandling
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_model_and_tokenizer(model_name: str, device: str) -> Any:
|
| 13 |
+
"""
|
| 14 |
+
Load the model and tokenizer.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
- model_name (str): The name of the model to load.
|
| 18 |
+
- device (str): The device to load the model onto.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
- model: The loaded model.
|
| 22 |
+
- tokenizer: The loaded tokenizer.
|
| 23 |
+
"""
|
| 24 |
+
try:
|
| 25 |
+
# Load the model and tokenizer
|
| 26 |
+
model = AutoModel.from_pretrained(
|
| 27 |
+
model_name,
|
| 28 |
+
trust_remote_code=True,
|
| 29 |
+
attn_implementation="sdpa",
|
| 30 |
+
torch_dtype=torch.bfloat16,
|
| 31 |
+
)
|
| 32 |
+
model = model.to(device=device)
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 34 |
+
model.eval()
|
| 35 |
+
|
| 36 |
+
# Log the successful loading of the model and tokenizer
|
| 37 |
+
logging.info("Model and tokenizer loaded successfully.")
|
| 38 |
+
|
| 39 |
+
# Return the model and tokenizer
|
| 40 |
+
return model, tokenizer
|
| 41 |
+
|
| 42 |
+
# Handle exceptions that may occur during model and tokenizer loading
|
| 43 |
+
except Exception as e:
|
| 44 |
+
# Custom exception handling
|
| 45 |
+
raise CustomExceptionHandling(e, sys) from e
|
src/app/response.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Necessary imports
|
| 2 |
+
import sys
|
| 3 |
+
import spaces
|
| 4 |
+
|
| 5 |
+
# Local imports
|
| 6 |
+
from src.config import (
|
| 7 |
+
device,
|
| 8 |
+
model_name,
|
| 9 |
+
system_prompt,
|
| 10 |
+
sampling,
|
| 11 |
+
stream,
|
| 12 |
+
top_p,
|
| 13 |
+
top_k,
|
| 14 |
+
temperature,
|
| 15 |
+
repetition_penalty,
|
| 16 |
+
max_new_tokens,
|
| 17 |
+
)
|
| 18 |
+
from src.app.model import load_model_and_tokenizer
|
| 19 |
+
from src.logger import logging
|
| 20 |
+
from src.exception import CustomExceptionHandling
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Model and tokenizer
|
| 24 |
+
model, tokenizer = load_model_and_tokenizer(model_name, device)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@spaces.GPU(duration=120)
|
| 28 |
+
def describe_image(image: str, question: str) -> str:
|
| 29 |
+
"""
|
| 30 |
+
Generates an answer to a given question based on the provided image and question.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
- image (str): The path to the image file.
|
| 34 |
+
- question (str): The question text.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
str: The generated answer to the question.
|
| 38 |
+
"""
|
| 39 |
+
try:
|
| 40 |
+
# Message format for the model
|
| 41 |
+
msgs = [{"role": "user", "content": [image, question]}]
|
| 42 |
+
|
| 43 |
+
# Generate the answer
|
| 44 |
+
answer = model.chat(
|
| 45 |
+
image=None,
|
| 46 |
+
msgs=msgs,
|
| 47 |
+
tokenizer=tokenizer,
|
| 48 |
+
sampling=sampling,
|
| 49 |
+
stream=stream,
|
| 50 |
+
top_p=top_p,
|
| 51 |
+
top_k=top_k,
|
| 52 |
+
temperature=temperature,
|
| 53 |
+
repetition_penalty=repetition_penalty,
|
| 54 |
+
max_new_tokens=max_new_tokens,
|
| 55 |
+
system_prompt=system_prompt,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Log the successful generation of the answer
|
| 59 |
+
logging.info("Answer generated successfully.")
|
| 60 |
+
|
| 61 |
+
# Return the answer
|
| 62 |
+
return "".join(answer)
|
| 63 |
+
|
| 64 |
+
# Handle exceptions that may occur during answer generation
|
| 65 |
+
except Exception as e:
|
| 66 |
+
# Custom exception handling
|
| 67 |
+
raise CustomExceptionHandling(e, sys) from e
|
src/config.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Model settings
|
| 2 |
+
device = "cuda"
|
| 3 |
+
model_name = "openbmb/MiniCPM-V-2_6"
|
| 4 |
+
|
| 5 |
+
# Decoding settings
|
| 6 |
+
sampling = True
|
| 7 |
+
stream = True
|
| 8 |
+
top_p = 0.8
|
| 9 |
+
top_k = 100
|
| 10 |
+
temperature = 0.7
|
| 11 |
+
repetition_penalty = 1.05
|
| 12 |
+
max_new_tokens = 2048
|
| 13 |
+
|
| 14 |
+
# System Prompt
|
| 15 |
+
system_prompt = """
|
| 16 |
+
You are an AI assistant specialized in visual content analysis.
|
| 17 |
+
Given an image and a related question, analyze the image thoroughly and provide a precise and informative answer based on the visible content.
|
| 18 |
+
Ensure your response is clear, accurate, and directly addresses the question.
|
| 19 |
+
"""
|
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 |
+
)
|