Emmanuel Frimpong Asante
commited on
Commit
·
82954e7
1
Parent(s):
cd985c3
update space
Browse files- Dockerfile +26 -0
- app.py +25 -3
Dockerfile
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# File: Dockerfile
|
2 |
+
|
3 |
+
# Use an official Python runtime as a parent image
|
4 |
+
FROM python:3.10-slim
|
5 |
+
|
6 |
+
|
7 |
+
# Create and set the working directory
|
8 |
+
WORKDIR /app
|
9 |
+
|
10 |
+
# Copy the requirements file into the container
|
11 |
+
COPY requirements.txt /app/
|
12 |
+
|
13 |
+
# Install any needed packages specified in requirements.txt
|
14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
15 |
+
|
16 |
+
# Copy the rest of the application code into the container
|
17 |
+
COPY . /app
|
18 |
+
|
19 |
+
# Install Gunicorn
|
20 |
+
RUN pip install gunicorn
|
21 |
+
|
22 |
+
# Expose the port that the app runs on
|
23 |
+
EXPOSE 5000
|
24 |
+
|
25 |
+
# Command to run the application using Gunicorn
|
26 |
+
CMD ["gunicorn", "app:app", "-b", "0.0.0.0:5000", "-w", "2", "--threads", "4", "--timeout", "120"]
|
app.py
CHANGED
@@ -13,6 +13,8 @@ from flask import Flask, render_template, request, redirect, url_for, session
|
|
13 |
from celery import Celery
|
14 |
import time
|
15 |
import threading
|
|
|
|
|
16 |
|
17 |
# Setup logging for better monitoring
|
18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -210,11 +212,31 @@ def chatbot():
|
|
210 |
chatbot_interface = build_chatbot_interface({'username': username})
|
211 |
return chatbot_interface.launch(inline=True, share=True)
|
212 |
|
213 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
if __name__ == "__main__":
|
215 |
try:
|
216 |
-
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
218 |
except Exception as e:
|
219 |
logger.error(f"Failed to launch Flask server: {e}")
|
220 |
raise RuntimeError("Could not launch the Flask server. Please check the application setup.")
|
|
|
13 |
from celery import Celery
|
14 |
import time
|
15 |
import threading
|
16 |
+
import gunicorn.app.base
|
17 |
+
from gunicorn.six import iteritems
|
18 |
|
19 |
# Setup logging for better monitoring
|
20 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
212 |
chatbot_interface = build_chatbot_interface({'username': username})
|
213 |
return chatbot_interface.launch(inline=True, share=True)
|
214 |
|
215 |
+
# Custom Gunicorn application to run Flask safely in a production environment like Hugging Face Space Docker
|
216 |
+
class StandaloneApplication(gunicorn.app.base.BaseApplication):
|
217 |
+
def __init__(self, app, options=None):
|
218 |
+
self.application = app
|
219 |
+
self.options = options or {}
|
220 |
+
super().__init__()
|
221 |
+
|
222 |
+
def load_config(self):
|
223 |
+
config = {key: value for key, value in iteritems(self.options) if key in self.cfg.settings and value is not None}
|
224 |
+
for key, value in config.items():
|
225 |
+
self.cfg.set(key.lower(), value)
|
226 |
+
|
227 |
+
def load(self):
|
228 |
+
return self.application
|
229 |
+
|
230 |
+
# Launch the Flask application using Gunicorn in a production-safe manner
|
231 |
if __name__ == "__main__":
|
232 |
try:
|
233 |
+
options = {
|
234 |
+
'bind': '%s:%s' % ('0.0.0.0', '5000'),
|
235 |
+
'workers': 2, # Set number of worker processes
|
236 |
+
'threads': 4, # Set number of threads per worker
|
237 |
+
'timeout': 120, # Set timeout for workers
|
238 |
+
}
|
239 |
+
StandaloneApplication(app, options).run()
|
240 |
except Exception as e:
|
241 |
logger.error(f"Failed to launch Flask server: {e}")
|
242 |
raise RuntimeError("Could not launch the Flask server. Please check the application setup.")
|