Spaces:
Runtime error
Runtime error
Delete model.py
Browse files
model.py
DELETED
@@ -1,55 +0,0 @@
|
|
1 |
-
# Importing the requirements
|
2 |
-
import torch
|
3 |
-
from transformers import AutoModel, AutoTokenizer
|
4 |
-
import spaces
|
5 |
-
|
6 |
-
|
7 |
-
# Device for the model
|
8 |
-
device = "cuda"
|
9 |
-
|
10 |
-
# Load the model and tokenizer
|
11 |
-
model = AutoModel.from_pretrained(
|
12 |
-
"openbmb/MiniCPM-V-2_6",
|
13 |
-
trust_remote_code=True,
|
14 |
-
attn_implementation="sdpa",
|
15 |
-
torch_dtype=torch.bfloat16,
|
16 |
-
)
|
17 |
-
model = model.to(device=device)
|
18 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
19 |
-
"openbmb/MiniCPM-V-2_6", trust_remote_code=True
|
20 |
-
)
|
21 |
-
model.eval()
|
22 |
-
|
23 |
-
|
24 |
-
@spaces.GPU(duration=120)
|
25 |
-
def answer_question(image, question):
|
26 |
-
"""
|
27 |
-
Generates an answer to a given question based on the provided image and question.
|
28 |
-
|
29 |
-
Args:
|
30 |
-
- image (str): The path to the image file.
|
31 |
-
- question (str): The question text.
|
32 |
-
|
33 |
-
Returns:
|
34 |
-
str: The generated answer to the question.
|
35 |
-
"""
|
36 |
-
# Message format for the model
|
37 |
-
msgs = [{"role": "user", "content": [image, question]}]
|
38 |
-
|
39 |
-
# Generate the answer
|
40 |
-
res = model.chat(
|
41 |
-
image=None,
|
42 |
-
msgs=msgs,
|
43 |
-
tokenizer=tokenizer,
|
44 |
-
sampling=True,
|
45 |
-
stream=True,
|
46 |
-
top_p=0.8,
|
47 |
-
top_k=100,
|
48 |
-
temperature=0.7,
|
49 |
-
repetition_penalty=1.05,
|
50 |
-
max_new_tokens=2048,
|
51 |
-
system_prompt="You are an AI assistant specialized in visual content analysis. Given an image and a related question, analyze the image thoroughly and provide a precise and informative answer based on the visible content. Ensure your response is clear, accurate, and directly addresses the question.",
|
52 |
-
)
|
53 |
-
|
54 |
-
# Return the answer
|
55 |
-
return "".join(res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|