Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,8 @@ import pyodbc
|
|
| 11 |
import openai
|
| 12 |
from langchain_openai import AzureChatOpenAI
|
| 13 |
|
|
|
|
|
|
|
| 14 |
from utils import SQLDatabaseChainPatched, table_search, extract_question_type, extract_table_name
|
| 15 |
|
| 16 |
openai.api_key = os.environ['OPENAI_API_KEY']
|
|
@@ -83,7 +85,7 @@ template = """You are a database expert. Given an input question, first create a
|
|
| 83 |
"""
|
| 84 |
|
| 85 |
if __name__ == '__main__':
|
| 86 |
-
connection_string = ("Driver=FreeTDS;Server=crawlersdb.c3pzpntwjvdf.us-east-1.rds.amazonaws.com;Database=
|
| 87 |
)
|
| 88 |
connection_url = URL.create(
|
| 89 |
"mssql+pyodbc",
|
|
@@ -105,8 +107,22 @@ if __name__ == '__main__':
|
|
| 105 |
'4k': llm
|
| 106 |
})
|
| 107 |
|
| 108 |
-
question = st.text_input("Ask a question in natural language and press enter")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
if 'questions' not in st.session_state:
|
| 111 |
st.session_state['questions'] = []
|
| 112 |
|
|
@@ -130,19 +146,28 @@ if __name__ == '__main__':
|
|
| 130 |
pass
|
| 131 |
|
| 132 |
if db_chain.intermediate_steps.get("sql_cmd_unchecked",'') == '':
|
| 133 |
-
print (db_chain)
|
| 134 |
-
st.markdown("Sorry I cannot answer that. Please try again later.")
|
|
|
|
| 135 |
elif db_chain.intermediate_steps.get("result",'') != '':
|
| 136 |
-
st.markdown("Answer to your question is - " + db_chain.intermediate_steps.get("result",''))
|
| 137 |
-
print (db_chain.intermediate_steps.get("result",''))
|
| 138 |
-
st.markdown("The SQL query is - " + db_chain.intermediate_steps['sql_cmd'])
|
|
|
|
| 139 |
elif db_chain.intermediate_steps.get("sql_data",'') != '':
|
| 140 |
out = pd.DataFrame.from_dict(db_chain.intermediate_steps['sql_data'])
|
| 141 |
-
st.markdown("Here is your result in a table format")
|
| 142 |
-
st.table(out)
|
| 143 |
-
st.markdown("The SQL query is - " + db_chain.intermediate_steps['sql_cmd'])
|
|
|
|
| 144 |
else:
|
| 145 |
-
st.markdown("Sorry I cannot answer that. Please try again later.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
st.session_state['questions'].append(question)
|
| 148 |
|
|
|
|
| 11 |
import openai
|
| 12 |
from langchain_openai import AzureChatOpenAI
|
| 13 |
|
| 14 |
+
from tabulate import tabulate
|
| 15 |
+
|
| 16 |
from utils import SQLDatabaseChainPatched, table_search, extract_question_type, extract_table_name
|
| 17 |
|
| 18 |
openai.api_key = os.environ['OPENAI_API_KEY']
|
|
|
|
| 85 |
"""
|
| 86 |
|
| 87 |
if __name__ == '__main__':
|
| 88 |
+
connection_string = ("Driver=FreeTDS;Server=crawlersdb.c3pzpntwjvdf.us-east-1.rds.amazonaws.com;Database=SmartCleverST;PORT=1433;UID=CleverData;PWD={};TrustServerCertificate=yes;".format(os.environ['DB_PWD'])
|
| 89 |
)
|
| 90 |
connection_url = URL.create(
|
| 91 |
"mssql+pyodbc",
|
|
|
|
| 107 |
'4k': llm
|
| 108 |
})
|
| 109 |
|
| 110 |
+
#question = st.text_input("Ask a question in natural language and press enter")
|
| 111 |
+
|
| 112 |
+
if question := st.chat_input("tion in natural language and press enter"):
|
| 113 |
+
# Display user message in chat message container
|
| 114 |
+
with st.chat_message("user"):
|
| 115 |
+
st.markdown(prompt)
|
| 116 |
+
# Add user message to chat history
|
| 117 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
| 118 |
+
|
| 119 |
+
if "messages" not in st.session_state:
|
| 120 |
+
st.session_state.messages = []
|
| 121 |
|
| 122 |
+
for message in st.session_state.messages:
|
| 123 |
+
with st.chat_message(message["role"]):
|
| 124 |
+
st.markdown(message["content"])
|
| 125 |
+
|
| 126 |
if 'questions' not in st.session_state:
|
| 127 |
st.session_state['questions'] = []
|
| 128 |
|
|
|
|
| 146 |
pass
|
| 147 |
|
| 148 |
if db_chain.intermediate_steps.get("sql_cmd_unchecked",'') == '':
|
| 149 |
+
#print (db_chain)
|
| 150 |
+
#st.markdown("Sorry I cannot answer that. Please try again later.")
|
| 151 |
+
response = "Sorry I cannot answer that. Please try again later."
|
| 152 |
elif db_chain.intermediate_steps.get("result",'') != '':
|
| 153 |
+
#st.markdown("Answer to your question is - " + db_chain.intermediate_steps.get("result",''))
|
| 154 |
+
#print (db_chain.intermediate_steps.get("result",''))
|
| 155 |
+
#st.markdown("The SQL query is - " + db_chain.intermediate_steps['sql_cmd'])
|
| 156 |
+
response = Answer to your question is - " + db_chain.intermediate_steps.get("result",'')
|
| 157 |
elif db_chain.intermediate_steps.get("sql_data",'') != '':
|
| 158 |
out = pd.DataFrame.from_dict(db_chain.intermediate_steps['sql_data'])
|
| 159 |
+
#st.markdown("Here is your result in a table format")
|
| 160 |
+
#st.table(out)
|
| 161 |
+
#st.markdown("The SQL query is - " + db_chain.intermediate_steps['sql_cmd'])
|
| 162 |
+
response = tabulate(out, headers='keys', tablefmt='psql')
|
| 163 |
else:
|
| 164 |
+
#st.markdown("Sorry I cannot answer that. Please try again later.")
|
| 165 |
+
response = "Sorry I cannot answer that. Please try again later."
|
| 166 |
+
|
| 167 |
+
with st.chat_message("assistant"):
|
| 168 |
+
st.markdown(response)
|
| 169 |
+
# Add assistant response to chat history
|
| 170 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 171 |
|
| 172 |
st.session_state['questions'].append(question)
|
| 173 |
|