Spaces:
Build error
Build error
srivatsavdamaraju
commited on
Commit
•
fa725d5
1
Parent(s):
25b37ff
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModel, AutoTokenizer
|
3 |
-
from PIL import Image
|
4 |
import torch
|
|
|
|
|
5 |
|
6 |
-
# Load the model and tokenizer
|
7 |
-
|
8 |
-
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
# Tokenize the question input
|
14 |
-
inputs = tokenizer(question, return_tensors="pt")
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# You may need to preprocess the image here (e.g., resizing)
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
output = model(**inputs) # Adjust based on your model's input requirements
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Create the Gradio interface
|
30 |
-
|
31 |
-
fn=
|
32 |
inputs=[
|
33 |
-
gr.
|
34 |
-
gr.
|
35 |
],
|
36 |
outputs="text",
|
37 |
-
title="
|
38 |
-
description="
|
39 |
)
|
40 |
|
41 |
-
# Launch the
|
42 |
-
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import AutoModel, AutoTokenizer
|
5 |
|
6 |
+
# Load the model and tokenizer from the local path
|
7 |
+
model = AutoModel.from_pretrained('minicpm/models', trust_remote_code=True)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained('minicpm/models', trust_remote_code=True)
|
|
|
9 |
|
10 |
+
# Set the model to evaluation mode
|
11 |
+
model.eval()
|
|
|
|
|
12 |
|
13 |
+
def predict(image, question):
|
14 |
+
# Preprocess the image
|
15 |
+
image = image.convert('RGB')
|
|
|
16 |
|
17 |
+
# Create the message list
|
18 |
+
msgs = [{'role': 'user', 'content': question}]
|
|
|
19 |
|
20 |
+
# Generate a response
|
21 |
+
res = model.chat(
|
22 |
+
image=image,
|
23 |
+
msgs=msgs,
|
24 |
+
tokenizer=tokenizer,
|
25 |
+
sampling=True,
|
26 |
+
temperature=0.1
|
27 |
+
)
|
28 |
+
return res
|
29 |
|
30 |
# Create the Gradio interface
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=predict,
|
33 |
inputs=[
|
34 |
+
gr.inputs.Image(type="pil", label="Upload an Image"),
|
35 |
+
gr.inputs.Textbox(label="Ask a Question")
|
36 |
],
|
37 |
outputs="text",
|
38 |
+
title="Image Question Answering",
|
39 |
+
description="Upload an image and ask a question about it."
|
40 |
)
|
41 |
|
42 |
+
# Launch the app
|
43 |
+
iface.launch()
|