Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import sqlite3
|
| 4 |
+
from langchain.llms import OpenAI
|
| 5 |
+
from langchain.sql_database import SQLDatabase
|
| 6 |
+
from langchain_experimental.sql import SQLDatabaseChain
|
| 7 |
+
|
| 8 |
+
os.getenviron("openai")
|
| 9 |
+
# Set your OpenAI API key (replace 'your-openai-api-key-here' with your actual key)
|
| 10 |
+
#os.environ['OPENAI_API_KEY'] = 'sk-None-vH7hfJP0CG5YqgXzFzE2T3BlbkFJLPTrdmmF9ca3fRJ7fzc8'
|
| 11 |
+
|
| 12 |
+
# Global variable to store the database path
|
| 13 |
+
db_path = None
|
| 14 |
+
|
| 15 |
+
# Function to upload and set the SQLite database
|
| 16 |
+
def upload_database(db_file):
|
| 17 |
+
global db_path
|
| 18 |
+
db_path = db_file.name # Get the path of the uploaded file
|
| 19 |
+
return f"Database {db_file.name} uploaded successfully!"
|
| 20 |
+
|
| 21 |
+
# Function to run the LangChain SQL queries
|
| 22 |
+
def query_sql_db(query):
|
| 23 |
+
if db_path is None:
|
| 24 |
+
return "Please upload an SQLite database file first."
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# Connect to the uploaded SQLite database
|
| 28 |
+
input_db = SQLDatabase.from_uri(f"sqlite:///{db_path}")
|
| 29 |
+
|
| 30 |
+
# Create an instance of the OpenAI model
|
| 31 |
+
openai_llm = OpenAI()
|
| 32 |
+
|
| 33 |
+
# Use the LLM instance in the SQLDatabaseChain
|
| 34 |
+
db_chain = SQLDatabaseChain.from_llm(openai_llm, input_db, verbose=False)
|
| 35 |
+
|
| 36 |
+
# Execute the query
|
| 37 |
+
result = db_chain.run(query)
|
| 38 |
+
return result
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"An error occurred: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Create a Gradio interface for both file upload and query
|
| 43 |
+
with gr.Blocks() as iface:
|
| 44 |
+
gr.Markdown("# Text-to-SQL")
|
| 45 |
+
gr.Markdown("Upload your SQLite database and then type a question in plain English to get the SQL query result.")
|
| 46 |
+
|
| 47 |
+
# File upload component
|
| 48 |
+
db_file = gr.File(label="Upload SQLite Database", file_types=[".sqlite", ".db"])
|
| 49 |
+
upload_btn = gr.Button("Upload Database")
|
| 50 |
+
upload_output = gr.Textbox(label="Upload Status")
|
| 51 |
+
|
| 52 |
+
# Input for querying the database
|
| 53 |
+
query_input = gr.Textbox(label="Enter your query in plain English")
|
| 54 |
+
query_output = gr.Textbox(label="SQL Query Result")
|
| 55 |
+
query_btn = gr.Button("Run Query")
|
| 56 |
+
|
| 57 |
+
# Link the upload button to the upload_database function
|
| 58 |
+
upload_btn.click(upload_database, inputs=db_file, outputs=upload_output)
|
| 59 |
+
|
| 60 |
+
# Link the query button to the query_sql_db function
|
| 61 |
+
query_btn.click(query_sql_db, inputs=query_input, outputs=query_output)
|
| 62 |
+
|
| 63 |
+
# Launch the app
|
| 64 |
+
iface.launch()
|