Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import anthropic
|
4 |
+
|
5 |
+
st.title("CSV Analyzer with Claude")
|
6 |
+
|
7 |
+
# API Key input
|
8 |
+
api_key = st.text_input("Enter your Anthropic API Key (starts with 'sk-'):", type="password")
|
9 |
+
|
10 |
+
# File upload
|
11 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
12 |
+
|
13 |
+
if uploaded_file is not None and api_key:
|
14 |
+
try:
|
15 |
+
# Read CSV
|
16 |
+
df = pd.read_csv(uploaded_file)
|
17 |
+
st.write("Data Preview:")
|
18 |
+
st.dataframe(df.head())
|
19 |
+
|
20 |
+
# Question input
|
21 |
+
question = st.text_input("What would you like to know about this data?")
|
22 |
+
|
23 |
+
if question and st.button("Analyze"):
|
24 |
+
try:
|
25 |
+
# Create base Anthropic client
|
26 |
+
client = anthropic.Client(api_key=api_key)
|
27 |
+
|
28 |
+
# Prepare content
|
29 |
+
data_info = {
|
30 |
+
"columns": list(df.columns),
|
31 |
+
"sample": df.head(3).to_dict('records')
|
32 |
+
}
|
33 |
+
|
34 |
+
# Create message
|
35 |
+
completion = client.messages.create(
|
36 |
+
max_tokens=1024,
|
37 |
+
messages=[{
|
38 |
+
"role": "user",
|
39 |
+
"content": f"Analyze this CSV data: {data_info}\n\nQuestion: {question}"
|
40 |
+
}],
|
41 |
+
model="claude-3.5-sonnet-20241022"
|
42 |
+
)
|
43 |
+
|
44 |
+
# Display response
|
45 |
+
st.write("Analysis:")
|
46 |
+
st.write(completion.content)
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
st.error(f"Analysis error: {str(e)}")
|
50 |
+
st.write("Error details:", e.__dict__)
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
st.error(f"File error: {str(e)}")
|