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