Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcristo committed Apr 17, 2023
0 parents commit d2f43fe
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.tags

/.bundle/
/.yardoc
/gems.locked
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

.rspec_status
.covered.db
/h2spec
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.2.2
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem 'puma'
gem 'rack'
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gpt-html
67 changes: 67 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -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:
<!doctype html>
<html>
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
40 changes: 40 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -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"
Empty file added public/favicon.ico
Empty file.

0 comments on commit d2f43fe

Please sign in to comment.