File size: 3,438 Bytes
d62d989
 
 
 
 
 
e70ffe8
d62d989
 
 
5e6477e
 
 
9690a07
 
 
4f9109c
d62d989
 
 
 
07d1fb4
d62d989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87e89d8
 
3354acb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87e89d8
 
d74323c
d62d989
 
 
91b1f9f
cca8beb
 
 
ccb73d5
 
 
0efc604
d92b39e
2ea15f8
e70ffe8
d62d989
 
 
0efc604
78a6db8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import gradio as gr

from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent

from langchain_openai import ChatOpenAI

from sklearn.datasets import fetch_openml

gpt35 = ChatOpenAI(
    model_name="gpt-3.5-turbo",
    api_key=os.environ["OPENAI_API_KEY"],
    temperature=0
)

bank_data, _ = fetch_openml(data_id=43718, return_X_y=True, parser="auto")

pandas_agent = create_pandas_dataframe_agent(
    llm=gpt35,
    df=bank_data,
    verbose=False,
    agent_type=AgentType.OPENAI_FUNCTIONS,
)


def predict(user_input):

    try:
        response = pandas_agent.invoke(user_input)

        prediction = response['output']

    except Exception as e:
        prediction = e
        
    return prediction


textbox = gr.Textbox(placeholder="Enter your query here", lines=6)

schema = """
The schema of the table you can query on is presented below:
| Column    | Type   | Description|
|-----------|--------|------------|
| age       | int    |Age of the customer|
| job       | str    |type of job (categorical: "admin.","unknown","unemployed","management","housemaid","entrepreneur","student", "blue-collar","self-employed","retired","technician","services") |
| marital   | str    |marital status (categorical: "married","divorced","single"; note: "divorced" means divorced or widowed)|
| education | str    |(categorical: "unknown","secondary","primary","tertiary")|
| default   | yes/no |has credit in default? (binary: "yes","no")|
| balance   | int    |average yearly balance, in euros (numeric)|
| housing   | yes/no |has housing loan? (binary: "yes","no")|
| loan      | yes/no |has personal loan? (binary: "yes","no")|
| contact   | str    |last contact communication type (categorical: "unknown","telephone","cellular")|
| day       | int    |last contact day of the month (numeric)|
| month     | str    |last contact month of year (categorical: "jan", "feb", "mar", ..., "nov", "dec")|
| duration  | int    |last contact duration, in seconds (numeric)|
| campaign  | int    |number of contacts performed during this campaign and for this client (numeric, includes last contact)|
| pdays     | int    |number of days that passed by after the client was last contacted from a previous campaign (numeric, -1 means client was not previously contacted)|
| poutcome  | str    |outcome of the previous marketing campaign (categorical: "unknown","other","failure","success")|
| deposit   | yes/no |has the client subscribed a term deposit? (binary: "yes","no")|
"""

demo = gr.Interface(
    inputs=textbox, fn=predict, outputs="text",
    title="Query BFSI customer information",
    description="This web API presents an interface to ask questions on customer information stored in a database.",
    article=schema,
    examples=[["What is the average balance maintained by our customers?", ""],
              ["How many customers have subscribed to a term deposit?", ""],
              ["How many customers have defaulted on loans?", ""],
              ["Do customers who default maintain a low balance?", ""],
              ["For how many customers was our last marketing campaign a success?", ""],
              ["What is the most common marital status of our customers?"]
             ],
    cache_examples=False,
    theme=gr.themes.Base(),
    concurrency_limit=8
)


demo.queue()
demo.launch(auth=("demouser", os.getenv('PASSWD')))