Okay, this branch has become a mess. Opening a new branch with a semi-working celery setup and then will add in my routes and html pages. This branch has reached it's deadend

This commit is contained in:
Normanras
2023-12-07 17:22:21 -05:00
parent 297ff41900
commit adba1147ca
61 changed files with 356 additions and 89 deletions

View File

@ -1 +1 @@
FLASK_APP=apicalls.py
FLASK_APP=run.py

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -2,13 +2,19 @@ from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
from .utils import make_celery
app = Flask(__name__)
# app.config.from_object(Config)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///words_prompts.db"
app.config["CELERY_CONFIG"] = {"broker_url": "redis://localhost"}
db = SQLAlchemy(app)
celery = make_celery(app)
celery.set_default()
Base = declarative_base()
app = Flask(__name__)
app.config.from_object(Config)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///words_prompts.db"
db = SQLAlchemy(app)
class AllWords(Base, db.Model):
word = db.Column(db.String, primary_key=True)
@ -16,6 +22,7 @@ class AllWords(Base, db.Model):
def __init__(self, word):
self.word = word
class Themes(Base, db.Model):
themes = db.Column(db.String, primary_key=True)

Binary file not shown.

View File

@ -0,0 +1,83 @@
from celery import shared_task
from redbeat import RedBeatSchedulerEntry
from celery import current_app as celery_app
from sqlalchemy.sql.expression import func, select, insert
from sqlalchemy.ext.automap import automap_base
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from gpt4all import GPT4All
engine = create_engine("sqlite:///words_prompts.db", pool_pre_ping=True)
Base = automap_base()
Base.prepare(engine, reflect=True)
Words = Base.classes.words
Themes = Base.classes.themes
MODEL = GPT4All(
model_name="gpt4all-falcon-q4_0.gguf",
# model_path=(Path.home() / ".cache" / "gpt4all"),
allow_download=False,
)
@shared_task
def grab_word():
while True:
with Session(engine) as word_session:
random_word = word_session.query(Words.words)
random_word = random_word.order_by(func.random()).first()
random_word = str(random_word)[4:-4]
# SYSTEM_TEMPLATE = "A single sentence based on a word."
# PROMPT_TEMPLATE = "### Instruction: {0} \n### Response: "
response = MODEL.generate(
f"Give me a writing prompt about {random_word}.",
temp=0.7,
callback=stop_on_token_callback,
)
word_session.execute(insert(Themes).values(themes=response))
word_session.commit()
def stop_on_token_callback(token_id, token_string):
"""
Function to limit return length of the
gpt4all response. Period indicates a sentence.
"""
if "." in token_string:
return False
return True
# def every(__seconds: float, func, *args, **kwargs):
# while True:
# func(*args, **kwargs)
# await asyncio.sleep(__seconds)
#
#
# def main():
# asyncio.ensure_future(grab_word())
# with app.app_context():
# loop = asyncio.get_event_loop()
# loop.run_until_complete(grab_word())
# loop.run_forever()
# async def get_theme(random_word):
# print("Get Theme function running.")
# # SYSTEM_TEMPLATE = "A single sentence based on a word."
# # PROMPT_TEMPLATE = "### Instruction: {0} \n### Response: "
# random_word = await random_word
# print(f"Theme Func, word: {random_word}")
# while True:
# response = MODEL.generate(
# f"Tell me about {random_word}.", temp=0.7, callback=stop_on_token_callback
# )
# with Session(engine) as thm_session:
# print(f"Theme func, response: {response}")
# thm_session.add(response)
# thm_session.commit()

View File

@ -3,30 +3,30 @@ Flask app that uses gpt4all to generate random song writing prompt.
"""
import os
import string
from uuid import uuid4
import random
import asyncio
from flask import (
render_template,
session,
request,
)
from gpt4all import GPT4All
from datetime import timedelta
from redbeat import RedBeatSchedulerEntry
from application import app
# , AllWords, Themes
from sqlalchemy.sql.expression import func, select
from sqlalchemy.sql.expression import func
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
from celery import current_app as celery_app
# from celery.schedules import schedule
# from werkzeug.middleware.profiler import ProfilerMiddleware
from werkzeug.middleware.profiler import ProfilerMiddleware
# app.wsgi_app = ProfilerMiddleware(
# app.wsgi_app,
# profile_dir="/Users/normrasmussen/Documents/Projects/gpt-song-prompt/flask-profiler/",
# )
from .background_tasks import grab_word
app.wsgi_app = ProfilerMiddleware(
app.wsgi_app,
profile_dir="/Users/normrasmussen/Documents/Github/song-prompt-webapp/flask-profiler/",
)
engine = create_engine("sqlite:///words_prompts.db", pool_pre_ping=True)
Base = automap_base()
@ -36,13 +36,8 @@ Words = Base.classes.words
Themes = Base.classes.themes
app.config.update(SECRET_KEY=os.urandom(24))
app.permanent_session_lifetime = timedelta(minutes=30)
# app.permanent_session_lifetime = timedelta(minutes=30)
MODEL = GPT4All(
model_name="gpt4all-falcon-q4_0.gguf",
# model_path=(Path.home() / ".cache" / "gpt4all"),
allow_download=False,
)
TIME_SIGNATURES = ["2/4", "3/4", "4/4", "2/2", "6/8", "9/8", "12/8"]
# Need to decide between manually entering this list or using ascii_letters, below
@ -52,82 +47,30 @@ SIGN = ["b", "#"]
MINOR = string.ascii_letters[0:7]
MAJOR = string.ascii_letters[26:33]
async def grab_word():
print("Running Grab_word func")
await asyncio.sleep(1)
while True:
with Session(engine) as word_session:
random_word = word_session.query(Words.words)
random_word = random_word.order_by(func.random()).first()
random_word = str(random_word)[4:-4]
print(f"Word func, random word: {random_word}")
return random_word
async def every(__seconds: float, func, *args, **kwargs):
while True:
func(*args, **kwargs)
await asyncio.sleep(__seconds)
async def main():
asyncio.ensure_future(grab_word())
with app.app_context():
loop = asyncio.get_event_loop()
loop.run_until_complete(grab_word())
loop.run_forever()
async def get_theme():
# SYSTEM_TEMPLATE = "A single sentence based on a word."
# PROMPT_TEMPLATE = "### Instruction: {0} \n### Response: "
WORD_PROMPT = await grab_word()
print(f"Theme Func, word: {WORD_PROMPT}")
while True:
response = MODEL.generate(
f"Tell me about {WORD_PROMPT}.", temp=0.7, callback=stop_on_token_callback)
with Session(engine) as thm_session:
print(f"Theme func, response: {response}")
thm_session.add(response)
thm_session.commit()
@app.route("/")
def main_prompt():
@app.route("/", methods=["GET", "POST"])
def prompt_all():
"""
Main function that loads the prompt
"""
return render_template("index.html", title="Home")
@app.route("/all", methods=["GET", "POST"])
def prompt_all():
schedule_id = str(uuid4())
interval = celery_app.schedule(run_every=60)
entry = RedBeatSchedulerEntry(
schedule_id, "application.background_tasks.grab_word", interval
)
entry.save()
if request.method == "POST":
message = "Results are here"
session["output_key"] = random.choice(KEYS) + random.choice(SIGN)
session["output_signature"] = random.choice(TIME_SIGNATURES)
with Session(engine) as word_session:
random_theme = word_session.query(Themes.themes)
session["output_theme"] = random_theme.order_by(func.random()).first()
# with MODEL.chat_session(SYSTEM_TEMPLATE, PROMPT_TEMPLATE):
# response = MODEL.generate(f"A single sentence about {WORD_PROMPT}.", temp=0.7)
# session["output_theme"] = str(response.splitlines()[0])
theme = random_theme.order_by(func.random()).first()
session["output_theme"] = str(theme)[3:-3]
return render_template("single-button.html", title="Results", message=message)
return render_template("single-button.html", title="Single Option")
def stop_on_token_callback(token_string):
"""
Function to limit return length of the gpt4all response. Period indicates a sentence.
"""
if "." in token_string:
return False
return True
if __name__ == "__main__":
app.run(debug=True)

13
application/utils.py Normal file
View File

@ -0,0 +1,13 @@
from celery import Celery
def make_celery(app):
celery = Celery(app.import_name)
celery.conf.update(app.config["CELERY_CONFIG"])
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery

BIN
dump.rdb Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -13,14 +13,24 @@ def prompt():
WORD_PROMPT = str(requests.get("https://random-word-api.herokuapp.com/word").text)[
2:-2
]
SYSTEM_TEMPLATE = "A creative response about a word."
PROMPT_TEMPLATE = "### Instruction: {0} \n### Response: "
# SYSTEM_TEMPLATE = "A creative response about a word."
# PROMPT_TEMPLATE = "### Instruction: {0} \n### Response: "
response = MODEL.generate(
f"Tell me something interesting about {WORD_PROMPT}.",
temp=0.7,
f"Give me a writing prompt where the writer has to write a song based on {WORD_PROMPT}.",
temp=1,
callback=stop_on_token_callback,
)
response2 = MODEL.generate(
f"Give me a writing prompt about {WORD_PROMPT}.",
#"Tell me one-sentence about Jitteriest.",
temp=1,
callback=stop_on_token_callback,
)
print("Make up a short story response:")
print(response)
print("\n")
print("Tell me one-sentence story about.")
print(response2)
# with MODEL.chat_session(SYSTEM_TEMPLATE, PROMPT_TEMPLATE):
# response = MODEL.generate(f"A single sentence about {WORD_PROMPT}.", temp=0.7)
@ -30,8 +40,12 @@ def prompt():
def stop_on_token_callback(token_id, token_string):
# per_amt = token_string.count('.')
# while per_amt < 3:
# return True
# return False
if "." in token_string:
return False
return False
return True

4
pyrightconfig.json Normal file
View File

@ -0,0 +1,4 @@
{
"venv" : "songprompt",
"venvPath" : "/Users/normrasmussen/.pyenv/versions"
}

View File

@ -1,23 +1,224 @@
absl-py==1.4.0
adafruit-nrfutil==0.5.3.post16
aiohttp==3.8.4
aiosignal==1.3.1
albumentations==0.4.3
altair==5.0.1
amqp==5.2.0
antlr4-python3-runtime==4.8
anyio==3.7.0
asgiref==3.7.2
async-timeout==4.0.2
asyncio==3.4.3
attrs==23.1.0
Automat==22.10.0
beautifulsoup4==4.12.2
billiard==4.2.0
bitstring==4.0.2
blinker==1.7.0
braceexpand==0.1.7
bs4==0.0.1
cachetools==5.3.1
celery==5.3.6
celery-redbeat==2.1.1
certifi==2023.11.17
cffi==1.15.1
charset-normalizer==3.3.2
click==8.1.7
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.3.0
coloredlogs==15.0.1
constantly==15.1.0
contourpy==1.1.0
cryptography==41.0.1
cssselect==1.2.0
cycler==0.11.0
decorator==5.1.1
Deprecated==1.2.14
dotmap==1.3.30
ecdsa==0.18.0
einops==0.3.0
esptool==4.6.2
exceptiongroup==1.1.1
fastapi==0.98.0
ffmpy==0.3.0
filelock==3.12.2
Flask==3.0.0
Flask-SQLAlchemy==3.1.1
flatbuffers==23.5.26
fonttools==4.40.0
frozenlist==1.3.3
fsspec==2023.6.0
ftfy==6.1.1
future==0.18.3
geocoder==1.38.1
gevent==23.9.1
gitdb==4.0.10
GitPython==3.1.31
google-auth==2.20.0
google-auth-oauthlib==1.0.0
gpt4all==2.0.2
gradio==3.13.2
greenlet==3.0.1
grpcio==1.56.0
h11==0.14.0
httpcore==0.17.2
httpx==0.24.1
huggingface-hub==0.15.1
humanfriendly==10.0
hyperlink==21.0.0
idna==3.6
imageio==2.9.0
imageio-ffmpeg==0.4.2
imgaug==0.2.6
importlib-metadata==6.7.0
incremental==22.10.0
iniconfig==2.0.0
invisible-watermark==0.1.5
itemadapter==0.8.0
itemloaders==1.1.0
itsdangerous==2.1.2
Jinja2==3.1.2
jmespath==1.0.1
jsonschema==4.17.3
kiwisolver==1.4.4
kombu==5.3.4
kornia==0.6.0
lazy_loader==0.2
linkify-it-py==2.0.2
lxml==4.9.3
Markdown==3.4.3
markdown-it-py==3.0.0
MarkupSafe==2.1.3
matplotlib==3.7.1
mdit-py-plugins==0.4.0
mdurl==0.1.2
meshtastic==2.1.9
mpmath==1.3.0
multidict==6.0.4
networkx==3.1
numpy==1.25.0
oauthlib==3.2.2
omegaconf==2.1.1
onnx==1.14.0
onnxruntime==1.15.1
open-clip-torch==2.7.0
opencv-python==4.7.0.72
opencv-python-headless==4.7.0.72
orjson==3.9.1
packaging==23.1
pandas==2.0.2
parsel==1.8.1
pexpect==4.8.0
Pillow==9.5.0
playwright==1.38.0
pluggy==1.3.0
prompt-toolkit==3.0.41
Protego==0.3.0
protobuf==4.23.3
psutil==5.9.5
ptyprocess==0.7.0
pudb==2019.2
pyarrow==12.0.1
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycodestyle==2.11.0
pycparser==2.21
pycryptodome==3.18.0
pydantic==1.10.9
pydeck==0.8.1b0
pyDeprecate==0.3.1
PyDispatcher==2.0.7
pydub==0.25.1
pyee==9.0.4
PyGithub==1.59.0
Pygments==2.15.1
PyJWT==2.7.0
Pympler==1.0.1
PyNaCl==1.5.0
pyOpenSSL==23.2.0
pyparsing==3.1.0
Pypubsub==4.0.3
PyQRCode==1.2.1
pyrsistent==0.19.3
pyserial==3.5
PySide6==6.5.1.1
PySide6-Addons==6.5.1.1
PySide6-Essentials==6.5.1.1
pytest==7.4.2
pytest-base-url==2.0.0
pytest-playwright==0.4.2
python-dateutil==2.8.2
python-dotenv==1.0.0
python-multipart==0.0.6
python-slugify==8.0.1
pytorch-lightning==1.4.2
pytz==2023.3
pytz-deprecation-shim==0.1.0.post0
PyWavelets==1.4.1
PyYAML==6.0
qt-material==2.14
queuelib==1.6.2
ratelim==0.1.6
redis==5.0.1
reedsolo==1.5.4
regex==2023.6.3
requests==2.31.0
requests-file==1.5.1
requests-oauthlib==1.3.1
rich==13.4.2
rsa==4.9
scikit-image==0.20.0
scipy==1.10.1
Scrapy==2.11.0
service-identity==23.1.0
shiboken6==6.5.1.1
six==1.16.0
smmap==5.0.0
sniffio==1.3.0
soupsieve==2.5
SQLAlchemy==2.0.23
starlette==0.27.0
streamlit==1.23.1
streamlit-drawable-canvas==0.8.0
sympy==1.12
tabulate==0.9.0
tenacity==8.2.2
tensorboard==2.13.0
tensorboard-data-server==0.7.1
test-tube==0.7.5
text-unidecode==1.3
tifffile==2023.4.12
timeago==1.0.16
tldextract==3.6.0
tokenizers==0.12.1
toml==0.10.2
tomli==2.0.1
toolz==0.12.0
torch==2.0.1
torchmetrics==0.6.0
torchvision==0.15.2
tornado==6.3.2
tqdm==4.66.1
transformers==4.19.2
Twisted==22.10.0
typing_extensions==4.8.0
tzdata==2023.3
tzlocal==4.3.1
uc-micro-py==1.0.2
urllib3==2.1.0
urwid==2.1.2
uvicorn==0.22.0
validators==0.20.0
vine==5.1.0
w3lib==2.1.2
wcwidth==0.2.6
webdataset==0.2.5
websockets==11.0.3
Werkzeug==3.0.1
wrapt==1.15.0
yarl==1.9.2
zipp==3.15.0
zope.event==5.0
zope.interface==6.1

2
run.py Normal file
View File

@ -0,0 +1,2 @@
from application import app

Binary file not shown.