Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
from io import StringIO
|
| 7 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 8 |
+
|
| 9 |
+
# Set API token
|
| 10 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.getenv("hf")
|
| 11 |
+
os.environ['HF_TOKEN'] = os.getenv("hf")
|
| 12 |
+
|
| 13 |
+
st.title("π DataCraft CSV")
|
| 14 |
+
st.subheader("β Crafting insights from structured data")
|
| 15 |
+
|
| 16 |
+
# Session state for chat history
|
| 17 |
+
if "chat_history" not in st.session_state:
|
| 18 |
+
st.session_state.chat_history = []
|
| 19 |
+
|
| 20 |
+
# Upload CSV
|
| 21 |
+
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
|
| 22 |
+
|
| 23 |
+
if uploaded_file:
|
| 24 |
+
df = pd.read_csv(uploaded_file)
|
| 25 |
+
st.success("β
File loaded successfully!")
|
| 26 |
+
st.subheader("π Quick Summary")
|
| 27 |
+
|
| 28 |
+
st.write("**Shape:**", df.shape)
|
| 29 |
+
st.write("**Columns:**", df.columns.tolist())
|
| 30 |
+
st.write("**Missing Values:**")
|
| 31 |
+
st.dataframe(df.isnull().sum())
|
| 32 |
+
st.write("**Data Types:**")
|
| 33 |
+
st.dataframe(df.dtypes)
|
| 34 |
+
|
| 35 |
+
st.subheader("π¬ Ask a question about the dataset")
|
| 36 |
+
|
| 37 |
+
user_input = st.text_input("E.g. 'What are the average values?', 'Plot sales over time'")
|
| 38 |
+
|
| 39 |
+
# Hugging Face Model Setup
|
| 40 |
+
deepseek = HuggingFaceEndpoint(
|
| 41 |
+
repo_id="deepseek-ai/DeepSeek-R1",
|
| 42 |
+
provider="nebius",
|
| 43 |
+
temperature=0.5,
|
| 44 |
+
max_new_tokens=150,
|
| 45 |
+
task="conversational"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
model = ChatHuggingFace(
|
| 49 |
+
llm=deepseek,
|
| 50 |
+
repo_id=deepseek.repo_id,
|
| 51 |
+
provider=deepseek.provider,
|
| 52 |
+
temperature=0.5,
|
| 53 |
+
max_new_tokens=150,
|
| 54 |
+
task="conversational"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
if user_input:
|
| 58 |
+
df_sample = df.head(50).to_csv(index=False)
|
| 59 |
+
|
| 60 |
+
prompt = f"""
|
| 61 |
+
You are a helpful data analyst. Here's a preview of the dataset and a user question. Provide an answer in plain English. If the question mentions plotting, include the code as well.
|
| 62 |
+
Dataset:
|
| 63 |
+
{df_sample}
|
| 64 |
+
User question: {user_input}
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
with st.spinner("Thinking..."):
|
| 68 |
+
try:
|
| 69 |
+
response = model.invoke([{"role": "user", "content": prompt}])
|
| 70 |
+
result = response.content if hasattr(response, "content") else response
|
| 71 |
+
st.session_state.chat_history.append((user_input, result))
|
| 72 |
+
|
| 73 |
+
st.markdown("### π§ Answer")
|
| 74 |
+
st.write(result)
|
| 75 |
+
|
| 76 |
+
# Optional: Execute simple plot command if mentioned
|
| 77 |
+
if "plot" in user_input.lower():
|
| 78 |
+
with st.expander("π Try plotting automatically"):
|
| 79 |
+
try:
|
| 80 |
+
# Try simple detection for column plots
|
| 81 |
+
cols = df.select_dtypes(include='number').columns.tolist()
|
| 82 |
+
if len(cols) >= 2:
|
| 83 |
+
fig, ax = plt.subplots()
|
| 84 |
+
sns.lineplot(data=df, x=cols[0], y=cols[1], ax=ax)
|
| 85 |
+
st.pyplot(fig)
|
| 86 |
+
else:
|
| 87 |
+
st.info("Could not find enough numeric columns to plot.")
|
| 88 |
+
except Exception as e:
|
| 89 |
+
st.error(f"Plotting failed: {e}")
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
st.error(f"Error: {e}")
|
| 93 |
+
|
| 94 |
+
# Display previous chat history
|
| 95 |
+
if st.session_state.chat_history:
|
| 96 |
+
st.subheader("π Previous Q&A")
|
| 97 |
+
for q, a in st.session_state.chat_history:
|
| 98 |
+
st.markdown(f"**You:** {q}")
|
| 99 |
+
st.markdown(f"**Bot:** {a}")
|