Skip to content

Commit f813c10

Browse files
committed
mvp coded to bare minimum of the app working
1 parent 763e87e commit f813c10

7 files changed

Lines changed: 119 additions & 53 deletions

File tree

.idea/dataSources.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rowgen/hf_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,18 @@ def send_message_to_api(self, message: str) -> str:
2323
],
2424
)
2525
return completion.choices[0].message["content"]
26+
27+
def prompt_fake_data(self, db_schema: str, num_rows: int = 10) -> str:
28+
"""
29+
Generates a prompt based on the DB information it recieves
30+
:return: str. the response of ai api.
31+
"""
32+
prompt = f"""
33+
Generate {num_rows} rows of fake sql data for the following table:column schema. Make the data look believable and realistic (not john doe or anything like that).
34+
Your response should be only in plain json with no words before or after. table:column schema: {db_schema}
35+
"""
36+
return self.send_message_to_api(prompt)
37+
38+
def prompt_insert_statements(self, jsondata: str, table_name: str):
39+
prompt = f"""Given the following jsondata that is to be inserted into a SQL table (table_name:{table_name}, generate insert statements for it. Your output should only be insert statements without any sentences before or after. data: {jsondata}"""
40+
return self.send_message_to_api(prompt)

src/rowgen/json_parser.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/rowgen/main.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from hf_api import HFapi
44
import argparse
55

6+
from rowgen.extract_from_db import DBconnect
7+
from rowgen.parser import parse_sql_from_code_block
8+
69

710
def main():
811
parser = argparse.ArgumentParser(description="RowGen CLI")
@@ -25,10 +28,20 @@ def main():
2528
else:
2629
db_url = "{args.db_type}://{args.user}:{args.password}@{args.host}:{args.port}/{args.database}"
2730

28-
engine = create_engine(db_url)
29-
with engine.connect() as conn:
30-
result = conn.execute(text("SELECT 1"))
31-
print("Connected to database:", result.scalar())
31+
dbc = DBconnect(db_url)
32+
hf = HFapi()
33+
data = hf.prompt_fake_data(dbc.table_columns, 20)
34+
inserts = hf.prompt_insert_statements(data, table_name="users")
35+
# print(inserts)
36+
sql_parser = parse_sql_from_code_block(inserts)
37+
sql_parser = sql_parser.split("\n")
38+
print(sql_parser)
39+
# execute
40+
engine = create_engine("sqlite:///testrowgendb.sqlite") # replace with your DB URL
41+
with engine.connect() as connection:
42+
for sql in sql_parser:
43+
connection.execute(text(sql))
44+
connection.commit()
3245

3346

3447
if __name__ == "__main__":

src/rowgen/parser.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import json
2+
3+
4+
def trim_code_block(text: str, language: str = "") -> str:
5+
"""
6+
Removes markdown code block fences like ```json ... ```
7+
"""
8+
prefix = f"```{language}" if language else "```"
9+
10+
if text.startswith(prefix):
11+
text = text.removeprefix(prefix).strip()
12+
elif text.startswith("```"):
13+
text = text.removeprefix("```").strip()
14+
15+
if text.endswith("```"):
16+
text = text.removesuffix("```").strip()
17+
18+
return text
19+
20+
21+
def parse_json_from_code_block(text: str) -> dict | None:
22+
"""
23+
Trim and parse JSON text from code block
24+
"""
25+
cleaned = trim_code_block(text, "json")
26+
try:
27+
return json.loads(cleaned)
28+
except json.JSONDecodeError as e:
29+
print("Failed to parse JSON:", e)
30+
print(cleaned)
31+
return None
32+
33+
34+
def parse_sql_from_code_block(text: str) -> str:
35+
"""
36+
Return cleaned SQL query string
37+
"""
38+
return trim_code_block(text, "sql")
39+
40+
41+
def save_json(data: dict, file_path: str = "output.json"):
42+
with open(file_path, "w") as f:
43+
json.dump(data, f)
44+
45+
46+
# print(
47+
# parse_sql_from_code_block(
48+
# """```sql
49+
# INSERT INTO users (id, name, username, rank) VALUES
50+
# (1, 'Alice Johnson', 'alicej', 3),
51+
# (2, 'Bob Smith', 'bobsmith', 1),
52+
# (3, 'Charlie Brown', 'charlieb', 2),
53+
# (4, 'Diana Prince', 'dianap', 4),
54+
# (5, 'Evan Wright', 'evanw', 5),
55+
# (6, 'Fiona Gallagher', 'fionag', 2),
56+
# (7, 'George King', 'georgek', 3),
57+
# (8, 'Hannah Montana', 'hannahm', 1),
58+
# (9, 'Ian McKellen', 'ianm', 4),
59+
# (10, 'Jessica Jones', 'jessicaj', 5),
60+
# (11, 'Kevin Hart', 'kevinh', 2),
61+
# (12, 'Laura Palmer', 'laurap', 3),
62+
# (13, 'Michael Scott', 'michaels', 1),
63+
# (14, 'Nancy Drew', 'nancyd', 4),
64+
# (15, 'Oscar Wilde', 'oscarw', 5),
65+
# (16, 'Pam Beesly', 'pamb', 2),
66+
# (17, 'Quincy Jones', 'quincyj', 3),
67+
# (18, 'Rachel Green', 'rachelg', 1),
68+
# (19, 'Steve Rogers', 'stever', 4),
69+
# (20, 'Tina Fey', 'tinaf', 5);
70+
# ```"""
71+
# )
72+
# )

src/rowgen/testrowgendb.sqlite

12 KB
Binary file not shown.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from rowgen.json_parser import JsonParse
2+
from rowgen.parser import parse_sql_from_code_block, parse_json_from_code_block
33
from rowgen.hf_api import HFapi
44
from pytest import fixture
55

@@ -13,9 +13,9 @@ def test_rows_json(hfapi):
1313
raw_query = hfapi.send_message_to_api(
1414
"This is my database: id, name, username, email, access_level. generate 5 fake data rows for it. don't say anything just generate in json. i want your whole message to be just a json"
1515
)
16-
jsp = JsonParse(raw_query)
16+
jsp = parse_json_from_code_block(raw_query)
1717
try:
18-
json.loads(jsp.processed_query)
18+
json.loads(jspy)
1919
return True
2020
except json.JSONDecodeError:
2121
return False

0 commit comments

Comments
 (0)