Spaces:
Running
Running
| """ | |
| Simple Flask server for HuggingFace Spaces deployment. | |
| Serves static files and provides a CORS proxy for ArXiv API requests. | |
| """ | |
| import os | |
| import requests | |
| from flask import Flask, send_from_directory, request, Response | |
| from flask_cors import CORS | |
| app = Flask(__name__, static_folder='static') | |
| CORS(app) | |
| # ==================== CORS Proxy Endpoints ==================== | |
| def proxy_arxiv(): | |
| """Proxy ArXiv API requests to avoid CORS issues.""" | |
| params = request.args.to_dict() | |
| try: | |
| resp = requests.get( | |
| 'https://export.arxiv.org/api/query', | |
| params=params, | |
| timeout=30, | |
| headers={'User-Agent': 'ArXiv-Research-Explorer/1.0'} | |
| ) | |
| return Response( | |
| resp.content, | |
| status=resp.status_code, | |
| content_type=resp.headers.get('Content-Type', 'application/xml') | |
| ) | |
| except Exception as e: | |
| return Response(f'Proxy error: {str(e)}', status=502) | |
| def proxy_ar5iv(arxiv_id): | |
| """Proxy ar5iv HTML requests to avoid CORS issues.""" | |
| try: | |
| url = f'https://ar5iv.labs.arxiv.org/html/{arxiv_id}' | |
| resp = requests.get( | |
| url, | |
| timeout=60, | |
| headers={'User-Agent': 'ArXiv-Research-Explorer/1.0'} | |
| ) | |
| return Response( | |
| resp.content, | |
| status=resp.status_code, | |
| content_type='text/html; charset=utf-8' | |
| ) | |
| except Exception as e: | |
| return Response(f'Proxy error: {str(e)}', status=502) | |
| # ==================== Static File Serving ==================== | |
| def serve_index(): | |
| return send_from_directory('static', 'index.html') | |
| def serve_static(path): | |
| """Serve static files, fallback to index.html for SPA routing.""" | |
| file_path = os.path.join('static', path) | |
| if os.path.isfile(file_path): | |
| return send_from_directory('static', path) | |
| return send_from_directory('static', 'index.html') | |
| if __name__ == '__main__': | |
| port = int(os.environ.get('PORT', 7860)) | |
| print(f"π ArXiv Research Explorer server starting on port {port}") | |
| print(f"π Serving static files from: {os.path.abspath('static')}") | |
| app.run(host='0.0.0.0', port=port, debug=False) | |