SilvusTV commited on
Commit
438833d
1 Parent(s): c60b3a8

create function on image.py and call it on app.py

Browse files
Files changed (2) hide show
  1. app.py +6 -2
  2. image.py +18 -17
app.py CHANGED
@@ -1,7 +1,11 @@
 
1
  import streamlit as st
2
 
3
- import os
4
  x = st.slider('Select a value')
5
  st.write(x, 'squared is', x * x)
6
 
7
- os.system("python image.py")
 
 
 
 
 
1
+ from image import *
2
  import streamlit as st
3
 
 
4
  x = st.slider('Select a value')
5
  st.write(x, 'squared is', x * x)
6
 
7
+
8
+ url = "https://i.imgur.com/qs0CxjE_d.webp?maxwidth=760&fidelity=grand"
9
+ text = "What is the two color's car ?"
10
+
11
+ st.write(image(url, text))
image.py CHANGED
@@ -1,24 +1,25 @@
1
  from transformers import ViltProcessor, ViltForQuestionAnswering
2
- import streamlit as st
3
  import requests
4
  from PIL import Image
 
 
 
 
5
 
6
- # prepare image + question
7
- url = "https://i.imgur.com/qs0CxjE_d.webp?maxwidth=760&fidelity=grand"
8
- image = Image.open(requests.get(url, stream=True).raw)
9
- text = "What is the two color's car ?"
 
 
 
 
 
 
 
 
10
 
11
- processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
12
- model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
13
 
14
- # prepare inputs
15
- encoding = processor(image, text, return_tensors="pt")
16
 
17
- # forward pass
18
- outputs = model(**encoding)
19
- logits = outputs.logits
20
- idx = logits.argmax(-1).item()
21
- st.write("Predicted answer:", model.config.id2label[idx])
22
- print("question asked:", text)
23
- print("image link:", url)
24
- print("Predicted answer:", model.config.id2label[idx])
 
1
  from transformers import ViltProcessor, ViltForQuestionAnswering
 
2
  import requests
3
  from PIL import Image
4
+ def image(url, text):
5
+ image = Image.open(requests.get(url, stream=True).raw)
6
+ processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
7
+ model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
8
 
9
+ # prepare inputs
10
+ encoding = processor(image, text, return_tensors="pt")
11
+
12
+ # forward pass
13
+ outputs = model(**encoding)
14
+ logits = outputs.logits
15
+ idx = logits.argmax(-1).item()
16
+ print("question asked:", text)
17
+ print("image link:", url)
18
+ print("Predicted answer:", model.config.id2label[idx])
19
+
20
+ return "Predicted answer:", model.config.id2label[idx]
21
 
 
 
22
 
 
 
23
 
24
+ # prepare image + question
25
+