curranjanssens commited on
Commit
b081033
1 Parent(s): 02f495a

Add application file

Browse files
Files changed (1) hide show
  1. SQLDB.py +107 -0
SQLDB.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import re
3
+ import openai
4
+
5
+ def gpt3(texts):
6
+ openai.api_key ="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"
7
+
8
+ response = openai.Completion.create(
9
+ engine="code-davinci-002",
10
+ prompt= texts,
11
+ temperature=0,
12
+ max_tokens=100,
13
+ top_p=1,
14
+ frequency_penalty=0.0,
15
+ presence_penalty=0.0,
16
+ suffix='")',
17
+ stop = ("conn.close()", "</code>", "#")
18
+ )
19
+ x = response.choices[0].text
20
+ #extract the text inside of the cur.execute() function. for example 'cur.execute("SELECT * FROM gptsum")' would turn into 'SELECT * FROM gptsum'
21
+ x = re.sub(r'(cur.execute\(\"|\"\))', '', x)
22
+ # cut everything off after the first "\n"
23
+ x = x.split("\n")[0]
24
+ return x
25
+
26
+
27
+
28
+ #using the name of the database, return the table names and the column names
29
+
30
+ def print_attributes(database, table):
31
+ conn = sqlite3.connect(database)
32
+ c = conn.cursor()
33
+ c.execute("PRAGMA table_info(" + table + ")")
34
+ print(c.fetchall())
35
+ conn.close()
36
+
37
+ # if tabes returns ([], []), re do the function and the base input
38
+ def get_tables():
39
+ # make the base variable global
40
+ global base
41
+ base = input("Name the database to connect to:")
42
+ if len(base) <5:
43
+ print("The name is too short")
44
+ get_tables()
45
+ #exit clause
46
+ if base == "exit":
47
+ return
48
+ conn = sqlite3.connect(base)
49
+ c = conn.cursor()
50
+ c.execute("SELECT name FROM sqlite_master WHERE type='table';")
51
+ tables = c.fetchall()
52
+
53
+
54
+ conn.close()
55
+ if tables == [] and columns == []:
56
+ get_tables()
57
+ return tables
58
+
59
+
60
+
61
+
62
+ #tables = get_tables()
63
+
64
+ #if len(base)==0:
65
+ # base= "gmaildb.sqlite"
66
+
67
+ # turn this: "request=input("What would you like to do to the database?")" into a callable function that repeats itself if too short
68
+
69
+ def request():
70
+ requests=input("What would you like to do to the database?")
71
+ if len(requests) < 5:
72
+ print("The request is too short")
73
+ request()
74
+ return requests
75
+
76
+ import gradio as gr
77
+
78
+
79
+ # create a UI using gradio that takes in a text name and a text prompt, which then is displayed back to the user
80
+
81
+ def greet( prompt):
82
+ txt= ("# Python 3 \n# SQLite \n# "+prompt+ "\nimport sqlite3"+'\nconn = sqlite3.connect("database")\ncur= conn.cursor()\ncur.execute("')
83
+ sql = gpt3(txt)
84
+ return sql
85
+
86
+ #the scrip variable is a string of python code with a sql query in it. execute the code keep the result in a variable
87
+
88
+
89
+
90
+
91
+ iface = gr.Interface(greet, inputs = ["text"], outputs = "text")
92
+ iface.launch(share=True )
93
+
94
+
95
+ #breakpoint()
96
+ #req = request()
97
+
98
+ #txt= ("# Python 3 \n# SQLite \n# "+req+ "\nimport sqlite3"+'\nconn = sqlite3.connect("'+ base +'")\ncur = conn.cursor()')
99
+ #cod = gpt3(txt)
100
+
101
+
102
+ #scrip= (txt+str(cod))
103
+ #print(scrip)
104
+ #exec(scrip)
105
+
106
+
107
+