multimodalart HF Staff commited on
Commit
6f699a8
·
1 Parent(s): 3613c3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -45
app.py CHANGED
@@ -1,54 +1,35 @@
1
- from flask import Flask, request, g
2
- from threading import Thread
3
- from queue import Queue
4
- import requests
5
- import os
6
 
7
- app = Flask(__name__)
 
 
 
8
 
9
- # Configurable parameter for number of workers
10
- WORKERS = int(os.getenv('WORKERS', 2))
11
 
12
- API_ENDPOINT = os.getenv('API_ENDPOINT', 'https://actual-api-endpoint')
 
 
 
13
 
14
- class Worker(Thread):
15
- def __init__(self, queue):
16
- Thread.__init__(self)
17
- self.queue = queue
18
 
19
- def run(self):
20
- while True:
21
- data, callback = self.queue.get()
22
- try:
23
- response = requests.post(API_ENDPOINT, data=data)
24
- callback(response)
25
- finally:
26
- self.queue.task_done()
27
 
 
 
 
 
28
 
29
- @app.before_first_request
30
- def initialize_workers():
31
- g.queue = Queue()
32
 
33
- for _ in range(WORKERS):
34
- worker = Worker(g.queue)
35
- worker.daemon = True
36
- worker.start()
37
 
38
-
39
- @app.route('/api', methods=['POST'])
40
- def queue_api():
41
- data = request.get_json()
42
-
43
- def callback(response):
44
- return response.content
45
-
46
- g.queue.put((data, callback))
47
-
48
- g.queue.join()
49
-
50
- return callback
51
-
52
-
53
- if __name__ == '__main__':
54
- app.run()
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.8-slim-buster
 
 
 
3
 
4
+ # Set environment variables
5
+ ENV PYTHONUNBUFFERED 1
6
+ ENV API_ENDPOINT=""
7
+ ENV WORKERS=2
8
 
9
+ # Set work directory in the container
10
+ WORKDIR /app
11
 
12
+ # Install necessary packages
13
+ RUN apt-get update \
14
+ && apt-get install -y nginx \
15
+ && apt-get clean
16
 
17
+ # Copy the current directory contents into the container at /app
18
+ COPY . /app
 
 
19
 
20
+ # Install any needed packages specified in requirements.txt
21
+ RUN pip install --no-cache-dir -r requirements.txt
 
 
 
 
 
 
22
 
23
+ # Setup NGINX
24
+ RUN rm /etc/nginx/sites-enabled/default
25
+ COPY nginx.conf /etc/nginx/sites-available/
26
+ RUN ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled/
27
 
28
+ # Test Nginx config
29
+ RUN nginx -t
 
30
 
31
+ # Expose port for the app
32
+ EXPOSE 7680
 
 
33
 
34
+ # Start the application
35
+ CMD service nginx start && python app.py