llepogam commited on
Commit
d75f942
·
1 Parent(s): 76ff9fc

update S3 bucket

Browse files
Files changed (1) hide show
  1. app.py +41 -16
app.py CHANGED
@@ -7,6 +7,7 @@ import requests
7
  from datetime import datetime
8
  import time
9
  import os
 
10
 
11
 
12
  ### Config
@@ -16,30 +17,54 @@ st.set_page_config(
16
  layout="wide"
17
  )
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # File path for history
20
  HISTORY_FILE = "https://llepogam-app-history.s3.eu-north-1.amazonaws.com/history.csv"
21
 
22
- # Initialize session state and load history
23
- if 'history' not in st.session_state:
24
- if os.path.exists(HISTORY_FILE):
25
- try:
26
- history_df = pd.read_csv(HISTORY_FILE)
27
- st.session_state.history = history_df.to_dict('records')
28
- except Exception as e:
29
- st.error(f"Error loading history: {str(e)}")
30
- st.session_state.history = []
31
- else:
32
- st.session_state.history = []
33
-
34
-
35
 
36
  def save_history():
37
- """Save history to CSV file"""
38
  try:
39
  history_df = pd.DataFrame(st.session_state.history)
40
- history_df.to_csv(HISTORY_FILE, index=False)
 
 
 
 
 
41
  except Exception as e:
42
- st.error(f"Error saving history: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Custom CSS
45
  st.markdown("""
 
7
  from datetime import datetime
8
  import time
9
  import os
10
+ import boto3
11
 
12
 
13
  ### Config
 
17
  layout="wide"
18
  )
19
 
20
+
21
+
22
+ # Initialize AWS session with credentials from Hugging Face secrets
23
+ session = boto3.Session(
24
+ aws_access_key_id=st.secrets["AWS_ACCESS_KEY_ID"],
25
+ aws_secret_access_key=st.secrets["AWS_SECRET_ACCESS_KEY"],
26
+ )
27
+
28
+ # Initialize S3 resource
29
+ s3 = session.resource("s3")
30
+ bucket_name = 'llepogam-app-history'
31
+ bucket = s3.Bucket(bucket_name)
32
+
33
+
34
  # File path for history
35
  HISTORY_FILE = "https://llepogam-app-history.s3.eu-north-1.amazonaws.com/history.csv"
36
 
37
+
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  def save_history():
40
+ """Save history to S3"""
41
  try:
42
  history_df = pd.DataFrame(st.session_state.history)
43
+ # Save to temporary file first
44
+ history_df.to_csv("/tmp/temp_history.csv", index=False)
45
+ # Upload to S3
46
+ bucket.upload_file("/tmp/temp_history.csv", "history.csv")
47
+ # Clean up temp file
48
+ os.remove("/tmp/temp_history.csv")
49
  except Exception as e:
50
+ st.error(f"Error saving history to S3: {str(e)}")
51
+
52
+ def load_history():
53
+ """Load history from S3"""
54
+ try:
55
+ # Download from S3 to temporary file
56
+ bucket.download_file("history.csv", "/tmp/temp_history.csv")
57
+ # Read the CSV
58
+ history_df = pd.read_csv("/tmp/temp_history.csv")
59
+ # Clean up temp file
60
+ os.remove("/tmp/temp_history.csv")
61
+ return history_df.to_dict('records')
62
+ except Exception as e:
63
+ st.error(f"Error loading history from S3: {str(e)}")
64
+ return []
65
+
66
+ if 'history' not in st.session_state:
67
+ st.session_state.history = load_history()
68
 
69
  # Custom CSS
70
  st.markdown("""