File size: 4,601 Bytes
eb768aa
154d965
80bcc31
 
 
 
 
557cd0f
 
7b36a49
33d03cd
557cd0f
33d03cd
 
557cd0f
d644eb8
33d03cd
4fbfa7c
33d03cd
 
 
58391cf
eb768aa
 
 
557cd0f
eb768aa
 
 
 
557cd0f
eb768aa
 
 
557cd0f
eb768aa
 
557cd0f
eb768aa
 
 
557cd0f
eb768aa
 
 
 
 
 
 
 
557cd0f
eb768aa
 
 
557cd0f
eb768aa
 
557cd0f
eb768aa
 
 
557cd0f
eb768aa
 
557cd0f
58391cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a02f02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import streamlit as st
from bertopic import BERTopic
from PIL import Image
from transformers import (
    pipeline,
    BlenderbotTokenizer,
    BlenderbotForConditionalGeneration,
)

st.title("Georgios Ioannou's Visual Question Answering With Hugging Face")
st.write("Drag and drop an image file here.")

# Allow the user to upload an image file
image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if image is not None:
    # Display the uploaded image
    image = Image.open(image)
    st.image(image, caption="Uploaded Image", use_column_width=True)

question = st.text_input("What's your question?")

# Model 1.
# Model 1 gets input from the user.
# User -> Model 1

vqa_pipeline = pipeline(
    task="visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa"
)
#########################################################################

# Model 2.
# Model 2 gets input from Model 1.
# User -> Model 1 -> Model 2

bbu_pipeline = pipeline(task="fill-mask", model="bert-base-uncased")
#########################################################################

# Model 3.
# Model 3 gets input from Model 2.
# User -> Model 1 -> Model 2 -> Model 3

model_name = "facebook/blenderbot-400M-distill"
tokenizer = BlenderbotTokenizer.from_pretrained(
    pretrained_model_name_or_path=model_name
)
facebook_model = BlenderbotForConditionalGeneration.from_pretrained(
    pretrained_model_name_or_path=model_name
)
#########################################################################

# Model 4.
# Model 4 gets input from Model 3.
# User -> Model 1 -> Model 2 -> Model 3 -> Model 4

gpt2_pipeline = pipeline(task="text-generation", model="gpt2")
#########################################################################

# Model 5.
# Model 5 gets input from Model 4.
# User -> Model 1 -> Model 2 -> Model 3 -> Model 4 -> Model 5

topic_model_1 = BERTopic.load(path="davanstrien/chat_topics")
topic_model_2 = BERTopic.load(path="MaartenGr/BERTopic_ArXiv")

 ###############################################################################

# 5 MODEL INFERENCES.
# User Input = Image + Question About The Image.
# User -> Model 1 -> Model 2 -> Model 3 -> Model 4 -> Model 5

# Model 1.

vqa_pipeline_output = vqa_pipeline(image, question, top_k=5)[0]

# Model 2.

text = (
    "I love "
    + str(vqa_pipeline_output["answer"])
    + " and I would like to know how to [MASK]."
)
bbu_pipeline_output = bbu_pipeline(text)

# Model 3.

utterance = bbu_pipeline_output[0]["sequence"]
inputs = tokenizer(utterance, return_tensors="pt")
result = facebook_model.generate(**inputs)
facebook_model_output = tokenizer.decode(result[0])

# Model 4.

facebook_model_output = facebook_model_output.replace("<s> ", "")
facebook_model_output = facebook_model_output.replace("<s>", "")
facebook_model_output = facebook_model_output.replace("</s>", "")
gpt2_pipeline_output = gpt2_pipeline(facebook_model_output)[0]["generated_text"]

# Model 5.

topic, prob = topic_model_1.transform(gpt2_pipeline_output)
topic_model_1_output = topic_model_1.get_topic_info(topic[0])["Representation"][
    0
]

topic, prob = topic_model_2.transform(gpt2_pipeline_output)
topic_model_2_output = topic_model_2.get_topic_info(topic[0])["Representation"][
    0
]
###############################################################################

st.write("-" * 150)
st.write("vqa_pipeline_output =", vqa_pipeline_output)
st.write("bbu_pipeline_output =", bbu_pipeline_output)
st.write("facebook_model_output =", facebook_model_output)
st.write("gpt2_pipeline_output =", gpt2_pipeline_output)
st.write("topic_model_1_output =", topic_model_1_output)
st.write("topic_model_2_output =", topic_model_2_output)
st.write("-" * 150)

st.write("SUMMARY")
st.subheader("Your Image:")
st.image(image, caption="Your Image", use_column_width=True)
st.subheader("Your Question:")
st.write(question)
st.write("-" * 100)

st.subheader("1. Highest Predicted Answer For Your Question:")
st.write(vqa_pipeline_output["answer"])
st.write(text)

st.subheader("2. Highest Predicted Sequence On [MASK] Based on 1.:")
st.write(bbu_pipeline_output[0]["sequence"])

st.subheader("3. Conversation Based On Previous Answer Based on 2.:")
st.write(facebook_model_output)

st.subheader("4. Text Generated Based On Previous Answer Based on 3.:")
st.write(gpt2_pipeline_output)

st.subheader("5. Highest Predicted Topic Model_1 For Previous The Answer Based on 4.:")
st.write(topic_model_1_output)

st.subheader("6. Highest Predicted Topic Model_2 For Previous The Answer Based on 4.:")
st.write(topic_model_2_output)

st.write("-" * 150)