Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
import os
|
4 |
-
import sqlite3
|
5 |
import google.generativeai as genai
|
6 |
-
import time
|
7 |
|
8 |
# Initialize Gemini
|
9 |
gemini_key = os.getenv("gemini_key")
|
@@ -14,20 +14,28 @@ class SQLPromptModel:
|
|
14 |
def __init__(self, database):
|
15 |
# Initialize with database file path and create connection
|
16 |
self.database = database
|
17 |
-
self.conn = sqlite3.connect(self.database)
|
18 |
|
19 |
def fetch_table_schema(self, table_name):
|
20 |
# Get database table structure
|
|
|
|
|
21 |
cursor = self.conn.cursor()
|
22 |
# PRAGMA table_info returns:
|
23 |
# (id, name, type, notnull, default_value, primary_key)
|
24 |
cursor.execute(f"PRAGMA table_info({table_name})")
|
25 |
schema = cursor.fetchall()
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def text2sql_gemini(self, schema, user_prompt, inp_prompt=None):
|
29 |
# Convert table columns to string format
|
30 |
table_columns = ', '.join([f"{col[1]} {col[2]}" for col in schema])
|
|
|
|
|
31 |
|
32 |
# Create prompt for Gemini AI
|
33 |
prompt = f"""Below are SQL table schemas paired with instructions that describe a task.
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image # to open / display image
|
3 |
import os
|
4 |
+
import sqlite3 # DB for SQL operation, analytical db
|
5 |
import google.generativeai as genai
|
6 |
+
import time #
|
7 |
|
8 |
# Initialize Gemini
|
9 |
gemini_key = os.getenv("gemini_key")
|
|
|
14 |
def __init__(self, database):
|
15 |
# Initialize with database file path and create connection
|
16 |
self.database = database
|
17 |
+
self.conn = sqlite3.connect(self.database) # create connection to DB
|
18 |
|
19 |
def fetch_table_schema(self, table_name):
|
20 |
# Get database table structure
|
21 |
+
# In order to execute SQL statements and fetch results from SQL queries,
|
22 |
+
#we will need to use a database cursor. Call con.cursor() to create the Cursor:
|
23 |
cursor = self.conn.cursor()
|
24 |
# PRAGMA table_info returns:
|
25 |
# (id, name, type, notnull, default_value, primary_key)
|
26 |
cursor.execute(f"PRAGMA table_info({table_name})")
|
27 |
schema = cursor.fetchall()
|
28 |
+
if schema:
|
29 |
+
return schema
|
30 |
+
else
|
31 |
+
return None
|
32 |
+
#return schema if schema else None
|
33 |
|
34 |
def text2sql_gemini(self, schema, user_prompt, inp_prompt=None):
|
35 |
# Convert table columns to string format
|
36 |
table_columns = ', '.join([f"{col[1]} {col[2]}" for col in schema])
|
37 |
+
#col[1] - column name/index
|
38 |
+
#col[2] - column data type
|
39 |
|
40 |
# Create prompt for Gemini AI
|
41 |
prompt = f"""Below are SQL table schemas paired with instructions that describe a task.
|