Spaces:
Runtime error
Runtime error
File size: 2,776 Bytes
b081033 |
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 104 105 106 107 108 |
import sqlite3
import re
import openai
def gpt3(texts):
openai.api_key ="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"
response = openai.Completion.create(
engine="code-davinci-002",
prompt= texts,
temperature=0,
max_tokens=100,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
suffix='")',
stop = ("conn.close()", "</code>", "#")
)
x = response.choices[0].text
#extract the text inside of the cur.execute() function. for example 'cur.execute("SELECT * FROM gptsum")' would turn into 'SELECT * FROM gptsum'
x = re.sub(r'(cur.execute\(\"|\"\))', '', x)
# cut everything off after the first "\n"
x = x.split("\n")[0]
return x
#using the name of the database, return the table names and the column names
def print_attributes(database, table):
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute("PRAGMA table_info(" + table + ")")
print(c.fetchall())
conn.close()
# if tabes returns ([], []), re do the function and the base input
def get_tables():
# make the base variable global
global base
base = input("Name the database to connect to:")
if len(base) <5:
print("The name is too short")
get_tables()
#exit clause
if base == "exit":
return
conn = sqlite3.connect(base)
c = conn.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = c.fetchall()
conn.close()
if tables == [] and columns == []:
get_tables()
return tables
#tables = get_tables()
#if len(base)==0:
# base= "gmaildb.sqlite"
# turn this: "request=input("What would you like to do to the database?")" into a callable function that repeats itself if too short
def request():
requests=input("What would you like to do to the database?")
if len(requests) < 5:
print("The request is too short")
request()
return requests
import gradio as gr
# create a UI using gradio that takes in a text name and a text prompt, which then is displayed back to the user
def greet( prompt):
txt= ("# Python 3 \n# SQLite \n# "+prompt+ "\nimport sqlite3"+'\nconn = sqlite3.connect("database")\ncur= conn.cursor()\ncur.execute("')
sql = gpt3(txt)
return sql
#the scrip variable is a string of python code with a sql query in it. execute the code keep the result in a variable
iface = gr.Interface(greet, inputs = ["text"], outputs = "text")
iface.launch(share=True )
#breakpoint()
#req = request()
#txt= ("# Python 3 \n# SQLite \n# "+req+ "\nimport sqlite3"+'\nconn = sqlite3.connect("'+ base +'")\ncur = conn.cursor()')
#cod = gpt3(txt)
#scrip= (txt+str(cod))
#print(scrip)
#exec(scrip)
|