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

@ -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