|
import streamlit as st |
|
import pandas as pd |
|
from pandasai import SmartDataframe |
|
from pandasai.llm.google_gemini import GoogleGemini |
|
import warnings |
|
from pandasai.responses.response_parser import ResponseParser |
|
import os |
|
from huggingface_hub import hf_hub_download |
|
|
|
class StreamlitResponse(ResponseParser): |
|
def __init__(self, context) -> None: |
|
super().__init__(context) |
|
|
|
def format_dataframe(self, result): |
|
st.dataframe(result["value"]) |
|
return |
|
|
|
def format_plot(self, result): |
|
st.image(result["value"]) |
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore") |
|
|
|
repo_id = "Akankshg/ML_DATA" |
|
filename = "EDA_DATA.parquet" |
|
|
|
|
|
token = os.environ["HUGGING_FACE_HUB_TOKEN"] |
|
|
|
|
|
local_file = hf_hub_download(repo_id=repo_id, filename=filename,repo_type="dataset", token=token) |
|
|
|
|
|
@st.cache_data() |
|
def fetch_data(): |
|
|
|
data = pd.read_parquet(local_file) |
|
return data |
|
|
|
df = fetch_data() |
|
|
|
|
|
google_api_key = os.environ.get("GOOGLE_API_KEY") |
|
|
|
|
|
llm = GoogleGemini(api_key=google_api_key) |
|
|
|
pandas_ai = SmartDataframe(df, config={"llm": llm, "response_parser": StreamlitResponse,"verbose": True}) |
|
pandas_ai_2 = SmartDataframe(df, config={"llm": llm,"verbose": True}) |
|
|
|
st.title("AI-Powered Data Analysis App") |
|
st.write("This application allows you to interact with your dataset using natural language prompts. Just ask a question, and the AI will provide insights based on your data.") |
|
|
|
|
|
st.subheader("Dataset Preview") |
|
st.dataframe(df.head()) |
|
|
|
|
|
prompt = st.text_input("Enter your prompt:", placeholder="e.g., What are the top diagnoses?") |
|
|
|
|
|
if st.button("Submit"): |
|
if 'plot' in prompt or 'graph' in prompt or 'PLOT' in prompt or 'Graph' in prompt: |
|
try: |
|
result = pandas_ai.chat(prompt) |
|
st.subheader("Result") |
|
except KeyError as e: |
|
st.error(f"Error: {e}. Unable to retrieve result.") |
|
elif prompt: |
|
try: |
|
result = pandas_ai_2.chat(prompt) |
|
st.subheader("Result") |
|
st.write(result) |
|
except KeyError as e: |
|
st.error(f"Error: {e}. Unable to retrieve result.") |
|
else: |
|
st.warning("Please enter a prompt.") |
|
|
|
|
|
st.write("Powered by PandasAI and Google Gemini.") |
|
|