Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from bertopic import BERTopic
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import (
|
5 |
+
pipeline,
|
6 |
+
BlenderbotTokenizer,
|
7 |
+
BlenderbotForConditionalGeneration,
|
8 |
+
)
|
9 |
+
|
10 |
+
# Function to load VQA pipeline
|
11 |
+
@st.cache(allow_output_mutation=True)
|
12 |
+
def load_vqa_pipeline():
|
13 |
+
return pipeline(task="visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
|
14 |
+
|
15 |
+
# Function to load BERT-based pipeline
|
16 |
+
@st.cache(allow_output_mutation=True)
|
17 |
+
def load_bbu_pipeline():
|
18 |
+
return pipeline(task="fill-mask", model="bert-base-uncased")
|
19 |
+
|
20 |
+
# Function to load Blenderbot model
|
21 |
+
@st.cache(allow_output_mutation=True)
|
22 |
+
def load_blenderbot_model():
|
23 |
+
model_name = "facebook/blenderbot-400M-distill"
|
24 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(pretrained_model_name_or_path=model_name)
|
25 |
+
return BlenderbotForConditionalGeneration.from_pretrained(pretrained_model_name_or_path=model_name)
|
26 |
+
|
27 |
+
# Function to load GPT-2 pipeline
|
28 |
+
@st.cache(allow_output_mutation=True)
|
29 |
+
def load_gpt2_pipeline():
|
30 |
+
return pipeline(task="text-generation", model="gpt2")
|
31 |
+
|
32 |
+
# Function to load BERTopic models
|
33 |
+
@st.cache(allow_output_mutation=True)
|
34 |
+
def load_topic_models():
|
35 |
+
topic_model_1 = BERTopic.load(path="davanstrien/chat_topics")
|
36 |
+
topic_model_2 = BERTopic.load(path="MaartenGr/BERTopic_ArXiv")
|
37 |
+
return topic_model_1, topic_model_2
|
38 |
+
|
39 |
+
st.title("Georgios Ioannou's Visual Question Answering With Hugging Face")
|
40 |
+
st.write("Drag and drop an image file here.")
|
41 |
+
|
42 |
+
# Allow the user to upload an image file
|
43 |
+
image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
44 |
+
|
45 |
+
if image is not None:
|
46 |
+
# Display the uploaded image
|
47 |
+
image = Image.open(image)
|
48 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
49 |
+
|
50 |
+
question = st.text_input("What's your question?")
|
51 |
+
|
52 |
+
# Load models using the cache
|
53 |
+
vqa_pipeline = load_vqa_pipeline()
|
54 |
+
bbu_pipeline = load_bbu_pipeline()
|
55 |
+
facebook_model = load_blenderbot_model()
|
56 |
+
gpt2_pipeline = load_gpt2_pipeline()
|
57 |
+
topic_model_1, topic_model_2 = load_topic_models()
|
58 |
+
|
59 |
+
# Model 1.
|
60 |
+
|
61 |
+
vqa_pipeline_output = vqa_pipeline(image, question, top_k=5)[0]
|
62 |
+
|
63 |
+
# Model 2.
|
64 |
+
|
65 |
+
text = (
|
66 |
+
"I love "
|
67 |
+
+ str(vqa_pipeline_output["answer"])
|
68 |
+
+ " and I would like to know how to [MASK]."
|
69 |
+
)
|
70 |
+
bbu_pipeline_output = bbu_pipeline(text)
|
71 |
+
|
72 |
+
# Model 3.
|
73 |
+
|
74 |
+
utterance = bbu_pipeline_output[0]["sequence"]
|
75 |
+
inputs = tokenizer(utterance, return_tensors="pt")
|
76 |
+
result = facebook_model.generate(**inputs)
|
77 |
+
facebook_model_output = tokenizer.decode(result[0])
|
78 |
+
|
79 |
+
# Model 4.
|
80 |
+
|
81 |
+
facebook_model_output = facebook_model_output.replace("<s> ", "")
|
82 |
+
facebook_model_output = facebook_model_output.replace("<s>", "")
|
83 |
+
facebook_model_output = facebook_model_output.replace("</s>", "")
|
84 |
+
gpt2_pipeline_output = gpt2_pipeline(facebook_model_output)[0]["generated_text"]
|
85 |
+
|
86 |
+
# Model 5.
|
87 |
+
|
88 |
+
topic, prob = topic_model_1.transform(gpt2_pipeline_output)
|
89 |
+
topic_model_1_output = topic_model_1.get_topic_info(topic[0])["Representation"][
|
90 |
+
0
|
91 |
+
]
|
92 |
+
|
93 |
+
topic, prob = topic_model_2.transform(gpt2_pipeline_output)
|
94 |
+
topic_model_2_output = topic_model_2.get_topic_info(topic[0])["Representation"][
|
95 |
+
0
|
96 |
+
]
|
97 |
+
|
98 |
+
st.write("-" * 150)
|
99 |
+
st.write("vqa_pipeline_output =", vqa_pipeline_output)
|
100 |
+
st.write("bbu_pipeline_output =", bbu_pipeline_output)
|
101 |
+
st.write("facebook_model_output =", facebook_model_output)
|
102 |
+
st.write("gpt2_pipeline_output =", gpt2_pipeline_output)
|
103 |
+
st.write("topic_model_1_output =", topic_model_1_output)
|
104 |
+
st.write("topic_model_2_output =", topic_model_2_output)
|
105 |
+
st.write("-" * 150)
|
106 |
+
|
107 |
+
st.write("SUMMARY")
|
108 |
+
st.subheader("Your Image:")
|
109 |
+
st.image(image, caption="Your Image", use_column_width=True)
|
110 |
+
st.subheader("Your Question:")
|
111 |
+
st.write(question)
|
112 |
+
st.write("-" * 100)
|
113 |
+
|
114 |
+
st.subheader("1. Highest Predicted Answer For Your Question:")
|
115 |
+
st.write(vqa_pipeline_output["answer"])
|
116 |
+
st.write(text)
|
117 |
+
|
118 |
+
st.subheader("2. Highest Predicted Sequence On [MASK] Based on 1.:")
|
119 |
+
st.write(bbu_pipeline_output[0]["sequence"])
|
120 |
+
|
121 |
+
st.subheader("3. Conversation Based On Previous Answer Based on 2.:")
|
122 |
+
st.write(facebook_model_output)
|
123 |
+
|
124 |
+
st.subheader("4. Text Generated Based On Previous Answer Based on 3.:")
|
125 |
+
st.write(gpt2_pipeline_output)
|
126 |
+
|
127 |
+
st.subheader("5. Highest Predicted Topic Model_1 For Previous The Answer Based on 4.:")
|
128 |
+
st.write(topic_model_1_output)
|
129 |
+
|
130 |
+
st.subheader("6. Highest Predicted Topic Model_2 For Previous The Answer Based on 4.:")
|
131 |
+
st.write(topic_model_2_output)
|
132 |
+
st.write("-" * 150)
|