PhyllisPeh commited on
Commit
ef62dae
·
1 Parent(s): d34c8c7

fixed initialisation error

Browse files
Files changed (6) hide show
  1. .hugginface-space +4 -0
  2. Dockerfile +7 -2
  3. app.py +38 -16
  4. entrypoint.sh +11 -0
  5. runtime.yaml +5 -2
  6. wsgi.py +7 -1
.hugginface-space ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # Hugging Face Space configuration
2
+ runtime: docker
3
+ sdk: docker
4
+ entrypoint: ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "--log-level", "info", "app:app"]
Dockerfile CHANGED
@@ -24,6 +24,11 @@ EXPOSE 7860
24
  ENV FLASK_ENV=production
25
  ENV FLASK_APP=app.py
26
  ENV PYTHONUNBUFFERED=1
 
27
 
28
- # Run gunicorn with optimized settings for HuggingFace
29
- CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "--log-level", "info", "app:app"]
 
 
 
 
 
24
  ENV FLASK_ENV=production
25
  ENV FLASK_APP=app.py
26
  ENV PYTHONUNBUFFERED=1
27
+ ENV PORT=7860
28
 
29
+ # Copy and set up entrypoint
30
+ COPY entrypoint.sh /entrypoint.sh
31
+ RUN chmod +x /entrypoint.sh
32
+
33
+ # Set the entrypoint
34
+ ENTRYPOINT ["/entrypoint.sh"]
app.py CHANGED
@@ -37,9 +37,20 @@ static_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
37
  os.makedirs(template_dir, exist_ok=True)
38
  os.makedirs(static_dir, exist_ok=True)
39
 
40
- app = Flask(__name__,
41
- template_folder=template_dir,
42
- static_folder=static_dir)
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Configure for production
45
  app.config['ENV'] = 'production'
@@ -51,17 +62,37 @@ progress_queue = queue.Queue()
51
  # Get API keys from environment variables with defaults for demo
52
  SERPAPI_API_KEY = os.getenv('SERPAPI_API_KEY', 'default')
53
  OPENAI_API_KEY = os.getenv('OPENAI_API_KEY', 'default')
54
- MAX_PATENTS = 3000 # Increased from 2000 to 5000 for better coverage
55
- MIN_PATENTS_FOR_GAPS = 3000 # Minimum patents needed for reliable gap detection
56
  CACHE_FILE = os.path.join(os.path.dirname(__file__), 'patent_embeddings_cache.pkl')
57
 
58
  # Initialize OpenAI API key
59
  openai.api_key = OPENAI_API_KEY
60
 
 
 
 
 
 
 
 
 
 
61
  @app.route('/health')
62
  def health_check():
63
- """Health check endpoint"""
64
- return jsonify({"status": "healthy"}), 200
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  @app.errorhandler(404)
67
  def not_found_error(error):
@@ -71,15 +102,6 @@ def not_found_error(error):
71
  def internal_error(error):
72
  return jsonify({"error": "Internal server error"}), 500
73
 
74
- @app.route('/')
75
- def index():
76
- """Main page route"""
77
- try:
78
- return render_template('index.html')
79
- except Exception as e:
80
- app.logger.error(f"Error rendering template: {str(e)}")
81
- return jsonify({"error": "Template error", "details": str(e)}), 500
82
-
83
  # Helper functions
84
  def load_cache():
85
  """Load cached embeddings from file"""
 
37
  os.makedirs(template_dir, exist_ok=True)
38
  os.makedirs(static_dir, exist_ok=True)
39
 
40
+ def create_app():
41
+ """Create and configure the Flask application"""
42
+ app = Flask(__name__,
43
+ template_folder=template_dir,
44
+ static_folder=static_dir)
45
+
46
+ # Configure for production
47
+ app.config['ENV'] = 'production'
48
+ app.config['DEBUG'] = False
49
+ app.config['PROPAGATE_EXCEPTIONS'] = True
50
+
51
+ return app
52
+
53
+ app = create_app()
54
 
55
  # Configure for production
56
  app.config['ENV'] = 'production'
 
62
  # Get API keys from environment variables with defaults for demo
63
  SERPAPI_API_KEY = os.getenv('SERPAPI_API_KEY', 'default')
64
  OPENAI_API_KEY = os.getenv('OPENAI_API_KEY', 'default')
65
+ MAX_PATENTS = 3000
66
+ MIN_PATENTS_FOR_GAPS = 3000
67
  CACHE_FILE = os.path.join(os.path.dirname(__file__), 'patent_embeddings_cache.pkl')
68
 
69
  # Initialize OpenAI API key
70
  openai.api_key = OPENAI_API_KEY
71
 
72
+ @app.route('/')
73
+ def index():
74
+ """Main page route"""
75
+ try:
76
+ return render_template('index.html')
77
+ except Exception as e:
78
+ app.logger.error(f"Error rendering template: {str(e)}")
79
+ return jsonify({"error": "Template error", "details": str(e)}), 500
80
+
81
  @app.route('/health')
82
  def health_check():
83
+ """Health check endpoint for HuggingFace Space"""
84
+ return jsonify({
85
+ "status": "healthy",
86
+ "timestamp": datetime.now().isoformat()
87
+ }), 200
88
+
89
+ @app.route('/test')
90
+ def test():
91
+ """Simple test endpoint"""
92
+ return jsonify({
93
+ "status": "ok",
94
+ "message": "Patent Explorer API is running"
95
+ })
96
 
97
  @app.errorhandler(404)
98
  def not_found_error(error):
 
102
  def internal_error(error):
103
  return jsonify({"error": "Internal server error"}), 500
104
 
 
 
 
 
 
 
 
 
 
105
  # Helper functions
106
  def load_cache():
107
  """Load cached embeddings from file"""
entrypoint.sh ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # entrypoint.sh
3
+
4
+ # Make sure the script fails on any error
5
+ set -e
6
+
7
+ # Create necessary directories
8
+ mkdir -p templates static
9
+
10
+ # Start the application
11
+ exec gunicorn --bind 0.0.0.0:7860 --workers 1 --threads 8 --timeout 0 --log-level info app:app
runtime.yaml CHANGED
@@ -1,3 +1,6 @@
1
- # This file configures the HuggingFace runtime environment
2
  build_command: docker build -t patent-explorer .
3
- predict: python app.py
 
 
 
 
 
 
1
  build_command: docker build -t patent-explorer .
2
+ runtime: docker
3
+ sdk: docker
4
+ app_port: 7860
5
+ healthcheck_url: /health
6
+ predict: gunicorn app:app --bind 0.0.0.0:7860 --workers 1 --threads 8 --timeout 0
wsgi.py CHANGED
@@ -1,5 +1,11 @@
 
1
  from app import app
2
 
 
 
 
 
3
  if __name__ == "__main__":
4
  # Run the application
5
- app.run(host='0.0.0.0', port=7860)
 
 
1
+ import os
2
  from app import app
3
 
4
+ def create_app():
5
+ """Create and configure the Flask application"""
6
+ return app
7
+
8
  if __name__ == "__main__":
9
  # Run the application
10
+ port = int(os.environ.get("PORT", 7860))
11
+ app.run(host="0.0.0.0", port=port, debug=False)