Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files
.env
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from langchain.llms import AzureOpenAI
|
| 5 |
+
from langchain.chat_models import AzureChatOpenAI
|
| 6 |
+
from langchain.chains import SQLDatabaseChain
|
| 7 |
+
from langchain.agents import Tool, AgentType, initialize_agent
|
| 8 |
+
from langchain.memory import ConversationBufferMemory
|
| 9 |
+
from langchain.prompts.prompt import PromptTemplate
|
| 10 |
+
|
| 11 |
+
from load_dotenv import load_dotenv
|
| 12 |
+
from sqlalchemy import MetaData, create_engine, inspect, select, text
|
| 13 |
+
|
| 14 |
+
# The file we created above
|
| 15 |
+
from loader import load_database
|
| 16 |
+
# Load the .env file to access the keys
|
| 17 |
+
load_dotenv()
|
| 18 |
+
|
| 19 |
+
# Set up the completion and chat llm (optional, experiment with this!)
|
| 20 |
+
llm = AzureOpenAI(deployment_name="your_deployment_name",
|
| 21 |
+
model_name="text-davinci-003")
|
| 22 |
+
chat_llm = AzureChatOpenAI(deployment_name="gpt-35-turbo",temperature=0.1)
|
| 23 |
+
|
| 24 |
+
# Set up the chat llm
|
| 25 |
+
os.environ["OPENAI_API_VERSION"]=os.getenv('OPENAI_CHAT_API_VERSION')
|
| 26 |
+
|
| 27 |
+
# Create engine and Call the function to ingest the data
|
| 28 |
+
engine = create_engine('sqlite:///db', echo=True)
|
| 29 |
+
db = load_database(engine)
|
| 30 |
+
|
| 31 |
+
# OR
|
| 32 |
+
|
| 33 |
+
# if the database exists somewhere you could do something like;
|
| 34 |
+
engine = create_engine("your custom URL, example - postgresql+psycopg2://scott:tiger@localhost:5432/mydatabase")
|
| 35 |
+
db = load_database(engine)
|
| 36 |
+
|
| 37 |
+
# Create an inspector object to inspect the database
|
| 38 |
+
inspector = inspect(engine)
|
| 39 |
+
|
| 40 |
+
# Get the list of table names
|
| 41 |
+
table_names = inspector.get_table_names()
|
| 42 |
+
|
| 43 |
+
# Create SQLDatabaseChain
|
| 44 |
+
sql_chain = SQLDatabaseChain.from_llm(llm, db,
|
| 45 |
+
verbose=True, use_query_checker=True)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Create SQLDatabaseChain
|
| 49 |
+
one_sql_chain = SQLDatabaseChain.from_llm(llm, car_db,
|
| 50 |
+
verbose=True, use_query_checker=True)
|
| 51 |
+
|
| 52 |
+
two_sql_chain = SQLDatabaseChain.from_llm(llm, bike_db,
|
| 53 |
+
verbose=True, use_query_checker=True)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
memory = ConversationBufferMemory(memory_key="chat_history",
|
| 58 |
+
return_messages=True)
|
| 59 |
+
|
| 60 |
+
tools = [one_sql_tool, two_sql_tool]
|
| 61 |
+
|
| 62 |
+
conversational_agent = initialize_agent(
|
| 63 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 64 |
+
tools=tools,
|
| 65 |
+
llm=llm,
|
| 66 |
+
verbose=True,
|
| 67 |
+
max_iterations=3,
|
| 68 |
+
memory=memory,
|
| 69 |
+
dialect='ansi',
|
| 70 |
+
early_stopping_method="generate",
|
| 71 |
+
handle_parsing_errors=True,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Define a simple query function that runs the query agent and returns the response
|
| 75 |
+
def query_fnr(input_text):
|
| 76 |
+
response = conversational_agent.run(input=input_text)
|
| 77 |
+
return response
|
| 78 |
+
|
| 79 |
+
# Build the UI
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=query_fn,
|
| 82 |
+
inputs=gr.inputs.Textbox(label="Enter your query"),
|
| 83 |
+
outputs=gr.outputs.Textbox(label="Query Result"),
|
| 84 |
+
title="Domain-specific chatbot"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Launch the UI but do not share it publicly
|
| 88 |
+
iface.launch(share=False, server_port=8080)
|
loader.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain import SQLDatabase
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sqlalchemy import create_engine, Column, Integer, String, Date
|
| 4 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 5 |
+
from sqlalchemy.orm import sessionmaker
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from sqlalchemy import create_engine, Column, String, Integer, Date
|
| 8 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 9 |
+
from sqlalchemy.orm import sessionmaker
|
| 10 |
+
|
| 11 |
+
def create_demand_table(engine, table_name, excel_file):
|
| 12 |
+
# Read the Excel file
|
| 13 |
+
dataframes = pd.read_excel(excel_file, sheet_name=None)
|
| 14 |
+
|
| 15 |
+
# Create a base class for the table models
|
| 16 |
+
Base = declarative_base()
|
| 17 |
+
|
| 18 |
+
# Define the table model
|
| 19 |
+
class DemandPlanned(Base):
|
| 20 |
+
__tablename__ = table_name
|
| 21 |
+
|
| 22 |
+
KEY = Column(String, primary_key=True)
|
| 23 |
+
DU = Column(String)
|
| 24 |
+
ORIGIN = Column(String)
|
| 25 |
+
DESTINATION = Column(String)
|
| 26 |
+
DEMAND_PLANNED_QTY = Column(Integer)
|
| 27 |
+
DEMAND_PLANNED_DATE = Column(Date)
|
| 28 |
+
|
| 29 |
+
# Drop the existing table in the SQLite database, if it exists
|
| 30 |
+
Base.metadata.drop_all(engine)
|
| 31 |
+
|
| 32 |
+
# Create the table in the SQLite database
|
| 33 |
+
Base.metadata.create_all(engine)
|
| 34 |
+
|
| 35 |
+
# Create a session to interact with the database
|
| 36 |
+
Session = sessionmaker(bind=engine)
|
| 37 |
+
|
| 38 |
+
with Session() as session:
|
| 39 |
+
# Insert data into the table (db)
|
| 40 |
+
demand = dataframes.get('Demand-Planned')
|
| 41 |
+
if demand is not None:
|
| 42 |
+
demand['DEMAND_PLANNED_DATE'] = pd.to_datetime(demand['DEMAND_PLANNED_DATE']).dt.strftime('%Y-%m-%d')
|
| 43 |
+
demand.to_sql(table_name, con=engine, if_exists='append', index=False)
|
| 44 |
+
|
| 45 |
+
db = SQLDatabase(engine)
|
| 46 |
+
|
| 47 |
+
# Commit the changes to the production database
|
| 48 |
+
session.commit()
|
| 49 |
+
|
| 50 |
+
# Close the session
|
| 51 |
+
session.close()
|
| 52 |
+
|
| 53 |
+
return db
|
requirements.txt
ADDED
|
File without changes
|