File size: 2,348 Bytes
81cb6e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
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 ====================

@app.route('/api/arxiv', methods=['GET'])
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)


@app.route('/api/ar5iv/<path:arxiv_id>', methods=['GET'])
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 ====================

@app.route('/')
def serve_index():
    return send_from_directory('static', 'index.html')


@app.route('/<path:path>')
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)