Spaces:
Running
Running
| import os | |
| filepath = r"c:\superai new\backend\main.py" | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # 1. Ensure urllib.request is imported | |
| if "import urllib.request" not in content: | |
| content = content.replace("from urllib.parse import quote", "from urllib.parse import quote\nimport urllib.request") | |
| # 2. Update get_image_base64 to use urllib as ultimate fallback | |
| new_helper = """ # BULLETPROOF Base64 fetcher with Ultimate urllib Fallback | |
| async def get_image_base64(url: str): | |
| global http_client | |
| # Attempt 1: Global Async Client | |
| try: | |
| if http_client: | |
| resp = await http_client.get(url, timeout=15.0) | |
| if resp.status_code == 200: | |
| return f"data:image/jpeg;base64,{base64.b64encode(resp.content).decode('utf-8')}" | |
| except Exception as e: | |
| print(f"โ ๏ธ Async Fetch Failed: {e}") | |
| # Attempt 2: Ultimate urllib Fallback (Standard Library, very stable) | |
| try: | |
| print(f"๐ Using urllib ultimate fallback for {url}...") | |
| req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) | |
| with urllib.request.urlopen(req, timeout=15) as response: | |
| if response.status == 200: | |
| data = response.read() | |
| return f"data:image/jpeg;base64,{base64.b64encode(data).decode('utf-8')}" | |
| except Exception as e: | |
| print(f"๐ฅ Ultimate urllib Failed: {e}") | |
| return url | |
| """ | |
| # Replace the existing get_image_base64 block | |
| import re | |
| content = re.sub(r'async def get_image_base64\(url: str\):.*?return url\n', new_helper, content, flags=re.DOTALL) | |
| with open(filepath, 'w', encoding='utf-8') as f: | |
| f.write(content) | |
| print("SUCCESS: main.py updated with urllib fallback.") | |