File size: 1,476 Bytes
0da93dc
30af771
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0da93dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""functions"""

def load_file():
    return "Loading..."

def load_xlsx(name):
    import pandas as pd
    
    xls_file = rf'{name}'
    data = pd.read_excel(xls_file)  
    return data

def table_loader(table_file, open_ai_key):
    import os
    from langchain.llms import OpenAI
    from langchain.agents import create_pandas_dataframe_agent    
    from pandas import read_csv
    
    global agent
    if open_ai_key is not None:
        os.environ['OPENAI_API_KEY'] = open_ai_key
    else:
        return "Enter API"
    
    if table_file.name.endswith('.xlsx') or table_file.name.endswith('.xls'):
        data = load_xlsx(table_file.name)
        agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
        return "Ready!"
    elif table_file.name.endswith('.csv'):
        data = read_csv(table_file.name)
        agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
        return "Ready!"
    else:
        return "Wrong file format! Upload excel file or csv!"
    
def run(query):
    from langchain.callbacks import get_openai_callback
    
    with get_openai_callback() as cb:
        response = (agent.run(query))
        costs = (f"Total Cost (USD): ${cb.total_cost}")
        output = f'{response} \n {costs}'
        return output
    
def respond(message, chat_history):
    import time
    
    bot_message = run(message)
    chat_history.append((message, bot_message))
    time.sleep(0.5)
    return "", chat_history