pentarosarium commited on
Commit
2c5fe24
·
1 Parent(s): 9f7c458

add token debugging6

Browse files
Files changed (2) hide show
  1. app.py +30 -51
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,56 +1,35 @@
1
  import streamlit as st
2
  import pandas as pd
3
- from modal import Function
4
  import os
5
- import stat
 
6
 
7
- def init_modal():
8
- """Initialize Modal with token from environment"""
9
  try:
10
- # Check if token exists in environment
11
  token = os.environ.get('MODAL_TOKEN')
12
  if not token:
13
  st.error("MODAL_TOKEN not found in environment variables")
14
- st.info("Please add MODAL_TOKEN to Hugging Face Space secrets")
15
- return False
16
 
17
- # Create token file in user's home directory
18
- token_dir = os.path.expanduser("~/.modal")
19
- token_file = os.path.join(token_dir, "token")
20
-
21
- # Create directory with proper permissions
22
- os.makedirs(token_dir, mode=0o700, exist_ok=True)
23
-
24
- # Write token file with proper permissions
25
- with open(token_file, "w") as f:
26
- f.write(token)
27
-
28
- # Set file permissions to be readable only by owner
29
- os.chmod(token_file, stat.S_IRUSR | stat.S_IWUSR)
30
-
31
- # Verify the token file exists
32
- if not os.path.exists(token_file):
33
- st.error("Failed to create token file")
34
- return False
35
-
36
- st.success("Modal token configured successfully")
37
-
38
- # Debug info
39
- if st.checkbox("Show Token File Info"):
40
- file_stat = os.stat(token_file)
41
- st.write({
42
- "Token file": token_file,
43
- "Exists": os.path.exists(token_file),
44
- "Permissions": oct(file_stat.st_mode)[-3:],
45
- "Owner": file_stat.st_uid,
46
- "Group": file_stat.st_gid
47
- })
48
-
49
- return True
50
  except Exception as e:
51
- st.error(f"Failed to initialize Modal: {str(e)}")
52
- st.exception(e) # This will show the full traceback
53
- return False
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  def main():
56
  st.title("Financial Statement Analyzer")
@@ -60,14 +39,15 @@ def main():
60
  st.write("Environment Variables:")
61
  st.write({k: "***" if k == "MODAL_TOKEN" else v
62
  for k, v in os.environ.items()})
63
- st.write("\nCurrent Working Directory:", os.getcwd())
64
- st.write("User Home:", os.path.expanduser("~"))
65
- st.write("User:", os.getenv("USER"))
66
 
67
- # Initialize Modal
68
- if not init_modal():
 
 
69
  return
70
 
 
 
71
  uploaded_files = st.file_uploader(
72
  "Choose PDF files",
73
  type="pdf",
@@ -86,8 +66,7 @@ def main():
86
  progress_bar.progress(25)
87
 
88
  # Process PDF through Modal backend
89
- pdf_processor = Function.lookup("stem", "process_pdf")
90
- financial_data = pdf_processor.remote(file)
91
  progress_bar.progress(75)
92
 
93
  if financial_data:
@@ -108,7 +87,7 @@ def main():
108
 
109
  except Exception as e:
110
  st.error("Error during processing")
111
- st.exception(e) # This will show the full traceback
112
  progress_bar.empty()
113
 
114
  if __name__ == "__main__":
 
1
  import streamlit as st
2
  import pandas as pd
 
3
  import os
4
+ from modal import Client, Function
5
+ import asyncio
6
 
7
+ def get_modal_client():
8
+ """Initialize Modal client with token"""
9
  try:
 
10
  token = os.environ.get('MODAL_TOKEN')
11
  if not token:
12
  st.error("MODAL_TOKEN not found in environment variables")
13
+ return None
 
14
 
15
+ # Create Modal client with token
16
+ client = asyncio.run(Client.from_token(token))
17
+ return client
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  except Exception as e:
19
+ st.error(f"Failed to initialize Modal client: {str(e)}")
20
+ st.exception(e)
21
+ return None
22
+
23
+ def process_pdf_with_modal(file, client):
24
+ """Process PDF using Modal with specific client"""
25
+ try:
26
+ # Use the client to get the function
27
+ pdf_processor = Function.lookup("stem", "process_pdf", client=client)
28
+ return pdf_processor.remote(file)
29
+ except Exception as e:
30
+ st.error(f"Error in Modal processing: {str(e)}")
31
+ st.exception(e)
32
+ return None
33
 
34
  def main():
35
  st.title("Financial Statement Analyzer")
 
39
  st.write("Environment Variables:")
40
  st.write({k: "***" if k == "MODAL_TOKEN" else v
41
  for k, v in os.environ.items()})
 
 
 
42
 
43
+ # Initialize Modal client
44
+ client = get_modal_client()
45
+ if client is None:
46
+ st.error("Could not initialize Modal client")
47
  return
48
 
49
+ st.success("Modal client initialized successfully")
50
+
51
  uploaded_files = st.file_uploader(
52
  "Choose PDF files",
53
  type="pdf",
 
66
  progress_bar.progress(25)
67
 
68
  # Process PDF through Modal backend
69
+ financial_data = process_pdf_with_modal(file, client)
 
70
  progress_bar.progress(75)
71
 
72
  if financial_data:
 
87
 
88
  except Exception as e:
89
  st.error("Error during processing")
90
+ st.exception(e)
91
  progress_bar.empty()
92
 
93
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1,4 +1,6 @@
1
  modal
2
  streamlit>=1.32.0
3
  pandas>=2.2.0
4
- python-dotenv>=1.0.0
 
 
 
1
  modal
2
  streamlit>=1.32.0
3
  pandas>=2.2.0
4
+ python-dotenv>=1.0.0
5
+ asyncio
6
+ aiohttp