Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,59 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
-
st.title("Generate responses from SQL tables")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from sqlalchemy import create_engine, inspect, URL
|
| 3 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 4 |
+
from langchain_community.utilities import SQLDatabase
|
| 5 |
+
from langchain_community.agent_toolkits import create_sql_agent
|
| 6 |
+
from langchain import PromptTemplate, SQLDatabase, HuggingFaceHub
|
| 7 |
+
from langchain_experimental.sql.base import SQLDatabaseChain
|
| 8 |
import streamlit as st
|
| 9 |
+
import pyodbc
|
| 10 |
|
| 11 |
+
print(pyodbc.drivers())
|
| 12 |
|
| 13 |
+
st.title("Generate responses from SQL tables")
|
| 14 |
+
|
| 15 |
+
your_endpoint_url = "https://mht99ia9b939puzq.us-east-1.aws.endpoints.huggingface.cloud"
|
| 16 |
+
|
| 17 |
+
template = """You are a SQLite expert. Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer to the input question.
|
| 18 |
+
The final answer should be in a concise natural language.
|
| 19 |
+
Use the following format:
|
| 20 |
+
Question: Question here
|
| 21 |
+
SQLQuery: SQL Query to run
|
| 22 |
+
SQLResult: Result of the SQLQuery
|
| 23 |
+
Answer: Final answer here.
|
| 24 |
+
Only use the following tables:
|
| 25 |
+
{table_info}
|
| 26 |
+
Question: {input}"""
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if __name__ == '__main__':
|
| 30 |
+
connection_string = ("Driver=ODBC Driver 18 for SQL Server;Server=crawlersdb.c3pzpntwjvdf.us-east-1.rds.amazonaws.com;Database=SmartClever;PORT=1433;UID=CleverData;PWD={};TrustServerCertificate=yes;".format(os.environ['DB_PWD'])
|
| 31 |
+
)
|
| 32 |
+
connection_url = URL.create(
|
| 33 |
+
"mssql+pyodbc",
|
| 34 |
+
query={"odbc_connect": connection_string}
|
| 35 |
+
)
|
| 36 |
+
engine = create_engine(connection_url)
|
| 37 |
+
db = SQLDatabase(engine=engine)
|
| 38 |
+
|
| 39 |
+
llm = HuggingFaceEndpoint(
|
| 40 |
+
endpoint_url=f"{your_endpoint_url}",
|
| 41 |
+
max_new_tokens=4096,
|
| 42 |
+
top_k=10,
|
| 43 |
+
top_p=0.95,
|
| 44 |
+
typical_p=0.95,
|
| 45 |
+
temperature=0.01,
|
| 46 |
+
repetition_penalty=1.03,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
prompt = PromptTemplate(template=template, input_variables=["input","table_info","top_k"])
|
| 50 |
+
sql_chain = SQLDatabaseChain.from_llm(prompt=prompt, llm=llm, db=db, verbose=True)
|
| 51 |
+
|
| 52 |
+
question = st.text_input("Ask a question in natural language and press enter")
|
| 53 |
+
|
| 54 |
+
if question != "":
|
| 55 |
+
try:
|
| 56 |
+
response = sql_chain.invoke(question)
|
| 57 |
+
st.write("Answer to your question is - ", response['result'].split("\n")[0])
|
| 58 |
+
except:
|
| 59 |
+
st.write("Sorry I cannot answer that. Please try again later.")
|