import streamlit as st
from utils import get_api_key, get_response

def main():

    st.header('Welcome to SciDocuParse! 🧑‍🔬📚')
    st.write('A scientific document parser, particularly specializing in graph analysis 📊 and data interpretation 🔍.')

    st.session_state["thought_process"] = ""
    st.session_state["response"] = ""

    with st.sidebar:
        st.header('SciDocuParse Sidebar 🔧')
        st.caption('A tool to help you analyze scientific papers and documents efficiently! 📝')
        
        paper = st.text_area('Paste scientific document citation here 🧑‍🏫',
                                        """@article{wang2020automated,
        title={Automated diabetic retinopathy grading and lesion detection based on the modified R-FCN object-detection algorithm},
        author={Wang, Jialiang and Luo, Jianxu and Liu, Bin and Feng, Rui and Lu, Lina and Zou, Haidong},
        journal={IET Computer Vision},
        volume={14},
        number={1},
        pages={1--8},
        year={2020},
        publisher={Wiley Online Library}
        }""", height=350, help='Paste your document citation in BiBtex format.')
    
        if not paper:
            st.error('Provide a citation first! ⚠️')

        user_prompt = st.text_area("Enter your query for analysis 🔍:", 
                                    "Summarize this document and highlight key findings in graphs 📈")

        persona = "You are a master Scientific Graph Analyzer skilled in interpreting graphs across all fields. Analyze trends (linear/exponential growth, correlations, outliers) and statistical patterns (mean, variance). Summarize key findings in plain language, Expalin data about causality, anomalies, or data limitations. Prioritize clarity: ensure outputs are accessible to technical and non-technical audiences. Combine technical precision with intuitive communication to deliver accurate, user-friendly interpretations."

        user_prompt = persona + paper + user_prompt

        # api_key = get_api_key()
        if st.button('Analyze with LLM 🚀'):
            with st.spinner('Processing your document...'):
                api_key = get_api_key()

                thought_process, response = get_response(user_prompt, api_key) # uncommenting it to save tokens

                st.session_state["thought_process"] = thought_process
                st.session_state["response"] = response
    
    if "thought_process" in st.session_state and "response" in st.session_state:
        if len(st.session_state["thought_process"]) >= 1 and len(st.session_state["response"]) >= 1:
            with st.expander('Show thought process 💭'):
                st.caption(thought_process)
            
            st.subheader('RESPONSE 📝')
            st.write(response)
            
    st.caption('SciDocuParse is made by John Manuel Carado')
    st.caption('Intelligent Systems course in WVSU - CICT, Midterm Requirement')
        
if __name__ == '__main__':
    main()