File size: 3,050 Bytes
bbf2928
 
 
ded1fc2
 
e4c8347
 
 
37fc8c9
cba456b
 
37fc8c9
 
 
cba456b
 
 
 
 
 
 
 
37fc8c9
e4c8347
 
 
 
bbf2928
 
 
 
37fc8c9
 
 
 
 
 
e4c8347
 
 
ded1fc2
bbf2928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cba456b
bbf2928
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import streamlit as st
import pandas as pd
from modal import Function
import os

def init_modal():
    """Initialize Modal with token from environment"""
    try:
        # Check if token exists in environment
        token = os.environ.get('MODAL_TOKEN')
        if not token:
            st.error("MODAL_TOKEN not found in environment variables")
            st.info("Please add MODAL_TOKEN to Hugging Face Space secrets")
            return False
            
        # Create token file
        token_dir = os.path.expanduser("~/.modal")
        os.makedirs(token_dir, exist_ok=True)
        
        with open(os.path.join(token_dir, "token"), "w") as f:
            f.write(token)
            
        st.success("Modal token configured successfully")
        return True
    except Exception as e:
        st.error(f"Failed to initialize Modal: {str(e)}")
        return False

def main():
    st.title("Financial Statement Analyzer")
    
    # Show environment info (for debugging)
    if st.checkbox("Show Debug Info"):
        st.write("Environment Variables:")
        st.write({k: "***" if k == "MODAL_TOKEN" else v 
                 for k, v in os.environ.items()})
    
    # Initialize Modal
    if not init_modal():
        return
    
    uploaded_files = st.file_uploader(
        "Choose PDF files", 
        type="pdf", 
        accept_multiple_files=True,
        help="Upload Consolidated Financial Statements in Russian"
    )
    
    if uploaded_files:
        for file in uploaded_files:
            with st.expander(f"Processing {file.name}", expanded=True):
                progress_bar = st.progress(0)
                status = st.empty()
                
                try:
                    status.info("Starting PDF processing...")
                    progress_bar.progress(25)
                    
                    # Process PDF through Modal backend
                    pdf_processor = Function.lookup("stem", "process_pdf")
                    financial_data = pdf_processor.remote(file)
                    progress_bar.progress(75)
                    
                    if financial_data:
                        # Display results in tabs
                        tab1, tab2 = st.tabs(["Financial Ratios", "Raw Data"])
                        
                        with tab1:
                            st.subheader("Key Financial Ratios")
                            st.dataframe(pd.DataFrame([financial_data]))
                            
                        with tab2:
                            st.subheader("Extracted Financial Data")
                            st.json(financial_data)
                            
                        status.success("Processing complete!")
                    else:
                        status.error("Failed to extract financial data")
                        
                except Exception as e:
                    st.error(f"Error during processing: {str(e)}")
                    progress_bar.empty()

if __name__ == "__main__":
    main()