Spaces:
Build error
Build error
| import streamlit as st | |
| import requests | |
| import json # Add this import to parse JSON responses | |
| def extract_text_from_image(uploaded_file): | |
| """Upload image to the text extraction API and return extracted text.""" | |
| try: | |
| # Reset file pointer to the beginning | |
| uploaded_file.seek(0) | |
| # Set the correct filename and MIME type | |
| file_name = uploaded_file.name if uploaded_file.name else "uploaded_image.png" | |
| mime_type = uploaded_file.type if uploaded_file.type else "image/png" | |
| # Prepare the file tuple (filename, file object, MIME type) | |
| files = {'image': (file_name, uploaded_file, mime_type)} | |
| # Make the API call | |
| response = requests.post('https://api-1-zvvu.onrender.com/upload', files=files) | |
| if response.status_code == 200: | |
| # Parse and return the response | |
| response_data = response.json() | |
| return response_data.get("text", "No text extracted.") | |
| else: | |
| return f"Error: {response.status_code} - {response.text}" | |
| except requests.RequestException as e: | |
| return f"Request failed: {e}" | |
| def main(): | |
| st.title('Image Text Extraction') | |
| # Camera input toggle | |
| enable = st.checkbox("Enable camera") | |
| # Camera input | |
| picture = st.camera_input("Take a picture", disabled=not enable) | |
| # File uploader | |
| uploaded_file = st.file_uploader("Or upload an image...", type=['png', 'jpg', 'jpeg']) | |
| # Determine which image to use | |
| image_to_process = picture or uploaded_file | |
| if image_to_process: | |
| # Display the image | |
| st.image(image_to_process, caption='Uploaded/Captured Image', use_column_width=True) | |
| # Extract text button | |
| if st.button('Extract Text'): | |
| # Show loading spinner | |
| with st.spinner('Extracting text...'): | |
| # Call the text extraction function | |
| extracted_text = extract_text_from_image(image_to_process) | |
| # Display the extracted text | |
| st.subheader('Extracted Text:') | |
| st.text_area("", value=extracted_text, height=300) | |
| if __name__ == '__main__': | |
| main() |