Ashrafb commited on
Commit
2a2c5ee
·
1 Parent(s): 755f657

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
app.py CHANGED
@@ -1,25 +1,33 @@
1
- import requests
2
  import streamlit as st
3
- import base64
4
 
 
 
 
 
5
  def main():
6
- st.title("Hugging Face Spaces Image Captioning App")
7
 
8
- uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
9
-
10
- if uploaded_image is not None:
11
- # Read the uploaded image and convert it to base64
12
- image_content = uploaded_image.read()
13
- encoded_image = base64.b64encode(image_content).decode()
14
-
15
- # Make a request to the Hugging Face Spaces API
16
- response = requests.post("https://ashrafb-salesforce-blip-image-captioning-base.hf.space/run/predict", json={
17
- "data": ["data:image/png;base64," + encoded_image]
18
- }).json()
19
-
20
- # Display the caption
21
- caption = response["data"][0]
22
- st.image(image_content, caption=caption, use_column_width=True)
 
 
 
 
23
 
 
24
  if __name__ == "__main__":
25
  main()
 
 
1
  import streamlit as st
2
+ import requests
3
 
4
+ # Define the API endpoint
5
+ API_ENDPOINT = "https://ashrafb-salesforce-blip-image-captioning-base.hf.space/run/predict"
6
+
7
+ # Define the Streamlit app
8
  def main():
9
+ st.title("Image Captioning App")
10
 
11
+ # Upload image
12
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
13
+
14
+ if uploaded_file is not None:
15
+ # Display the uploaded image
16
+ st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
17
+
18
+ # Prepare the data for the API request
19
+ image_data = uploaded_file.read()
20
+ image_base64 = b64encode(image_data).decode('utf-8')
21
+ payload = {"data": ["data:image/jpeg;base64," + image_base64]}
22
+
23
+ # Make the API request
24
+ response = requests.post(API_ENDPOINT, json=payload).json()
25
+
26
+ # Display the caption from the API response
27
+ caption = response["data"][0]["captions"][0]["caption"]
28
+ st.subheader("Generated Caption:")
29
+ st.write(caption)
30
 
31
+ # Run the app
32
  if __name__ == "__main__":
33
  main()