diff --git a/application/__init__.py b/application/__init__.py index 36df6ff..e3d8e33 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -1,7 +1,25 @@ from flask import Flask from config import Config +from flask_sqlalchemy import SQLAlchemy +from sqlalchemy.ext.declarative import declarative_base + +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) + + def __init__(self, word): + self.word = word + +class Themes(Base, db.Model): + themes = db.Column(db.String, primary_key=True) + + def __init__(self, themes): + self.themes = themes import application.routes diff --git a/application/__pycache__/__init__.cpython-311.pyc b/application/__pycache__/__init__.cpython-311.pyc index 781aa63..182fbd8 100644 Binary files a/application/__pycache__/__init__.cpython-311.pyc and b/application/__pycache__/__init__.cpython-311.pyc differ diff --git a/application/__pycache__/routes.cpython-311.pyc b/application/__pycache__/routes.cpython-311.pyc index 1613c24..7dffc39 100644 Binary files a/application/__pycache__/routes.cpython-311.pyc and b/application/__pycache__/routes.cpython-311.pyc differ diff --git a/application/dbmodels.py b/application/dbmodels.py new file mode 100644 index 0000000..e69de29 diff --git a/application/routes.py b/application/routes.py index 042082a..a160a88 100644 --- a/application/routes.py +++ b/application/routes.py @@ -4,26 +4,43 @@ Flask app that uses gpt4all to generate random song writing prompt. import os import string import random -import requests +import asyncio from flask import ( render_template, session, request, ) from gpt4all import GPT4All -from datetime import datetime, timezone, timedelta -from pathlib import Path +from datetime import timedelta from application import app -from werkzeug.middleware.profiler import ProfilerMiddleware -app.wsgi_app = ProfilerMiddleware(app.wsgi_app, profile_dir="/root/flask-profiler/") +# , AllWords, Themes +from sqlalchemy.sql.expression import func, select +from sqlalchemy.ext.automap import automap_base +from sqlalchemy.orm import Session +from sqlalchemy import create_engine + +# from werkzeug.middleware.profiler import ProfilerMiddleware + +# app.wsgi_app = ProfilerMiddleware( +# app.wsgi_app, +# profile_dir="/Users/normrasmussen/Documents/Projects/gpt-song-prompt/flask-profiler/", +# ) + +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 app.config.update(SECRET_KEY=os.urandom(24)) app.permanent_session_lifetime = timedelta(minutes=30) MODEL = GPT4All( model_name="gpt4all-falcon-q4_0.gguf", -# model_path=(Path.home() / ".cache" / "gpt4all"), + # 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"] @@ -34,8 +51,46 @@ SIGN = ["b", "#"] # Option 2 MINOR = string.ascii_letters[0:7] MAJOR = string.ascii_letters[26:33] -# and then use this: -# output = random.choice(KEYS)+random.choice(SIGN) + +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("/") @@ -48,58 +103,30 @@ def main_prompt(): @app.route("/all", methods=["GET", "POST"]) def prompt_all(): - print("Running Prompt_all func") - WORD_PROMPT = str(requests.get("https://random-word-api.herokuapp.com/word").text)[ - 2:-2 - ] - SYSTEM_TEMPLATE = 'A single sentence based on a word.' - PROMPT_TEMPLATE = '### Instruction: {0} \n### Response: ' if request.method == "POST": message = "Results are here" session["output_key"] = random.choice(KEYS) + random.choice(SIGN) session["output_signature"] = random.choice(TIME_SIGNATURES) - session["word_prompt"] = WORD_PROMPT + 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]) - response = MODEL.generate(f"Tell me about {WORD_PROMPT}.", temp=0.7, callback=stop_on_token_callback) - session["output_theme"] = response - - # numresp = len(response)- 1 - # if numresp <= 1: - # session["output_theme"] = str(response) - # randresp = random.randrange(0, numresp) - # session["output_theme"] = response 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_id, token_string): - if '.' in token_string: + +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 - else: - return True - -@app.route("/") -def prompt_instrument(): - pass - - -@app.route("/") -def prompt_key(): - pass - - -@app.route("/") -def prompt_timesig(): - pass - - -@app.route("/") -def prompt_influence(): - pass - response = MODEL.generate("The writing prompt is about the weather:", temp=0) - session["list_response"] = response.splitlines() + return True if __name__ == "__main__":