NathanPap commited on
Commit
8667f0c
·
verified ·
1 Parent(s): 12c9b45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -26
app.py CHANGED
@@ -1,31 +1,53 @@
1
  import streamlit as st
2
- from utils import query_agent
 
3
 
4
- # Basic UI
5
- st.title("CSV Analyst with Claude")
6
- st.header("Upload your CSV file for analysis")
7
 
8
- # Input Anthropic API key
9
- anthropic_api_key = st.text_input("Enter your Anthropic API Key:", type="password")
10
 
11
- # Capture the CSV file
12
- data = st.file_uploader("Upload CSV file", type="csv")
13
- query = st.text_area("What would you like to know about your data?")
14
 
15
- # Check if Anthropic API key is available
16
- if anthropic_api_key:
17
- # If Anthropic API key is available, show submit button
18
- button = st.button("Analyze")
19
-
20
- if button and data is not None:
21
- try:
22
- with st.spinner("Claude is analyzing your data..."):
23
- # Get Response
24
- answer = query_agent(data, query, api_key=anthropic_api_key)
25
- st.write(answer)
26
- except Exception as e:
27
- st.error(f"An error occurred: {str(e)}")
28
- elif button and data is None:
29
- st.warning("Please upload a CSV file first.")
30
- else:
31
- st.warning("Please input your Anthropic API key to continue.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)}")