Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import plotly.express as px | |
from pandasai import Agent | |
from langchain_community.embeddings.openai import OpenAIEmbeddings | |
from langchain_community.vectorstores import FAISS | |
from langchain_openai import ChatOpenAI | |
from langchain.chains import RetrievalQA | |
from langchain.schema import Document | |
import os | |
# ์ ๋ชฉ ์ค์ | |
st.title("PandasAI ๋ฐ์ดํฐ ๋ถ์๊ธฐ with RAG") | |
# ์ฌ์ด๋๋ฐ์ API ํค ์ ๋ ฅ ํ๋ ์ถ๊ฐ | |
api_key = st.sidebar.text_input("OpenAI API Key", type="password") | |
pandasai_api_key = st.sidebar.text_input("PandasAI API Key", type="password") | |
# ํ์ผ ์ ๋ก๋ | |
uploaded_file = st.file_uploader("์์ ๋๋ CSV ํ์ผ์ ์ ๋ก๋ํ์ธ์", type=["xlsx", "csv"]) | |
if uploaded_file is not None and api_key and pandasai_api_key: | |
# API ํค ์ค์ | |
os.environ["OPENAI_API_KEY"] = api_key | |
os.environ["PANDASAI_API_KEY"] = pandasai_api_key | |
# ๋ฐ์ดํฐ ๋ก๋ | |
if uploaded_file.name.endswith('.xlsx'): | |
df = pd.read_excel(uploaded_file) | |
else: | |
df = pd.read_csv(uploaded_file) | |
st.write("๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:") | |
st.write(df.head()) | |
# PandasAI Agent ์ค์ | |
agent = Agent(df) | |
# ๋ฐ์ดํฐํ๋ ์์ ๋ฌธ์๋ก ๋ณํ | |
documents = [ | |
Document( | |
page_content=", ".join([f"{col}: {row[col]}" for col in df.columns]), | |
metadata={"index": index} | |
) | |
for index, row in df.iterrows() | |
] | |
# RAG ์ค์ | |
embeddings = OpenAIEmbeddings() | |
vectorstore = FAISS.from_documents(documents, embeddings) | |
retriever = vectorstore.as_retriever() | |
qa_chain = RetrievalQA.from_chain_type( | |
llm=ChatOpenAI(), | |
chain_type="stuff", | |
retriever=retriever | |
) | |
# ํญ ์์ฑ | |
tab1, tab2, tab3 = st.tabs(["PandasAI ๋ถ์", "RAG ์ง๋ฌธ๋ต๋ณ", "๋ฐ์ดํฐ ์๊ฐํ"]) | |
with tab1: | |
st.header("PandasAI๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ถ์") | |
pandas_question = st.text_input("๋ฐ์ดํฐ์ ๋ํด ์ง๋ฌธํ์ธ์ (PandasAI):") | |
if pandas_question: | |
result = agent.chat(pandas_question) | |
st.write("PandasAI ๋ต๋ณ:", result) | |
with tab2: | |
st.header("RAG๋ฅผ ์ฌ์ฉํ ์ง๋ฌธ๋ต๋ณ") | |
rag_question = st.text_input("๋ฐ์ดํฐ์ ๋ํด ์ง๋ฌธํ์ธ์ (RAG):") | |
if rag_question: | |
result = qa_chain.run(rag_question) | |
st.write("RAG ๋ต๋ณ:", result) | |
with tab3: | |
st.header("๋ฐ์ดํฐ ์๊ฐํ") | |
viz_question = st.text_input("์ด๋ค ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฌ๊ณ ์ถ์ผ์ ๊ฐ์? (์: '์ฐ๋ด๊ณผ ๊ฒฝ๋ ฅ์ ๊ด๊ณ๋ฅผ ์ฐ์ ๋๋ก ๋ณด์ฌ์ค')") | |
if viz_question: | |
try: | |
result = agent.chat(viz_question) | |
# PandasAI์ ๊ฒฐ๊ณผ๊ฐ ๋ฌธ์์ด์ด๋ฏ๋ก, ์ด๋ฅผ ์คํ ๊ฐ๋ฅํ ์ฝ๋๋ก ๋ณํ | |
import re | |
code_pattern = r'```python\n(.*?)\n```' | |
code_match = re.search(code_pattern, result, re.DOTALL) | |
if code_match: | |
viz_code = code_match.group(1) | |
# 'plt' ๋์ 'px'๋ฅผ ์ฌ์ฉํ๋๋ก ์ฝ๋ ์์ | |
viz_code = viz_code.replace('plt.', 'px.') | |
viz_code = viz_code.replace('plt.show()', 'fig = px.scatter(df, x=x, y=y)') | |
# ์ฝ๋ ์คํ ๋ฐ ๊ทธ๋ํ ํ์ | |
exec(viz_code) | |
st.plotly_chart(fig) | |
else: | |
st.write("๊ทธ๋ํ๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค๋ฅธ ์ง๋ฌธ์ ํด๋ณด์ธ์.") | |
except Exception as e: | |
st.write(f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}") | |
st.write("๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ์ง๋ฌธํด๋ณด์ธ์.") | |
elif not api_key: | |
st.warning("OpenAI API ํค๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์.") | |
elif not pandasai_api_key: | |
st.warning("PandasAI API ํค๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์.") |