| 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: |
| |
| image_data = uploaded_file.read() |
|
|
| |
| st.image(image_data, caption="Uploaded Image.", use_column_width=True) |
|
|
| |
| image_base64 = base64.b64encode(image_data).decode('utf-8') |
|
|
| |
| payload = {"data": ["data:image/jpeg;base64," + image_base64]} |
|
|
| |
| response = requests.post("https://ashrafb-salesforce-blip-image-captioning-base2.hf.space/run/predict", json=payload) |
|
|
| |
| 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() |
|
|