|
|
|
import sys
|
|
import gradio as gr
|
|
import spaces
|
|
|
|
|
|
from src.config import (
|
|
device,
|
|
model_name,
|
|
sampling,
|
|
stream,
|
|
repetition_penalty,
|
|
)
|
|
from src.minicpm.model import load_model_tokenizer_and_processor
|
|
from src.logger import logging
|
|
from src.exception import CustomExceptionHandling
|
|
|
|
|
|
|
|
model, tokenizer, processor = load_model_tokenizer_and_processor(model_name, device)
|
|
|
|
|
|
@spaces.GPU(duration=120)
|
|
def describe_image(
|
|
image: str,
|
|
question: str,
|
|
temperature: float,
|
|
top_p: float,
|
|
top_k: int,
|
|
max_new_tokens: int,
|
|
) -> str:
|
|
"""
|
|
Generates an answer to a given question based on the provided image and question.
|
|
|
|
Args:
|
|
- image (str): The path to the image file.
|
|
- question (str): The question text.
|
|
- temperature (float): The temperature parameter for the model.
|
|
- top_p (float): The top_p parameter for the model.
|
|
- top_k (int): The top_k parameter for the model.
|
|
- max_new_tokens (int): The max tokens to be generated by the model.
|
|
|
|
Returns:
|
|
str: The generated answer to the question.
|
|
"""
|
|
try:
|
|
|
|
if not image or not question:
|
|
gr.Warning("Please provide an image and a question.")
|
|
|
|
|
|
msgs = [{"role": "user", "content": [image, question]}]
|
|
|
|
|
|
answer = model.chat(
|
|
image=None,
|
|
msgs=msgs,
|
|
tokenizer=tokenizer,
|
|
processor=processor,
|
|
sampling=sampling,
|
|
stream=stream,
|
|
top_p=top_p,
|
|
top_k=top_k,
|
|
temperature=temperature,
|
|
repetition_penalty=repetition_penalty,
|
|
max_new_tokens=max_new_tokens,
|
|
)
|
|
|
|
|
|
logging.info("Answer generated successfully.")
|
|
|
|
|
|
return "".join(answer)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
raise CustomExceptionHandling(e, sys) from e
|
|
|