This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
103 lines (89 loc) · 3.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from os import system
from dataclasses import dataclass
from sqlite3 import connect, Error as SQLError
from ollama import create, RequestError, ResponseError
from loguru import logger
DB_PATH: str = "db/models.db"
name = input("Choose a name for your model: ")
select = input("Select a model to train: ")
prompt = input("Enter a prompt to train the model with: ")
backup = input("Would you like to backup this model? (y/n/restore/view): ")
system("clear")
modelfile: str = f"""
FROM {select}
SYSTEM {prompt}
"""
class Database:
@staticmethod
def create_backup(path: str) -> None:
try:
conn = connect(path)
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS models (name TEXT, modelfile TEXT)")
c.execute("INSERT INTO models VALUES (?, ?)", (name, modelfile))
conn.commit()
except SQLError as e:
logger.error(e)
finally:
conn.close()
@staticmethod
def restore_backup(path: str) -> None:
try:
conn = connect(path)
c = conn.cursor()
c.execute("SELECT * FROM models")
models = c.fetchall()
for model in models:
create(model=model[0], modelfile=model[1])
logger.info(f"Restored Model: {model[0]}")
except SQLError as e:
logger.error(e)
finally:
conn.close()
@staticmethod
def lookup(path: str, name: str) -> None:
try:
conn = connect(path)
c = conn.cursor()
c.execute("SELECT modelfile FROM models WHERE name=?", (name,))
modelfile = c.fetchone()
logger.info(f"Model: {name}\n {modelfile[0]}")
except SQLError as e:
logger.error(e)
finally:
conn.close()
class Factory:
@staticmethod
def generate() -> None:
try:
model: Model = Model(name, modelfile)
create(model=model.name, modelfile=model.modelfile)
logger.info(f"Generated Model: {model.name}")
except (RuntimeError, RequestError, ResponseError) as e:
logger.error(e)
finally:
match backup:
case "y":
Database.create_backup(DB_PATH)
logger.info(f"Backup created for Model: {name}")
case "n":
logger.info("No backup created")
exit(0)
case "restore":
logger.info("Restoring backup...")
Database.restore_backup(DB_PATH)
exit(0)
case "view":
query = input("Enter the name of the backup you'd like to view: ")
system("clear")
Database.lookup(DB_PATH, query)
exit(0)
case _:
logger.error("No backup created. Invalid input, exiting...")
exit(1)
@dataclass
class Model(Factory):
name: str
modelfile: str
if __name__ == "__main__":
Model.generate()