import streamlit as st from langchain_community.llms import Ollama import pandas as pd from pandasai import SmartDataframe llm = Ollama(model="mistral") st.title("Data Analysis with PandasAI") uploader_file = st.file_uploader("Upload a file", type=["csv", "xlsx"]) if uploader_file is not None: try: # Attempt to read CSV data = pd.read_csv(uploader_file, encoding='utf-8') st.write("Preview of CSV data:") st.write(data.head(3)) except Exception as e_csv: try: # Attempt to read XLSX if CSV reading fails data = pd.read_excel(uploader_file, engine='openpyxl') st.write("Preview of XLSX data:") st.write(data.head(3)) except Exception as e_xlsx: st.write("Unable to read file. Please upload a valid CSV or XLSX file.") data = None # Set data to None if reading both CSV and XLSX fails if data is not None: # Continue with SmartDataframe and generation df = SmartDataframe(data, config={"llm" : llm}) prompt = st.text_area("Enter your prompt :") if st.button("Generate"): if prompt: with st.spinner("Generating response..."): st.write(df.chat(prompt)) else: st.warning("Please enter your prompt")