Spaces:
Running
Running
pgurazada1
commited on
Commit
•
0e32d1b
1
Parent(s):
9b1037c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ChatOpenAI
|
8 |
+
|
9 |
+
ccms_db_loc = 'ccms.db'
|
10 |
+
|
11 |
+
ccms_db = SQLDatabase.from_uri(f"sqlite:///{ccms_db_loc}")
|
12 |
+
|
13 |
+
gpt4 = ChatOpenAI(
|
14 |
+
model_name=model_gpt4,
|
15 |
+
api_key=creds["openai_api_key"],
|
16 |
+
temperature=0
|
17 |
+
)
|
18 |
+
|
19 |
+
sqlite_agent = create_sql_agent(
|
20 |
+
gpt4,
|
21 |
+
db=ccms_db,
|
22 |
+
agent_type="openai-tools",
|
23 |
+
verbose=True
|
24 |
+
)
|
25 |
+
|
26 |
+
def predict(user_input):
|
27 |
+
|
28 |
+
try:
|
29 |
+
response = sqlite_agent.invoke(user_input)
|
30 |
+
|
31 |
+
prediction = response['output']
|
32 |
+
|
33 |
+
except Exception as e:
|
34 |
+
prediction = e
|
35 |
+
|
36 |
+
return prediction
|
37 |
+
|
38 |
+
|
39 |
+
textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
|
40 |
+
|
41 |
+
demo = gr.Interface(
|
42 |
+
inputs=textbox, fn=predict, outputs="text",
|
43 |
+
title="Query Credit Card Database",
|
44 |
+
description="This web API presents an interface to ask questions on information stored in a credit card database.",
|
45 |
+
article=schema,
|
46 |
+
examples=[["Who are the top 5 merchants by total transactions?", ""],
|
47 |
+
["How many customers are in our database?", ""],
|
48 |
+
["How many accounts in total do we have?", ""]
|
49 |
+
],
|
50 |
+
concurrency_limit=8
|
51 |
+
)
|
52 |
+
|
53 |
+
|
54 |
+
demo.queue()
|
55 |
+
demo.launch(auth=("demouser", os.getenv('PASSWD')))
|