File size: 3,594 Bytes
d62d989
 
 
 
 
 
d18946c
d62d989
 
 
d18946c
 
 
 
 
 
 
 
4f9109c
d62d989
 
d18946c
d62d989
7602d3b
d62d989
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87e89d8
 
3354acb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87e89d8
 
d74323c
d62d989
 
 
91b1f9f
cca8beb
 
 
ccb73d5
 
 
0efc604
d92b39e
beae6b6
 
e70ffe8
d62d989
 
 
0efc604
e17b724
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
84
85
86
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, AzureChatOpenAI

from sklearn.datasets import fetch_openml

gpt4o_azure = AzureChatOpenAI(
    model_name='gpt-4o-mini',
    api_key=os.environ["AZURE_OPENAI_KEY"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_version="2024-02-01",
    temperature=0
)

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

pandas_agent = create_pandas_dataframe_agent(
    llm=gpt4o_azure,
    df=bank_data,
    verbose=True,
    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="origin",
#    theme=gr.themes.Base(),
    concurrency_limit=8
)


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