Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,6 @@
|
|
1 |
# Importing the requirements
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
from PIL import Image
|
5 |
-
from transformers import AutoModel, AutoTokenizer
|
6 |
-
import spaces
|
7 |
-
|
8 |
-
# Device for the model
|
9 |
-
device = "cuda"
|
10 |
-
|
11 |
-
# Load the model and tokenizer
|
12 |
-
model = AutoModel.from_pretrained(
|
13 |
-
"openbmb/MiniCPM-Llama3-V-2_5", trust_remote_code=True, torch_dtype=torch.float16
|
14 |
-
)
|
15 |
-
model = model.to(device="cuda")
|
16 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
17 |
-
"openbmb/MiniCPM-Llama3-V-2_5", trust_remote_code=True
|
18 |
-
)
|
19 |
-
model.eval()
|
20 |
-
|
21 |
-
|
22 |
-
@spaces.GPU(duration=120)
|
23 |
-
def answer_question(image, question):
|
24 |
-
"""
|
25 |
-
Generates an answer to a given question based on the provided image and question.
|
26 |
-
|
27 |
-
Args:
|
28 |
-
image (str): The path to the image file.
|
29 |
-
question (str): The question text.
|
30 |
-
|
31 |
-
Returns:
|
32 |
-
str: The generated answer to the question.
|
33 |
-
"""
|
34 |
-
|
35 |
-
# Message format for the model
|
36 |
-
msgs = [{"role": "user", "content": question}]
|
37 |
-
|
38 |
-
# Generate the answer
|
39 |
-
res = model.chat(
|
40 |
-
image=image,
|
41 |
-
msgs=msgs,
|
42 |
-
tokenizer=tokenizer,
|
43 |
-
sampling=True,
|
44 |
-
temperature=0.7,
|
45 |
-
stream=True,
|
46 |
-
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.",
|
47 |
-
)
|
48 |
-
|
49 |
-
# Return the answer
|
50 |
-
return "".join(res)
|
51 |
|
52 |
|
53 |
# Image and text inputs for the interface
|
@@ -59,9 +12,9 @@ answer = gr.Textbox(label="Predicted answer")
|
|
59 |
|
60 |
# Examples for the interface
|
61 |
examples = [
|
62 |
-
["cat.jpg", "How many cats are there?"],
|
63 |
-
["dog.jpg", "What color is the dog?"],
|
64 |
-
["bird.jpg", "What is the bird doing?"],
|
65 |
]
|
66 |
|
67 |
# Title, description, and article for the interface
|
|
|
1 |
# Importing the requirements
|
2 |
import gradio as gr
|
3 |
+
from model import answer_question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
|
6 |
# Image and text inputs for the interface
|
|
|
12 |
|
13 |
# Examples for the interface
|
14 |
examples = [
|
15 |
+
["images/cat.jpg", "How many cats are there?"],
|
16 |
+
["images/dog.jpg", "What color is the dog?"],
|
17 |
+
["images/bird.jpg", "What is the bird doing?"],
|
18 |
]
|
19 |
|
20 |
# Title, description, and article for the interface
|