| from langchain.agents import create_csv_agent |
| from langchain.chat_models import ChatOpenAI |
| from langchain.agents.agent_types import AgentType |
| import os |
| import pandas as pd |
| import streamlit as st |
| from dotenv import load_dotenv |
| import tempfile |
|
|
| def main(): |
| |
| load_dotenv() |
|
|
| |
| openai_api_key = os.getenv("OPENAI_API_KEY") |
|
|
| |
| if openai_api_key is None or openai_api_key == "Financial Agent": |
| st.error("OpenAI API Key is not set in the environment. Please check your environment variables.") |
| return |
| else: |
| st.success("Financial Agent Started ") |
|
|
| |
| csv_file = st.file_uploader("Upload a CSV file", type="csv") |
|
|
| |
| if csv_file is not None: |
| try: |
| |
| df = pd.read_csv(csv_file, encoding='ISO-8859-1') |
| st.write("Preview of the CSV file:") |
| st.dataframe(df.head()) |
| except Exception as e: |
| st.error(f"Error reading the CSV file: {str(e)}") |
| return |
|
|
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp_file: |
| tmp_file.write(csv_file.getbuffer()) |
| tmp_file_path = tmp_file.name |
|
|
| |
| agent = create_csv_agent( |
| ChatOpenAI(temperature=0, model="gpt-4-turbo"), |
| tmp_file_path, |
| verbose=True, |
| agent_type=AgentType.OPENAI_FUNCTIONS, |
| ) |
|
|
| |
| user_question = st.text_input("Ask a question:") |
|
|
| |
| if user_question and user_question.strip(): |
| with st.spinner(text="In progress..."): |
| try: |
| response = agent.run(user_question) |
| st.write(response) |
| except Exception as e: |
| st.error(f"Error while processing your request: {str(e)}") |
| else: |
| st.warning("Please enter a valid question.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|
|
|
|
|