Add application file
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from gradio import themes
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
# Load the model and tokenizer
|
@@ -13,34 +13,20 @@ tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
|
13 |
|
14 |
def analyze_image_direct(image, question):
|
15 |
# Convert PIL Image to the format expected by the model
|
16 |
-
# This
|
17 |
-
|
|
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
|
24 |
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
class PurpleTheme(themes.Theme):
|
28 |
-
base = "light"
|
29 |
-
font = "Arial"
|
30 |
-
colors = {
|
31 |
-
"primary": "#9b59b6",
|
32 |
-
"text": "#FFFFFF",
|
33 |
-
"background": "#5B2C6F",
|
34 |
-
"secondary_background": "#7D3C98",
|
35 |
-
}
|
36 |
-
|
37 |
-
# Create Gradio interface with the custom theme
|
38 |
-
iface = gr.Interface(fn=analyze_image_direct,
|
39 |
-
theme=PurpleTheme(),
|
40 |
-
inputs=[gr.Image(type="pil"), gr.Textbox(lines=2, placeholder="Enter your question here...")],
|
41 |
-
outputs='text',
|
42 |
-
title="Direct Image Question Answering",
|
43 |
-
description="Upload an image and ask a question about it directly using the model.")
|
44 |
-
|
45 |
-
# Launch the interface
|
46 |
-
iface.launch()
|
|
|
|
|
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
import numpy as np
|
5 |
|
6 |
# Load the model and tokenizer
|
|
|
13 |
|
14 |
def analyze_image_direct(image, question):
|
15 |
# Convert PIL Image to the format expected by the model
|
16 |
+
# Note: This step depends on the model's expected input format
|
17 |
+
# For demonstration, assuming the model accepts PIL images directly
|
18 |
+
enc_image = model.encode_image(image) # This method might not exist; adjust based on actual model capabilities
|
19 |
|
20 |
+
# Generate an answer to the question based on the encoded image
|
21 |
+
# Note: This step is hypothetical and depends on the model's capabilities
|
22 |
+
answer = model.answer_question(enc_image, question, tokenizer) # Adjust based on actual model capabilities
|
|
|
23 |
|
24 |
return answer
|
25 |
+
# Create a Gradio interface
|
26 |
+
with gr.Block() as block:
|
27 |
+
image = gr.inputs.Image(label="Image")
|
28 |
+
question = gr.inputs.Textbox(label="Question")
|
29 |
+
output = gr.outputs.Textbox(label="Answer")
|
30 |
+
gr.Interface(fn=analyze_image_direct, inputs=[image, question], outputs=output).launch()
|
31 |
|
32 |
+
block.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|