Add application file
Browse files
app.py
CHANGED
@@ -1,10 +1,6 @@
|
|
1 |
-
from __future__ import annotations
|
2 |
-
from typing import Iterable
|
3 |
import gradio as gr
|
4 |
-
from gradio
|
5 |
-
from gradio.themes.utils import colors, fonts, sizes
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
-
from PIL import Image
|
8 |
import numpy as np
|
9 |
|
10 |
# Load the model and tokenizer
|
@@ -16,46 +12,35 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
17 |
|
18 |
def analyze_image_direct(image, question):
|
19 |
-
# This is a placeholder; modify based on the actual capabilities of your model.
|
20 |
-
# Here we assume that the model has methods `encode_image` and `answer_question` which might not exist.
|
21 |
-
# You need to replace them with the actual methods your model uses to process images and generate answers.
|
22 |
-
|
23 |
# Convert PIL Image to the format expected by the model
|
24 |
-
#
|
25 |
-
enc_image = np.array(image)
|
26 |
|
27 |
-
#
|
28 |
-
inputs = tokenizer
|
29 |
-
outputs = model.generate(inputs, max_length=50)
|
30 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
|
32 |
return answer
|
33 |
|
34 |
# Define a custom theme with purple color scheme
|
35 |
-
class PurpleTheme(
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
self.primary_text_color = colors.white
|
45 |
-
self.secondary_background_color = colors.purple_700
|
46 |
-
self.secondary_text_color = colors.white
|
47 |
-
self.font = fonts.GoogleFont("Arial")
|
48 |
-
|
49 |
-
# Create a custom theme instance
|
50 |
-
purple_theme = PurpleTheme()
|
51 |
|
52 |
# Create Gradio interface with the custom theme
|
53 |
iface = gr.Interface(fn=analyze_image_direct,
|
54 |
-
theme=
|
55 |
inputs=[gr.Image(type="pil"), gr.Textbox(lines=2, placeholder="Enter your question here...")],
|
56 |
outputs='text',
|
57 |
title="Direct Image Question Answering",
|
58 |
description="Upload an image and ask a question about it directly using the model.")
|
59 |
|
60 |
# Launch the interface
|
61 |
-
iface.launch()
|
|
|
|
|
|
|
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
|
|
|
12 |
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 is a placeholder transformation; adjust as needed
|
17 |
+
enc_image = np.array(image)
|
18 |
|
19 |
+
# Example of processing text input with the model
|
20 |
+
inputs = tokenizer(question, return_tensors='pt')
|
21 |
+
outputs = model.generate(**inputs, max_length=50)
|
22 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
|
24 |
return answer
|
25 |
|
26 |
# Define a custom theme with purple color scheme
|
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()
|