Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| from io import StringIO | |
| from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace | |
| os.environ["HF_TOKEN"]=os.getenv('HF_Token') | |
| os.environ["HUGGINGFACEHUB_API_KEY"]=os.getenv('HF_Token') | |
| st.set_page_config(page_title="InsightGenie β AI-Powered CSV Explorer", layout="wide") | |
| st.title("π§ InsightGenie") | |
| st.markdown("**Explore your CSV like magic β Ask, analyze, and visualize with AI.**") | |
| if "qa_conversations" not in st.session_state: | |
| st.session_state.qa_conversations = [] | |
| uploaded_csv = st.file_uploader("π Upload your CSV file to begin", type=["csv"]) | |
| if uploaded_csv: | |
| try: | |
| data = pd.read_csv(uploaded_csv) | |
| st.success("β Data loaded successfully!") | |
| st.header("π Dataset Overview") | |
| st.markdown(f"- **Rows and Columns:** {data.shape[0]} rows Γ {data.shape[1]} columns") | |
| st.markdown("**π Column Names:**") | |
| st.write(data.columns.tolist()) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.markdown("**π§© Missing Values**") | |
| st.dataframe(data.isnull().sum(), height=200) | |
| with col2: | |
| st.markdown("**π’ Data Types**") | |
| st.dataframe(data.dtypes, height=200) | |
| except Exception as e: | |
| st.error(f"β Failed to read the file: {e}") | |
| st.stop() | |
| st.header("π¬ Ask InsightGenie") | |
| user_question = st.text_input("Type your question about the dataset here:") | |
| genie_endpoint = HuggingFaceEndpoint( | |
| repo_id="deepseek-ai/DeepSeek-R1", | |
| provider="nebius", | |
| temperature=0.5, | |
| max_new_tokens=150, | |
| task="conversational" | |
| ) | |
| genie_chatbot = ChatHuggingFace( | |
| llm=genie_endpoint, | |
| repo_id=genie_endpoint.repo_id, | |
| provider=genie_endpoint.provider, | |
| temperature=0.5, | |
| max_new_tokens=150, | |
| task="conversational" | |
| ) | |
| if user_question: | |
| sample_data = data.head(50).to_csv(index=False) | |
| prompt = f""" | |
| You are a skilled data assistant named InsightGenie. A user has uploaded a dataset and asked a question. | |
| Answer clearly. If the question involves charts or graphs, provide appropriate Python code using matplotlib or seaborn. | |
| Hereβs a preview of the dataset: | |
| {sample_data} | |
| User question: | |
| {user_question} | |
| """ | |
| with st.spinner("π Generating response..."): | |
| try: | |
| model_response = genie_chatbot.invoke([{"role": "user", "content": prompt}]) | |
| bot_reply = model_response.content if hasattr(model_response, "content") else model_response | |
| st.session_state.qa_conversations.append((user_question, bot_reply)) | |
| st.markdown("### π§ Genie Says") | |
| st.write(bot_reply) | |
| # Auto-plot for simple queries | |
| if "plot" in user_question.lower(): | |
| with st.expander("π Auto-generated Plot"): | |
| try: | |
| numeric_cols = data.select_dtypes(include='number').columns.tolist() | |
| if len(numeric_cols) >= 2: | |
| fig, ax = plt.subplots() | |
| sns.lineplot(data=data, x=numeric_cols[0], y=numeric_cols[1], ax=ax) | |
| ax.set_title(f"{numeric_cols[1]} vs {numeric_cols[0]}") | |
| st.pyplot(fig) | |
| else: | |
| st.info("β οΈ Not enough numeric columns to generate a plot.") | |
| except Exception as e: | |
| st.error(f"β Plotting error: {e}") | |
| except Exception as e: | |
| st.error(f"β Error generating AI response: {e}") | |
| if st.session_state.qa_conversations: | |
| st.header("π Chat History") | |
| for user_q, ai_a in reversed(st.session_state.qa_conversations): | |
| st.markdown(f"**π§βπ» You:** {user_q}") | |
| st.markdown(f"**π€ InsightGenie:** {ai_a}") | |
| st.markdown("---") | |