Add HW 5
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
FROM python:3.12-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /server
|
||||
COPY requirements.txt .
|
||||
RUN --mount=type=cache,id=distsys-course-pip,target=/root/.cache/pip,sharing=locked \
|
||||
pip install -r requirements.txt
|
||||
COPY . .
|
||||
|
||||
CMD ["python3", "-u", "server.py"]
|
||||
@@ -0,0 +1,2 @@
|
||||
flask==3.1.2
|
||||
pika==1.3.2
|
||||
@@ -0,0 +1,51 @@
|
||||
from flask import Flask, request
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
class Server:
|
||||
# TODO: Implement API server
|
||||
def __init__(self, mq_host, mq_port, data_dir):
|
||||
pass
|
||||
|
||||
def add_image(self, image_url: str) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
def get_processed_images(self) -> List[str]:
|
||||
raise NotImplementedError
|
||||
|
||||
def get_image_caption(self, image_id: str) -> Optional[str]:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
app = Flask(__name__)
|
||||
|
||||
server = Server('rabbitmq', 5672, '/data')
|
||||
|
||||
@app.route('/api/v1.0/images', methods=['POST'])
|
||||
def add_image():
|
||||
body = request.get_json(force=True)
|
||||
if not isinstance(body, dict) or not isinstance(body.get('image_url'), str) or not body['image_url']:
|
||||
return 'image_url must be a nonempty string', 400
|
||||
image_id = server.add_image(body['image_url'])
|
||||
return {"image_id": image_id}
|
||||
|
||||
@app.route('/api/v1.0/images', methods=['GET'])
|
||||
def get_processed_images():
|
||||
image_ids = server.get_processed_images()
|
||||
return {"image_ids": image_ids}
|
||||
|
||||
@app.route('/api/v1.0/images/<string:image_id>', methods=['GET'])
|
||||
def get_image_caption(image_id):
|
||||
result = server.get_image_caption(image_id)
|
||||
if result is None:
|
||||
return "Image not found.", 404
|
||||
else:
|
||||
return {'caption': result}
|
||||
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = create_app()
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
@@ -0,0 +1,13 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
FROM python:3.12-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /worker
|
||||
COPY requirements.txt .
|
||||
RUN --mount=type=cache,id=distsys-course-pip,target=/root/.cache/pip,sharing=locked \
|
||||
pip install -r requirements.txt
|
||||
COPY . .
|
||||
|
||||
CMD ["python3", "-u", "worker.py"]
|
||||
@@ -0,0 +1 @@
|
||||
pika==1.3.2
|
||||
@@ -0,0 +1,15 @@
|
||||
class Worker:
|
||||
# TODO: Implement server
|
||||
def __init__(self, mq_host, mq_port, data_dir):
|
||||
pass
|
||||
|
||||
def produce_image_caption(self, image_url):
|
||||
return str(abs(hash(image_url)) % (10 ** 8))
|
||||
|
||||
def run(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
worker = Worker('rabbitmq', 5672, '/data')
|
||||
worker.run()
|
||||
Reference in New Issue
Block a user