Spaces:
Runtime error

File size: 1,386 Bytes
71abe1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")