Upload 7 files
Browse files- src/__init__.py +0 -0
- src/config.py +8 -0
- src/exception.py +50 -0
- src/logger.py +21 -0
- src/minicpm/__init__.py +0 -0
- src/minicpm/model.py +63 -0
- src/minicpm/response.py +78 -0
src/__init__.py
ADDED
File without changes
|
src/config.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Model settings
|
2 |
+
device = "cuda"
|
3 |
+
model_name = "openbmb/MiniCPM-o-2_6"
|
4 |
+
|
5 |
+
# Decoding settings
|
6 |
+
sampling = False
|
7 |
+
stream = False
|
8 |
+
repetition_penalty = 1.05
|
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/minicpm/__init__.py
ADDED
File without changes
|
src/minicpm/model.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Necessary imports
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from typing import Any
|
6 |
+
import torch
|
7 |
+
from transformers import AutoModel, AutoTokenizer, AutoProcessor
|
8 |
+
|
9 |
+
# Local imports
|
10 |
+
from src.logger import logging
|
11 |
+
from src.exception import CustomExceptionHandling
|
12 |
+
|
13 |
+
|
14 |
+
# Load the Environment Variables from .env file
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# Access token for using the model
|
18 |
+
access_token = os.environ.get("ACCESS_TOKEN")
|
19 |
+
|
20 |
+
|
21 |
+
def load_model_tokenizer_and_processor(model_name: str, device: str) -> Any:
|
22 |
+
"""
|
23 |
+
Load the model, tokenizer and processor.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
- model_name (str): The name of the model to load.
|
27 |
+
- device (str): The device to load the model onto.
|
28 |
+
|
29 |
+
Returns:
|
30 |
+
- model: The loaded model.
|
31 |
+
- tokenizer: The loaded tokenizer.
|
32 |
+
- processor: The loaded processor.
|
33 |
+
"""
|
34 |
+
try:
|
35 |
+
# Load the model, tokenizer and processor
|
36 |
+
model = AutoModel.from_pretrained(
|
37 |
+
model_name,
|
38 |
+
trust_remote_code=True,
|
39 |
+
attn_implementation="sdpa",
|
40 |
+
torch_dtype=torch.bfloat16,
|
41 |
+
init_vision=True,
|
42 |
+
init_audio=False,
|
43 |
+
init_tts=False,
|
44 |
+
token=access_token
|
45 |
+
)
|
46 |
+
model = model.eval().to(device=device)
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
48 |
+
model_name, trust_remote_code=True, token=access_token
|
49 |
+
)
|
50 |
+
processor = AutoProcessor.from_pretrained(
|
51 |
+
model_name, trust_remote_code=True, token=access_token
|
52 |
+
)
|
53 |
+
|
54 |
+
# Log the successful loading of the model, tokenizer and processor
|
55 |
+
logging.info("Model, tokenizer and processor loaded successfully.")
|
56 |
+
|
57 |
+
# Return the model, tokenizer and processor
|
58 |
+
return model, tokenizer, processor
|
59 |
+
|
60 |
+
# Handle exceptions that may occur during model, tokenizer and processor loading
|
61 |
+
except Exception as e:
|
62 |
+
# Custom exception handling
|
63 |
+
raise CustomExceptionHandling(e, sys) from e
|
src/minicpm/response.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Necessary imports
|
2 |
+
import sys
|
3 |
+
import gradio as gr
|
4 |
+
import spaces
|
5 |
+
|
6 |
+
# Local imports
|
7 |
+
from src.config import (
|
8 |
+
device,
|
9 |
+
model_name,
|
10 |
+
sampling,
|
11 |
+
stream,
|
12 |
+
repetition_penalty,
|
13 |
+
)
|
14 |
+
from src.minicpm.model import load_model_tokenizer_and_processor
|
15 |
+
from src.logger import logging
|
16 |
+
from src.exception import CustomExceptionHandling
|
17 |
+
|
18 |
+
|
19 |
+
# Model, tokenizer and processor
|
20 |
+
model, tokenizer, processor = load_model_tokenizer_and_processor(model_name, device)
|
21 |
+
|
22 |
+
|
23 |
+
@spaces.GPU(duration=120)
|
24 |
+
def describe_image(
|
25 |
+
image: str,
|
26 |
+
question: str,
|
27 |
+
temperature: float,
|
28 |
+
top_p: float,
|
29 |
+
top_k: int,
|
30 |
+
max_new_tokens: int,
|
31 |
+
) -> str:
|
32 |
+
"""
|
33 |
+
Generates an answer to a given question based on the provided image and question.
|
34 |
+
|
35 |
+
Args:
|
36 |
+
- image (str): The path to the image file.
|
37 |
+
- question (str): The question text.
|
38 |
+
- temperature (float): The temperature parameter for the model.
|
39 |
+
- top_p (float): The top_p parameter for the model.
|
40 |
+
- top_k (int): The top_k parameter for the model.
|
41 |
+
- max_new_tokens (int): The max tokens to be generated by the model.
|
42 |
+
|
43 |
+
Returns:
|
44 |
+
str: The generated answer to the question.
|
45 |
+
"""
|
46 |
+
try:
|
47 |
+
# Check if image or question is None
|
48 |
+
if not image or not question:
|
49 |
+
gr.Warning("Please provide an image and a question.")
|
50 |
+
|
51 |
+
# Message format for the model
|
52 |
+
msgs = [{"role": "user", "content": [image, question]}]
|
53 |
+
|
54 |
+
# Generate the answer
|
55 |
+
answer = model.chat(
|
56 |
+
image=None,
|
57 |
+
msgs=msgs,
|
58 |
+
tokenizer=tokenizer,
|
59 |
+
processor=processor,
|
60 |
+
sampling=sampling,
|
61 |
+
stream=stream,
|
62 |
+
top_p=top_p,
|
63 |
+
top_k=top_k,
|
64 |
+
temperature=temperature,
|
65 |
+
repetition_penalty=repetition_penalty,
|
66 |
+
max_new_tokens=max_new_tokens,
|
67 |
+
)
|
68 |
+
|
69 |
+
# Log the successful generation of the answer
|
70 |
+
logging.info("Answer generated successfully.")
|
71 |
+
|
72 |
+
# Return the answer
|
73 |
+
return "".join(answer)
|
74 |
+
|
75 |
+
# Handle exceptions that may occur during answer generation
|
76 |
+
except Exception as e:
|
77 |
+
# Custom exception handling
|
78 |
+
raise CustomExceptionHandling(e, sys) from e
|