File size: 1,281 Bytes
ce15fb9 9cc78ec 479c4d2 2a2c5ee 479c4d2 b3e768f 0405d46 d813764 68511b6 479c4d2 68511b6 2d3135b 479c4d2 68511b6 479c4d2 9cc78ec 479c4d2 b3e768f f9082d2 479c4d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import streamlit as st
import requests
import base64
def main():
st.title("Aiconvert.online Image Captioning App")
st.markdown('<style>h1{color: Crimson; text-align: center;}</style>', unsafe_allow_html=True)
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
# Read the image file
image_data = uploaded_file.read()
# Display the uploaded image
st.image(image_data, caption="Uploaded Image.", use_column_width=True)
# Convert image data to base64 string
image_base64 = base64.b64encode(image_data).decode('utf-8')
# Prepare the payload
payload = {"data": ["data:image/jpeg;base64," + image_base64]}
# Make the API request
response = requests.post("https://ashrafb-salesforce-blip-image-captioning-base2.hf.space/run/predict", json=payload)
# Check if response is successful (status code 200)
if response.status_code == 200:
caption = response.json()["data"][0]
st.subheader("Generated Caption:")
st.write(caption)
else:
st.error(f"Error: Unable to process image. Status code {response.status_code}")
if __name__ == "__main__":
main()
|