Spaces:
Runtime error
Runtime error
File size: 1,824 Bytes
d49d27b 4118429 0220555 4b5dfcd d49d27b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# Necessary imports
import sys
import gradio as gr
import spaces
# Local imports
from src.config import (
device,
model_name,
sampling,
stream,
top_p,
top_k,
temperature,
repetition_penalty,
max_new_tokens,
)
from src.app.model import load_model_and_tokenizer
from src.logger import logging
from src.exception import CustomExceptionHandling
# Model and tokenizer
model, tokenizer = load_model_and_tokenizer(model_name, device)
@spaces.GPU(duration=120)
def describe_image(image: str, question: str) -> 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.
Returns:
str: The generated answer to the question.
"""
try:
# Check if image or question is None
if not image or not question:
gr.Warning("Please provide an image and a question.")
# Message format for the model
msgs = [{"role": "user", "content": [image, question]}]
# Generate the answer
answer = model.chat(
image=None,
msgs=msgs,
tokenizer=tokenizer,
sampling=sampling,
stream=stream,
top_p=top_p,
top_k=top_k,
temperature=temperature,
repetition_penalty=repetition_penalty,
max_new_tokens=max_new_tokens,
)
# Log the successful generation of the answer
logging.info("Answer generated successfully.")
# Return the answer
return "".join(answer)
# Handle exceptions that may occur during answer generation
except Exception as e:
# Custom exception handling
raise CustomExceptionHandling(e, sys) from e
|