manan145 commited on
Commit
8f210d6
·
verified ·
1 Parent(s): 73def8d

update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -1,30 +1,41 @@
1
  import streamlit as st
2
  import requests
 
3
 
4
- # Replace this with your Google cloud run container endpoint
5
  API_BASE = "https://medical-vqa-955125037464.us-central1.run.app"
6
 
7
  st.title("Visual Question Answering (VQA)")
8
 
9
  if "image_id" not in st.session_state:
10
  st.session_state["image_id"] = None
 
 
11
 
12
  # Upload an image
13
  uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
14
 
15
  if uploaded_image is not None and st.session_state["image_id"] is None:
16
- # Convert the uploaded file to a suitable format for requests
17
- files = {"image_file": (uploaded_image.name, uploaded_image, uploaded_image.type)}
 
 
 
 
18
  response = requests.post(f"{API_BASE}/upload_image", files=files)
19
  if response.status_code == 200:
20
  data = response.json()
21
  st.session_state["image_id"] = data["image_id"]
22
- st.image(uploaded_image, caption="Uploaded Image", use_container_width=True)
 
23
  else:
24
  st.error("Failed to upload image. Please try again.")
25
 
26
  # If we have an image_id, allow asking questions
27
  if st.session_state["image_id"] is not None:
 
 
 
28
  question = st.text_input("Enter your question about the image:")
29
  if question and st.button("Ask"):
30
  response = requests.post(f"{API_BASE}/ask_question", data={"image_id": st.session_state["image_id"], "question": question})
@@ -40,5 +51,7 @@ if st.session_state["image_id"] is not None:
40
  st.experimental_rerun()
41
 
42
  if st.button("Upload New Image"):
 
43
  st.session_state["image_id"] = None
 
44
  st.experimental_rerun()
 
1
  import streamlit as st
2
  import requests
3
+ from io import BytesIO
4
 
5
+ # Replace this with your Google Cloud Run container endpoint
6
  API_BASE = "https://medical-vqa-955125037464.us-central1.run.app"
7
 
8
  st.title("Visual Question Answering (VQA)")
9
 
10
  if "image_id" not in st.session_state:
11
  st.session_state["image_id"] = None
12
+ if "image_bytes" not in st.session_state:
13
+ st.session_state["image_bytes"] = None
14
 
15
  # Upload an image
16
  uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
17
 
18
  if uploaded_image is not None and st.session_state["image_id"] is None:
19
+ # Read the uploaded image as bytes so we can display it later
20
+ image_bytes = uploaded_image.read()
21
+ # Reset the file pointer for the request
22
+ uploaded_image.seek(0)
23
+
24
+ files = {"image_file": (uploaded_image.name, image_bytes, uploaded_image.type)}
25
  response = requests.post(f"{API_BASE}/upload_image", files=files)
26
  if response.status_code == 200:
27
  data = response.json()
28
  st.session_state["image_id"] = data["image_id"]
29
+ st.session_state["image_bytes"] = image_bytes
30
+ st.image(image_bytes, caption="Uploaded Image", use_container_width=True)
31
  else:
32
  st.error("Failed to upload image. Please try again.")
33
 
34
  # If we have an image_id, allow asking questions
35
  if st.session_state["image_id"] is not None:
36
+ # Always display the image before asking questions
37
+ st.image(st.session_state["image_bytes"], caption="Uploaded Image", use_container_width=True)
38
+
39
  question = st.text_input("Enter your question about the image:")
40
  if question and st.button("Ask"):
41
  response = requests.post(f"{API_BASE}/ask_question", data={"image_id": st.session_state["image_id"], "question": question})
 
51
  st.experimental_rerun()
52
 
53
  if st.button("Upload New Image"):
54
+ # Reset the state to allow a new image to be uploaded
55
  st.session_state["image_id"] = None
56
+ st.session_state["image_bytes"] = None
57
  st.experimental_rerun()