Skip to content

Commit 1f9e07e

Browse files
committed
init
0 parents  commit 1f9e07e

22 files changed

+1490
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.venv/
2+
*.pyc
3+
__pycache__
4+
old.git
5+
6+
openai-api-key.txt

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[python]": {
3+
"editor.defaultFormatter": "ms-python.black-formatter"
4+
},
5+
"python.formatting.provider": "none"
6+
}

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Requirements
2+
3+
Python v3.11.3
4+
5+
# Installation
6+
7+
```bash
8+
python3.11 -m venv .venv
9+
source .venv/bin/activate
10+
pip install -r requirements.txt
11+
```
12+
13+
Then, get an API key from OpenAI and put it in a file called `openai-api-key.txt` in the root directory. You can get an API key from https://platform.openai.com/account/api-keys.
14+
15+
# Usage
16+
17+
```bash
18+
python app.py
19+
```
20+
21+
and go to a url at http://localhost:7500
22+
23+
# To test in a live repl
24+
25+
```bash
26+
python app.py --shell
27+
```
28+
29+
and go to a url at http://localhost:7500
30+
31+
# To run from a scripted test
32+
33+
```bash
34+
python app.py --prerecording=sample2.convo.txt
35+
```
36+
37+
and go to a url at http://localhost:7500
38+
39+
# Full options
40+
41+
```bash
42+
$ python app.py --help
43+
usage: app.py [-h] [--shell] [--confirm] [--prerecording FILENAME]
44+
45+
Have GPT hallucinate a web app
46+
47+
options:
48+
-h, --help show this help message and exit
49+
--shell Start an interactive repl for each request, like flask shell, where the
50+
user can live respond to a request
51+
--confirm Wait for the user to accept each GPT suggestion before proceeding
52+
--prerecording FILENAME
53+
Use a prerecorded conversation from a file
54+
```

app.py

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import flask
2+
from flask import Flask, redirect, render_template, render_template_string, request
3+
import os
4+
from flask_sqlalchemy import SQLAlchemy
5+
from flask_migrate import Migrate
6+
import os
7+
from datetime import datetime
8+
import sys
9+
import inspect
10+
import time
11+
12+
import flask_login
13+
from flask_login import UserMixin, LoginManager, login_user, logout_user, current_user
14+
15+
app = Flask(__name__)
16+
app.config["SECRET_KEY"] = "mysecret"
17+
18+
19+
basedir = os.path.abspath(os.path.dirname(__file__))
20+
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(
21+
basedir, "data.sqlite"
22+
)
23+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
24+
25+
db = SQLAlchemy(app)
26+
Migrate(app, db)
27+
28+
29+
class PrintableMixin:
30+
def __repr__(self):
31+
"""
32+
Gives detailed reprs like
33+
<Product {'description': 'A small widget', 'name': 'Widget', 'price': 10, 'category': 'Widgets', 'id': 1, 'ingredients': 'Metal, plastic'}>
34+
"""
35+
clean_dict = {k: v for (k, v) in self.__dict__.items() if not k.startswith("_")}
36+
return f"<{self.__class__.__name__} {repr(clean_dict)}>"
37+
38+
39+
class Product(PrintableMixin, db.Model):
40+
__tablename__ = "products"
41+
42+
id = db.Column(db.Integer, primary_key=True)
43+
name = db.Column(db.String, nullable=True)
44+
description = db.Column(db.String, nullable=True)
45+
category = db.Column(db.String, nullable=True)
46+
price = db.Column(db.Integer, nullable=True)
47+
ingredients = db.Column(db.String, nullable=True)
48+
49+
order_products = db.relationship("OrderProduct", back_populates="product")
50+
51+
52+
class Order(PrintableMixin, db.Model):
53+
__tablename__ = "orders"
54+
55+
id = db.Column(db.Integer, primary_key=True)
56+
status = db.Column(db.String, nullable=True)
57+
order_date = db.Column(db.String, default=datetime.utcnow, nullable=True)
58+
59+
customer_id = db.Column(
60+
db.Integer,
61+
db.ForeignKey("customers.id", name="order_customer_fk"),
62+
nullable=True,
63+
)
64+
customer = db.relationship(
65+
"Customer", foreign_keys=[customer_id], back_populates="orders"
66+
)
67+
68+
order_products = db.relationship("OrderProduct", back_populates="order")
69+
70+
71+
class OrderProduct(PrintableMixin, db.Model):
72+
id = db.Column(db.Integer, primary_key=True)
73+
quantity = db.Column(db.Integer, nullable=True)
74+
75+
order_id = db.Column(
76+
db.Integer,
77+
db.ForeignKey("orders.id", name="orderproduct_order_fk"),
78+
nullable=True,
79+
)
80+
order = db.relationship(
81+
"Order", foreign_keys=[order_id], back_populates="order_products"
82+
)
83+
84+
product_id = db.Column(
85+
db.Integer,
86+
db.ForeignKey("products.id", name="orderproduct_product_fk"),
87+
nullable=True,
88+
)
89+
product = db.relationship(
90+
"Product", foreign_keys=[product_id], back_populates="order_products"
91+
)
92+
93+
94+
class Customer(PrintableMixin, db.Model, UserMixin):
95+
__tablename__ = "customers"
96+
97+
id = db.Column(db.Integer, primary_key=True)
98+
first_name = db.Column(db.String, nullable=True)
99+
last_name = db.Column(db.String, nullable=True)
100+
email = db.Column(db.String, nullable=True)
101+
address = db.Column(db.String, nullable=True)
102+
phone = db.Column(db.String, nullable=True)
103+
104+
orders = db.relationship("Order", back_populates="customer")
105+
106+
107+
login_manager = LoginManager()
108+
login_manager.init_app(app)
109+
110+
111+
@login_manager.user_loader
112+
def load_user(user_id):
113+
return Customer.query.get_or_404(user_id)
114+
115+
116+
@app.route("/login-bob")
117+
def login_bob():
118+
bob = Customer.query.get_or_404(3)
119+
login_user(bob)
120+
return redirect("/whoami")
121+
122+
123+
@app.route("/whoami")
124+
def whoami():
125+
if current_user.is_authenticated:
126+
return f"Current user is {current_user.first_name} {current_user.last_name}"
127+
else:
128+
return "No user is logged in"
129+
130+
131+
@app.route("/logout")
132+
def logout():
133+
logout_user()
134+
return redirect("/whoami")
135+
136+
137+
# prevent browser autofetched favicon request from going to GPT
138+
@app.route("/favicon.ico")
139+
def favicon():
140+
return ("No favicon", 404)
141+
142+
143+
@app.route("/test-frame")
144+
def test_frame():
145+
return render_template("frame-test.html")
146+
147+
148+
###
149+
150+
from gpt_http import gpt_hallucinate
151+
152+
gpt_hallucinate(app, get_gbls=lambda: globals())
153+
154+
if __name__ == "__main__":
155+
app.run(debug=True, host="0.0.0.0", port=7500)

data.sqlite

28 KB
Binary file not shown.

example1.readme.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example 1 readme
2+
3+
Urls to try:
4+
5+
/
6+
/cart
7+
/products
8+
/products/4
9+
/analytics
10+
/about
11+
/admin/orders

0 commit comments

Comments
 (0)