File size: 1,755 Bytes
9a6940b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)}")