Spaces:
Sleeping
Sleeping
pgurazada1
commited on
Commit
•
5238443
1
Parent(s):
d0a5472
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
|
|
|
4 |
from langchain_community.utilities.sql_database import SQLDatabase
|
5 |
from langchain_community.agent_toolkits import create_sql_agent
|
6 |
|
7 |
-
from langchain_openai import
|
8 |
|
9 |
ccms_db_loc = 'ccms.db'
|
10 |
|
@@ -18,23 +20,41 @@ gpt4o_azure = AzureChatOpenAI(
|
|
18 |
temperature=0
|
19 |
)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
api_key=os.environ["OPENAI_API_KEY"],
|
24 |
-
temperature=0
|
25 |
-
)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
)
|
32 |
|
33 |
sqlite_agent = create_sql_agent(
|
34 |
-
gpt4o_azure,
|
35 |
db=ccms_db,
|
|
|
36 |
agent_type="openai-tools",
|
37 |
agent_executor_kwargs={'handle_parsing_errors':True},
|
|
|
38 |
verbose=True
|
39 |
)
|
40 |
|
@@ -50,6 +70,7 @@ def predict(user_input):
|
|
50 |
|
51 |
return prediction
|
52 |
|
|
|
53 |
|
54 |
textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
|
55 |
schema = 'The schema for the database is presented below: \n <img src="https://cdn-uploads.huggingface.co/production/uploads/64118e60756b9e455c7eddd6/S1alVt_D88qatd-N4Dkjd.png" > \n<img src="https://cdn-uploads.huggingface.co/production/uploads/64118e60756b9e455c7eddd6/81ggHEjrt6wFrMyXJtHVS.png" > (Source: https://github.com/shrivastavasatyam/Credit-Card-Management-System)'
|
@@ -59,12 +80,19 @@ demo = gr.Interface(
|
|
59 |
title="Query a Credit Card Database",
|
60 |
description="This web API presents an interface to ask questions on information stored in a credit card database.",
|
61 |
article=schema,
|
62 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
cache_examples=False,
|
64 |
theme=gr.themes.Base(),
|
65 |
concurrency_limit=8
|
66 |
)
|
67 |
|
68 |
-
|
69 |
demo.queue()
|
70 |
demo.launch(auth=("demouser", os.getenv('PASSWD')))
|
|
|
1 |
import os
|
2 |
+
|
3 |
import gradio as gr
|
4 |
|
5 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
6 |
from langchain_community.utilities.sql_database import SQLDatabase
|
7 |
from langchain_community.agent_toolkits import create_sql_agent
|
8 |
|
9 |
+
from langchain_openai import AzureChatOpenAI
|
10 |
|
11 |
ccms_db_loc = 'ccms.db'
|
12 |
|
|
|
20 |
temperature=0
|
21 |
)
|
22 |
|
23 |
+
context = ccms_db.get_context()
|
24 |
+
database_schema = context['table_info']
|
|
|
|
|
|
|
25 |
|
26 |
+
system_message = f"""You are a SQLite expert agent designed to interact with a SQLite database.
|
27 |
+
Given an input question, create a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
|
28 |
+
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 5 results using the LIMIT clause as per SQLite. You can order the results to return the most informative data in the database..
|
29 |
+
You can order the results by a relevant column to return the most interesting examples in the database.
|
30 |
+
Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers.
|
31 |
+
You have access to tools for interacting with the database.
|
32 |
+
Only use the given tools. Only use the information returned by the tools to construct your final answer.
|
33 |
+
You MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.
|
34 |
+
|
35 |
+
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.
|
36 |
+
|
37 |
+
If the question does not seem related to the database, just return "I don't know" as the answer.
|
38 |
+
|
39 |
+
Only use the following tables:
|
40 |
+
{database_schema}
|
41 |
+
"""
|
42 |
+
|
43 |
+
full_prompt = ChatPromptTemplate.from_messages(
|
44 |
+
[
|
45 |
+
("system", system_message),
|
46 |
+
("human", '{input}'),
|
47 |
+
MessagesPlaceholder("agent_scratchpad")
|
48 |
+
]
|
49 |
)
|
50 |
|
51 |
sqlite_agent = create_sql_agent(
|
52 |
+
llm=gpt4o_azure,
|
53 |
db=ccms_db,
|
54 |
+
prompt=full_prompt,
|
55 |
agent_type="openai-tools",
|
56 |
agent_executor_kwargs={'handle_parsing_errors':True},
|
57 |
+
max_iterations=10,
|
58 |
verbose=True
|
59 |
)
|
60 |
|
|
|
70 |
|
71 |
return prediction
|
72 |
|
73 |
+
# UI
|
74 |
|
75 |
textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
|
76 |
schema = 'The schema for the database is presented below: \n <img src="https://cdn-uploads.huggingface.co/production/uploads/64118e60756b9e455c7eddd6/S1alVt_D88qatd-N4Dkjd.png" > \n<img src="https://cdn-uploads.huggingface.co/production/uploads/64118e60756b9e455c7eddd6/81ggHEjrt6wFrMyXJtHVS.png" > (Source: https://github.com/shrivastavasatyam/Credit-Card-Management-System)'
|
|
|
80 |
title="Query a Credit Card Database",
|
81 |
description="This web API presents an interface to ask questions on information stored in a credit card database.",
|
82 |
article=schema,
|
83 |
+
examples=[
|
84 |
+
["Who are the top 5 merchants by total transactions?", ""],
|
85 |
+
["Which are the top 5 cities with the highest spend and what is their percentage contribution to overall spends?", ""],
|
86 |
+
["Which is the highest spend month and amount for each card type?", ""],
|
87 |
+
["Which was the city with the lowest percentage spend for the Gold card type?", ""],
|
88 |
+
["What was the percentage contribution of spends by females for each card type?", ""],
|
89 |
+
["Which city has the highest spend to transaction ratio on weekends?", ""],
|
90 |
+
["Which was the city to reach 500 transactions the fastest?", ""]
|
91 |
+
],
|
92 |
cache_examples=False,
|
93 |
theme=gr.themes.Base(),
|
94 |
concurrency_limit=8
|
95 |
)
|
96 |
|
|
|
97 |
demo.queue()
|
98 |
demo.launch(auth=("demouser", os.getenv('PASSWD')))
|