Download app.py from richterts/Excel_GPT: direct link, hf CLI and curl.
- Browser
- Download file 4.06 kB
-
https://huggingface.co/spaces/richterts/Excel_GPT/resolve/main/app.py
- Command line
-
hf download hf://spaces/richterts/Excel_GPT/app.py
-
curl -L -o app.py https://huggingface.co/spaces/richterts/Excel_GPT/resolve/main/app.py
4.06 kB
| import os | |
| import openai | |
| from langchain.agents import create_csv_agent | |
| from langchain.llms import OpenAI | |
| from langchain.document_loaders.csv_loader import CSVLoader | |
| import gradio as gr | |
| openai.api_key = os.environ["OPENAI_API_KEY"] | |
| #Dataset - Black Friday Sales | |
| import pandas as pd | |
| df = pd.read_csv('./content/train.csv') | |
| df.head() | |
| #CSV Agent | |
| #note: this agent calls the Pandas DataFrame agent under the hood, which in turn calls the Python agent, which executes LLM generated | |
| # Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously. | |
| agent = create_csv_agent(OpenAI(temperature=0), | |
| './content/train.csv', | |
| verbose=True) | |
| agent | |
| agent.agent.llm_chain.prompt.template | |
| #You are working with a pandas dataframe in Python. The name of the dataframe is df. You should use the tools below to answer the question posed of you: | |
| #python_repl_ast: A Python shell. Use this to execute python commands. Input should be a valid python command. When using this tool, sometimes output is abbreviated - make sure it does not look abbreviated before using it in your answer. | |
| #Use the following format: | |
| #Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [python_repl_ast] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question | |
| #This is the result of print(df.head()): {df} | |
| #Begin! Question: {input} {agent_scratchpad} | |
| print ('Response: ' + agent.run("how many rows are there?")) | |
| #agent.run("how many people are female?") | |
| #agent.run("how many people have stayed more than 3 years in the city?") | |
| #agent.run("how many people have stayed more than 3 years in the city and are female?") | |
| #agent.run("Are there more males or females?") | |
| #LangChain CSV Loader | |
| loader = CSVLoader(file_path='./content/train.csv') | |
| data = loader.load() | |
| #Convert 1 XLS file to csv | |
| xls_file = r'./content/excel_file_example.xlsx' | |
| output_csv = r'./content/excel_file_example.csv' | |
| # Read the XLS file using pandas and openpyxl as the engine | |
| data = pd.read_excel(xls_file, engine='openpyxl') | |
| # Save the data as a CSV file | |
| data.to_csv(output_csv, index=False) | |
| #Convert 1 | |
| #Convert 2 XLS file to csv | |
| xls_file2 = r'./content/marea_negra_2022.xlsx' | |
| output_csv2 = r'./content/marea_negra_2022.csv' | |
| # Read the XLS file using pandas and openpyxl as the engine | |
| data = pd.read_excel(xls_file2, engine='openpyxl') | |
| # Save the data as a CSV file | |
| data.to_csv(output_csv2, index=False) | |
| #Convert 2 | |
| #Calling agent convert 1 | |
| agent = create_csv_agent(OpenAI(temperature=0), | |
| './content/excel_file_example.csv', | |
| verbose=True) | |
| #Calling agent convert 2 | |
| agent2 = create_csv_agent(OpenAI(temperature=0), | |
| './content/marea_negra_2022.csv', | |
| verbose=True) | |
| #Calling agent convert 3 Tabs From Original File | |
| agent3 = create_csv_agent(OpenAI(temperature=0), | |
| './content/marea_negra_2022_clientes.csv', | |
| verbose=True) | |
| #Calling agent convert 4 Tabs From Original File | |
| agent4 = create_csv_agent(OpenAI(temperature=0), | |
| './content/Empresas Base de prueba.csv', | |
| verbose=True) | |
| #agent.run("What are the column names?") | |
| #agent3.run("Dame la lista de clientes ") | |
| #agent3.run("Dame la lista de clientes ya contactados ") | |
| #agent3.run("Dame la lista de clientes en prospeccion") | |
| #agent4.run("Dame las ventas totales de agosto 2022") | |
| #agent.run("Which country appears the most and how many times does it appear?") | |
| #agent.run("What is the ratio of males to females?") | |
| #First Interface | |
| def random_response(message, history): | |
| query = message | |
| agent4.run(query) | |
| return agent4.run(query) | |
| gr.ChatInterface(random_response).launch(share=False) | |
| #First Interface | |