Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,33 @@
|
|
| 1 |
-
import requests
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def main():
|
| 6 |
-
st.title("
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|