Download app.py from Ashrafb/Imc: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/Ashrafb/Imc/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ashrafb/Imc/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ashrafb/Imc/resolve/main/app.py
1.28 kB
| 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() | |