From d2f43fef97b6b46c93aef0972e8a95aa9ca46601 Mon Sep 17 00:00:00 2001 From: David Cristofaro Date: Sun, 16 Apr 2023 20:12:10 +1000 Subject: [PATCH] Initial commit --- .dockerignore | 39 +++++++++++++++++++++++++++ .gitignore | 15 +++++++++++ .tool-versions | 1 + Dockerfile | 18 +++++++++++++ Gemfile | 4 +++ Gemfile.lock | 17 ++++++++++++ LICENSE | 19 +++++++++++++ README.md | 1 + config.ru | 67 ++++++++++++++++++++++++++++++++++++++++++++++ fly.toml | 40 +++++++++++++++++++++++++++ public/favicon.ico | 0 11 files changed, 221 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 .tool-versions create mode 100644 Dockerfile create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 LICENSE create mode 100644 README.md create mode 100644 config.ru create mode 100644 fly.toml create mode 100644 public/favicon.ico diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9dc7e6a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,39 @@ +# See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files. + +# Ignore git directory. +/.git/ + +# Ignore bundler config. +/.bundle + +# Ignore all default key files. +/config/master.key +/config/credentials/*.key + +# Ignore all environment files. +/.env* +!/.env.example + +# Ignore all logfiles and tempfiles. +/log/* +/tmp/* +!/log/.keep +!/tmp/.keep + +# Ignore pidfiles, but keep the directory. +/tmp/pids/* +!/tmp/pids/ +!/tmp/pids/.keep + +# Ignore storage (uploaded files in development and any SQLite databases). +/storage/* +!/storage/.keep +/tmp/storage/* +!/tmp/storage/ +!/tmp/storage/.keep + +# Ignore assets. +/node_modules/ +/app/assets/builds/* +!/app/assets/builds/.keep +/public/assets diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4da16e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.tags + +/.bundle/ +/.yardoc +/gems.locked +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +.rspec_status +.covered.db +/h2spec diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..f2a971a --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.2.2 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2aa0bfb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# syntax = docker/dockerfile:1 + +FROM ruby:3.2-alpine + +WORKDIR /app + +RUN apk add build-base + +# Install application gems +COPY Gemfile Gemfile.lock ./ +RUN bundle install + +# Copy application code +COPY . . + +# Start the server by default, this can be overwritten at runtime +EXPOSE 9292 +CMD ["puma"] diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..49d3afd --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'puma' +gem 'rack' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..bf05d5b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,17 @@ +GEM + remote: https://rubygems.org/ + specs: + nio4r (2.5.9) + puma (6.2.1) + nio4r (~> 2.0) + rack (3.0.7) + +PLATFORMS + arm64-darwin-22 + +DEPENDENCIES + puma + rack + +BUNDLED WITH + 2.4.12 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9cf1062 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e81512 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# gpt-html diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..7031d8d --- /dev/null +++ b/config.ru @@ -0,0 +1,67 @@ +require 'json' +require 'net/http' + +use Rack::Static, :root => 'public' + +app = lambda do |env| + request_uri = env['REQUEST_URI'] + + if request_uri == '/health' + return [200, { 'content-type' => 'text/plain' }, [Time.now.to_i.to_s]] + end + + body = lambda do |stream| + puts "\nFetching: #{request_uri}" + puts "---------\n\n" + + uri = URI('https://api.openai.com/v1/chat/completions') + + request = Net::HTTP::Post.new(uri) + request['content-type'] = 'application/json' + request['authorization'] = "Bearer #{ENV['OPENAI_API_KEY']}" + request.body = JSON.dump({ + model: 'gpt-3.5-turbo', + stream: true, + messages: [ + { + role: 'system', + content: <<~HEREDOC, + Output a valid HTML document for the webpage that could be located at the URL path provided by the user. Include general navigation anchor tags as well as relative anchor tags to other related pages. Include a minimal amount of inline styles to improve the look of the page. Make the text content quite long with a decent amount of interesting content. Do not use any dummy text on the page. + + Start the reponse with the following exact characters: + + + + HEREDOC + }, + { role: 'user', content: request_uri }, + ], + }) + + Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| + http.request(request) do |response| + response.read_body do |chunk| + chunk.sub(/\Adata: /, '').sub(/\n\n\z/, '').split("\n\ndata: ").each do |data| + break if data == '[DONE]' + + content = JSON.parse(data).dig('choices')&.first.dig('delta', 'content') + + next if content.nil? + + stream.write(content) + + # Debug + print(content) + end + end + end + end + ensure + stream.close + puts + end + + [200, { 'content-type' => 'text/html' }, body] +end + +run app diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..88f46fd --- /dev/null +++ b/fly.toml @@ -0,0 +1,40 @@ +# fly.toml file generated for gpt-html-8d2f on 2023-04-17T22:40:36+10:00 + +app = "gpt-html-8d2f" +kill_signal = "SIGINT" +kill_timeout = 5 +primary_region = "syd" +processes = [] + +[build] + +[env] + +[experimental] + auto_rollback = true + +[[services]] + http_checks = [] + internal_port = 9292 + processes = ["app"] + protocol = "tcp" + script_checks = [] + [services.concurrency] + hard_limit = 25 + soft_limit = 20 + type = "connections" + + [[services.ports]] + force_https = true + handlers = ["http"] + port = 80 + + [[services.ports]] + handlers = ["tls", "http"] + port = 443 + + [[services.tcp_checks]] + grace_period = "1s" + interval = "15s" + restart_limit = 0 + timeout = "2s" diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000..e69de29