New_Space / app.py
NathanPap's picture
Create app.py
9a6940b verified
raw
history blame
1.76 kB
import streamlit as st
import pandas as pd
import anthropic
st.title("CSV Analyzer with Claude")
# API Key input
api_key = st.text_input("Enter your Anthropic API Key (starts with 'sk-'):", type="password")
# File upload
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file is not None and api_key:
try:
# Read CSV
df = pd.read_csv(uploaded_file)
st.write("Data Preview:")
st.dataframe(df.head())
# Question input
question = st.text_input("What would you like to know about this data?")
if question and st.button("Analyze"):
try:
# Create base Anthropic client
client = anthropic.Client(api_key=api_key)
# Prepare content
data_info = {
"columns": list(df.columns),
"sample": df.head(3).to_dict('records')
}
# Create message
completion = client.messages.create(
max_tokens=1024,
messages=[{
"role": "user",
"content": f"Analyze this CSV data: {data_info}\n\nQuestion: {question}"
}],
model="claude-3.5-sonnet-20241022"
)
# Display response
st.write("Analysis:")
st.write(completion.content)
except Exception as e:
st.error(f"Analysis error: {str(e)}")
st.write("Error details:", e.__dict__)
except Exception as e:
st.error(f"File error: {str(e)}")