replicate / cog

Containers for machine learning
https://cog.run
Apache License 2.0
7.45k stars 512 forks source link

document how upload-url works #1374

Open anotherjesse opened 7 months ago

anotherjesse commented 7 months ago

cog still supports upload files outside of replicate :yay:

  1. start cog server with flag --upload-url=http://host/:port/basepath
  2. run http server listening on host:port
    • PUT /path/... should write file to disk
    • GET /path/... should return the file
  3. when you make a prediction you need to include output_file_prefix
    • this needs to match http://host/:port/basepath and then include any additional information to uniqueify
    • example: http://host/:port/basepath/{uuid}- where the client has to decide the uuid
anotherjesse commented 7 months ago

A simple http server I wrote to test this

from http.server import BaseHTTPRequestHandler, HTTPServer
import os

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def _file_path(self):

        path = os.getcwd() + self.path
        print(path)
        return path

    def do_GET(self):
        file_path = self._file_path()
        if not os.path.exists(file_path):
            self.send_response(404)
            self.end_headers()
            return

        with open(file_path, 'rb') as file:
            self.send_response(200)
            self.send_header('Content-type', 'text/plain')
            self.end_headers()
            self.wfile.write(file.read())

    def do_PUT(self):
        file_path = self._file_path()
        file_length = int(self.headers['Content-Length'])
        with open(file_path, 'wb') as file:
            file.write(self.rfile.read(file_length))
        self.send_response(200)
        self.end_headers()

def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):
    server_address = ('', 4321)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

if __name__ == '__main__':
    run()
bxclib2 commented 3 months ago

I checked the code, when using async prediction, output_file_prefix is ignored. They didn't even use it. I want to add a uuid in the path, and I don't know how to do it...

SaschaOwl commented 1 month ago

@anotherjesse Thank you for the documentation!

How can you start cog with the --upload-url flag? When I try to add it, I always get unknown flag: --upload-url.

I use docker to run it, just like it is documented in getting-started

platform-kit commented 1 week ago

Hi there. Am I to infer from this thread that replicate's previously default upload behavior has changed?

Asking because of this issue: https://github.com/replicate/cog/issues/1759

mattt commented 1 week ago

@platform-kit In the case of #1759, I think that's a problem with the model itself rather than a change in upload behavior.