Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,6 +47,61 @@ if "debug" not in st.session_state:
|
|
| 47 |
with st.sidebar:
|
| 48 |
st.session_state.debug = st.toggle("Enable Debug Mode", value=debug_mode)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def log_debug(message):
|
| 51 |
"""Helper function to log debug messages only when debug mode is enabled"""
|
| 52 |
if st.session_state.debug:
|
|
@@ -706,21 +761,82 @@ def main():
|
|
| 706 |
if url and url.strip():
|
| 707 |
try:
|
| 708 |
import requests
|
| 709 |
-
# Simplified URL handling
|
| 710 |
headers = {
|
| 711 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
|
|
|
|
|
| 712 |
}
|
| 713 |
-
response = requests.get(url, stream=True, headers=headers, timeout=10)
|
| 714 |
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 722 |
except Exception as e:
|
| 723 |
-
st.error(f"Error
|
|
|
|
|
|
|
|
|
|
| 724 |
|
| 725 |
# If we have an uploaded image, process it
|
| 726 |
if uploaded_image is not None:
|
|
|
|
| 47 |
with st.sidebar:
|
| 48 |
st.session_state.debug = st.toggle("Enable Debug Mode", value=debug_mode)
|
| 49 |
|
| 50 |
+
# Add after existing debug mode toggle in sidebar
|
| 51 |
+
with st.sidebar:
|
| 52 |
+
if st.session_state.debug:
|
| 53 |
+
st.write("### Connection Diagnostics")
|
| 54 |
+
if st.button("Test File Upload Connection"):
|
| 55 |
+
try:
|
| 56 |
+
# Create a simple test file in memory
|
| 57 |
+
import io
|
| 58 |
+
test_file = io.BytesIO(b"test content")
|
| 59 |
+
test_file.name = "test.txt"
|
| 60 |
+
|
| 61 |
+
# Test the Streamlit file uploader connection
|
| 62 |
+
st.write("Checking file upload capability...")
|
| 63 |
+
st.write("Status: Testing... If this freezes, there may be connectivity issues.")
|
| 64 |
+
|
| 65 |
+
# Check basic file operations
|
| 66 |
+
test_path = "test_upload_capability.txt"
|
| 67 |
+
try:
|
| 68 |
+
with open(test_path, "w") as f:
|
| 69 |
+
f.write("test")
|
| 70 |
+
st.write("✅ File write test: Success")
|
| 71 |
+
import os
|
| 72 |
+
os.remove(test_path)
|
| 73 |
+
st.write("✅ File delete test: Success")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
st.write(f"❌ File operation test: Failed - {str(e)}")
|
| 76 |
+
|
| 77 |
+
# Check Streamlit session state
|
| 78 |
+
try:
|
| 79 |
+
st.session_state.test_value = "test"
|
| 80 |
+
if st.session_state.test_value == "test":
|
| 81 |
+
st.write("✅ Session state test: Success")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
st.write(f"❌ Session state test: Failed - {str(e)}")
|
| 84 |
+
|
| 85 |
+
# Environment variables check
|
| 86 |
+
import os
|
| 87 |
+
st.write("### Environment Variables")
|
| 88 |
+
for key in ["STREAMLIT_SERVER_ENABLE_CORS", "STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION",
|
| 89 |
+
"TEMP", "TMP", "TMPDIR"]:
|
| 90 |
+
st.write(f"{key}: {os.environ.get(key, 'Not set')}")
|
| 91 |
+
|
| 92 |
+
# Check for specific Hugging Face Spaces environment variables
|
| 93 |
+
hf_vars = [k for k in os.environ if k.startswith("HF_")]
|
| 94 |
+
if hf_vars:
|
| 95 |
+
st.write("### Hugging Face Environment Variables")
|
| 96 |
+
for key in hf_vars:
|
| 97 |
+
st.write(f"{key}: {os.environ.get(key, 'Not set')}")
|
| 98 |
+
|
| 99 |
+
st.success("Diagnostics completed!")
|
| 100 |
+
except Exception as e:
|
| 101 |
+
st.error(f"Diagnostics error: {str(e)}")
|
| 102 |
+
import traceback
|
| 103 |
+
st.error(traceback.format_exc())
|
| 104 |
+
|
| 105 |
def log_debug(message):
|
| 106 |
"""Helper function to log debug messages only when debug mode is enabled"""
|
| 107 |
if st.session_state.debug:
|
|
|
|
| 761 |
if url and url.strip():
|
| 762 |
try:
|
| 763 |
import requests
|
| 764 |
+
# Simplified URL handling with more robust approach
|
| 765 |
headers = {
|
| 766 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 767 |
+
'Accept': 'image/jpeg, image/png, image/*, */*',
|
| 768 |
+
'Referer': 'https://huggingface.co/'
|
| 769 |
}
|
|
|
|
| 770 |
|
| 771 |
+
# Try three different methods to handle various API restrictions
|
| 772 |
+
try_methods = True
|
| 773 |
+
|
| 774 |
+
# Method 1: Direct requests
|
| 775 |
+
if try_methods:
|
| 776 |
+
try:
|
| 777 |
+
response = requests.get(url, stream=True, headers=headers, timeout=10)
|
| 778 |
+
if response.status_code == 200 and 'image' in response.headers.get('Content-Type', ''):
|
| 779 |
+
try:
|
| 780 |
+
image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 781 |
+
uploaded_image = image
|
| 782 |
+
st.session_state.upload_method = "url_direct"
|
| 783 |
+
try_methods = False
|
| 784 |
+
st.success("✅ Image loaded via direct request")
|
| 785 |
+
except Exception as e:
|
| 786 |
+
st.warning(f"Direct method received data but couldn't process as image: {str(e)}")
|
| 787 |
+
else:
|
| 788 |
+
st.info(f"Direct method failed: Status {response.status_code}, trying alternative method...")
|
| 789 |
+
except Exception as e:
|
| 790 |
+
st.info(f"Direct method error: {str(e)}, trying alternative method...")
|
| 791 |
+
|
| 792 |
+
# Method 2: Use Python's urllib as fallback
|
| 793 |
+
if try_methods:
|
| 794 |
+
try:
|
| 795 |
+
import urllib.request
|
| 796 |
+
from urllib.error import HTTPError
|
| 797 |
+
|
| 798 |
+
opener = urllib.request.build_opener()
|
| 799 |
+
opener.addheaders = [('User-agent', headers['User-Agent'])]
|
| 800 |
+
urllib.request.install_opener(opener)
|
| 801 |
+
|
| 802 |
+
with urllib.request.urlopen(url, timeout=10) as response:
|
| 803 |
+
image_data = response.read()
|
| 804 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 805 |
+
uploaded_image = image
|
| 806 |
+
st.session_state.upload_method = "url_urllib"
|
| 807 |
+
try_methods = False
|
| 808 |
+
st.success("✅ Image loaded via urllib")
|
| 809 |
+
except HTTPError as e:
|
| 810 |
+
st.info(f"urllib method failed: HTTP error {e.code}, trying next method...")
|
| 811 |
+
except Exception as e:
|
| 812 |
+
st.info(f"urllib method error: {str(e)}, trying next method...")
|
| 813 |
+
|
| 814 |
+
# Method 3: Use a proxy service as last resort
|
| 815 |
+
if try_methods:
|
| 816 |
+
try:
|
| 817 |
+
# This uses an image proxy service to bypass CORS issues
|
| 818 |
+
# Only as last resort since it depends on external service
|
| 819 |
+
proxy_url = f"https://images.weserv.nl/?url={url}"
|
| 820 |
+
response = requests.get(proxy_url, stream=True, timeout=10)
|
| 821 |
+
if response.status_code == 200:
|
| 822 |
+
image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
| 823 |
+
uploaded_image = image
|
| 824 |
+
st.session_state.upload_method = "url_proxy"
|
| 825 |
+
try_methods = False
|
| 826 |
+
st.success("✅ Image loaded via proxy service")
|
| 827 |
+
else:
|
| 828 |
+
st.error(f"All methods failed to load the image from URL. Last status: {response.status_code}")
|
| 829 |
+
except Exception as e:
|
| 830 |
+
st.error(f"All methods failed. Final error: {str(e)}")
|
| 831 |
+
|
| 832 |
+
if not uploaded_image:
|
| 833 |
+
st.error("Failed to load image using all available methods.")
|
| 834 |
+
|
| 835 |
except Exception as e:
|
| 836 |
+
st.error(f"Error processing URL: {str(e)}")
|
| 837 |
+
if st.session_state.debug:
|
| 838 |
+
import traceback
|
| 839 |
+
st.error(traceback.format_exc())
|
| 840 |
|
| 841 |
# If we have an uploaded image, process it
|
| 842 |
if uploaded_image is not None:
|