| import streamlit as st |
| import pandas as pd |
| import anthropic |
|
|
| st.title("CSV Analyzer with Claude") |
|
|
| |
| api_key = st.text_input("Enter your Anthropic API Key (starts with 'sk-'):", type="password") |
|
|
| |
| uploaded_file = st.file_uploader("Choose a CSV file", type="csv") |
|
|
| if uploaded_file is not None and api_key: |
| try: |
| |
| df = pd.read_csv(uploaded_file) |
| st.write("Data Preview:") |
| st.dataframe(df.head()) |
| |
| |
| question = st.text_input("What would you like to know about this data?") |
| |
| if question and st.button("Analyze"): |
| try: |
| |
| client = anthropic.Client(api_key=api_key) |
| |
| |
| data_info = { |
| "columns": list(df.columns), |
| "sample": df.head(3).to_dict('records') |
| } |
| |
| |
| 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" |
| ) |
| |
| |
| 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)}") |