maria355 commited on
Commit
d64cc8e
Β·
verified Β·
1 Parent(s): 9e269fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -116
app.py CHANGED
@@ -925,128 +925,128 @@ def main():
925
  form_response = tax_agent_response(form_question, tax_form_type=form_type)
926
  st.markdown(form_response)
927
  # Tab 3: Upload Form
928
- with tab3:
929
- st.header("Upload Your Tax Form")
930
- st.markdown("Upload a PDF tax form to analyze, get summaries, and ask questions about it.")
931
-
932
- # File uploader
933
- uploaded_file = st.file_uploader(
934
- "Choose a PDF file",
935
- type=['pdf'],
936
- help="Upload any Pakistani tax form PDF for analysis"
937
- )
938
-
939
- if uploaded_file is not None:
940
- # Store uploaded file in session state
941
- st.session_state.pdf_bytes = BytesIO(uploaded_file.read())
942
- st.session_state.uploaded_file_name = uploaded_file.name
943
-
944
- # Display success message
945
- st.success(f"βœ… Successfully uploaded: {uploaded_file.name}")
946
-
947
- # Extract form fields from uploaded PDF
948
- with st.spinner("Analyzing uploaded form..."):
949
- st.session_state.form_fields = extract_form_fields(st.session_state.pdf_bytes)
950
-
951
- # Display PDF preview
952
- st.markdown("### πŸ“„ Form Preview")
953
- display_pdf(st.session_state.pdf_bytes)
954
-
955
- # Generate summary using LLM
956
- if GROQ_API_KEY:
957
- with st.spinner("Generating form summary..."):
958
- summary = generate_form_summary(st.session_state.form_fields, uploaded_file.name)
959
- if summary:
960
- st.markdown("### πŸ“‹ Form Summary")
961
- st.markdown(summary)
962
-
963
- # Show form fields if available
964
- if st.session_state.form_fields:
965
- st.markdown("### πŸ“ Form Fields")
966
 
967
- # Get field explanations if LLM is available
968
- field_explanations = {}
 
 
 
 
 
 
 
 
 
 
969
  if GROQ_API_KEY:
970
- with st.spinner("Analyzing form fields..."):
971
- explanations = explain_form_fields(
972
- st.session_state.form_fields,
973
- "Pakistan",
974
- uploaded_file.name
975
- )
976
- if explanations:
977
- field_explanations = explanations
978
 
979
- # Display fields with explanations if available
980
- if field_explanations and "sections" in field_explanations:
981
- for section in field_explanations["sections"]:
982
- with st.expander(f"πŸ“‘ {section['name']}"):
983
- st.write(section["explanation"])
984
- for field_name in section["fields"]:
985
- matching_fields = [f for f in st.session_state.form_fields
986
- if f["name"] == field_name]
987
- if matching_fields:
988
- field = matching_fields[0]
989
- st.write(f"**{field['name']}** ({field['type']})")
990
- else:
991
- # Simple field display without explanations
992
- for field in st.session_state.form_fields:
993
- st.write(f"**{field['name']}** ({field['type']})")
994
-
995
- else:
996
- st.warning("⚠️ This form doesn't have fillable fields or couldn't be analyzed.")
997
-
998
- # Q&A Section for uploaded form
999
- st.markdown("### ❓ Ask Questions About This Form")
1000
- st.markdown("Get help understanding your uploaded tax form.")
1001
-
1002
- upload_question = st.text_input(
1003
- "Ask a question about this form:",
1004
- placeholder="What is this form used for? How do I fill section X?",
1005
- key="upload_question_input"
1006
- )
1007
-
1008
- if upload_question:
1009
- with st.spinner("Analyzing your question..."):
1010
- # Generate response using the uploaded form context
1011
- upload_response = generate_upload_form_response(
1012
- upload_question,
1013
- uploaded_file.name,
1014
- st.session_state.form_fields
1015
- )
1016
 
1017
- st.markdown("### πŸ’¬ Response:")
1018
- st.markdown(upload_response)
1019
-
1020
- # Download options
1021
- st.markdown("### πŸ“₯ Download Options")
1022
- col1, col2 = st.columns(2)
1023
-
1024
- with col1:
1025
- # Download original form
1026
- st.download_button(
1027
- label="πŸ“₯ Download Original Form",
1028
- data=st.session_state.pdf_bytes,
1029
- file_name=uploaded_file.name,
1030
- mime="application/pdf"
 
 
 
 
 
 
 
 
 
 
 
 
 
1031
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1032
 
1033
- with col2:
1034
- # Option to fill form if it has fields
1035
- if st.session_state.form_fields:
1036
- if st.button("✏️ Fill This Form"):
1037
- st.session_state.is_filling = True
1038
- st.rerun()
1039
-
1040
- else:
1041
- st.info("πŸ‘† Please upload a PDF tax form to get started.")
1042
- st.markdown("""
1043
- **What you can do with uploaded forms:**
1044
- - οΏ½οΏ½οΏ½ View and preview the form
1045
- - πŸ“‹ Get AI-generated summaries
1046
- - πŸ” Analyze form fields and sections
1047
- - ❓ Ask questions about the form
1048
- - ✏️ Fill out interactive forms
1049
- """)
1050
 
1051
  if __name__ == "__main__":
1052
  main()
 
925
  form_response = tax_agent_response(form_question, tax_form_type=form_type)
926
  st.markdown(form_response)
927
  # Tab 3: Upload Form
928
+ with tab3:
929
+ st.header("Upload Your Tax Form")
930
+ st.markdown("Upload a PDF tax form to analyze, get summaries, and ask questions about it.")
931
+
932
+ # File uploader
933
+ uploaded_file = st.file_uploader(
934
+ "Choose a PDF file",
935
+ type=['pdf'],
936
+ help="Upload any Pakistani tax form PDF for analysis"
937
+ )
938
+
939
+ if uploaded_file is not None:
940
+ # Store uploaded file in session state
941
+ st.session_state.pdf_bytes = BytesIO(uploaded_file.read())
942
+ st.session_state.uploaded_file_name = uploaded_file.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
943
 
944
+ # Display success message
945
+ st.success(f"βœ… Successfully uploaded: {uploaded_file.name}")
946
+
947
+ # Extract form fields from uploaded PDF
948
+ with st.spinner("Analyzing uploaded form..."):
949
+ st.session_state.form_fields = extract_form_fields(st.session_state.pdf_bytes)
950
+
951
+ # Display PDF preview
952
+ st.markdown("### πŸ“„ Form Preview")
953
+ display_pdf(st.session_state.pdf_bytes)
954
+
955
+ # Generate summary using LLM
956
  if GROQ_API_KEY:
957
+ with st.spinner("Generating form summary..."):
958
+ summary = generate_form_summary(st.session_state.form_fields, uploaded_file.name)
959
+ if summary:
960
+ st.markdown("### πŸ“‹ Form Summary")
961
+ st.markdown(summary)
 
 
 
962
 
963
+ # Show form fields if available
964
+ if st.session_state.form_fields:
965
+ st.markdown("### πŸ“ Form Fields")
966
+
967
+ # Get field explanations if LLM is available
968
+ field_explanations = {}
969
+ if GROQ_API_KEY:
970
+ with st.spinner("Analyzing form fields..."):
971
+ explanations = explain_form_fields(
972
+ st.session_state.form_fields,
973
+ "Pakistan",
974
+ uploaded_file.name
975
+ )
976
+ if explanations:
977
+ field_explanations = explanations
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
978
 
979
+ # Display fields with explanations if available
980
+ if field_explanations and "sections" in field_explanations:
981
+ for section in field_explanations["sections"]:
982
+ with st.expander(f"πŸ“‘ {section['name']}"):
983
+ st.write(section["explanation"])
984
+ for field_name in section["fields"]:
985
+ matching_fields = [f for f in st.session_state.form_fields
986
+ if f["name"] == field_name]
987
+ if matching_fields:
988
+ field = matching_fields[0]
989
+ st.write(f"**{field['name']}** ({field['type']})")
990
+ else:
991
+ # Simple field display without explanations
992
+ for field in st.session_state.form_fields:
993
+ st.write(f"**{field['name']}** ({field['type']})")
994
+
995
+ else:
996
+ st.warning("⚠️ This form doesn't have fillable fields or couldn't be analyzed.")
997
+
998
+ # Q&A Section for uploaded form
999
+ st.markdown("### ❓ Ask Questions About This Form")
1000
+ st.markdown("Get help understanding your uploaded tax form.")
1001
+
1002
+ upload_question = st.text_input(
1003
+ "Ask a question about this form:",
1004
+ placeholder="What is this form used for? How do I fill section X?",
1005
+ key="upload_question_input"
1006
  )
1007
+
1008
+ if upload_question:
1009
+ with st.spinner("Analyzing your question..."):
1010
+ # Generate response using the uploaded form context
1011
+ upload_response = generate_upload_form_response(
1012
+ upload_question,
1013
+ uploaded_file.name,
1014
+ st.session_state.form_fields
1015
+ )
1016
+
1017
+ st.markdown("### πŸ’¬ Response:")
1018
+ st.markdown(upload_response)
1019
+
1020
+ # Download options
1021
+ st.markdown("### πŸ“₯ Download Options")
1022
+ col1, col2 = st.columns(2)
1023
+
1024
+ with col1:
1025
+ # Download original form
1026
+ st.download_button(
1027
+ label="πŸ“₯ Download Original Form",
1028
+ data=st.session_state.pdf_bytes,
1029
+ file_name=uploaded_file.name,
1030
+ mime="application/pdf"
1031
+ )
1032
+
1033
+ with col2:
1034
+ # Option to fill form if it has fields
1035
+ if st.session_state.form_fields:
1036
+ if st.button("✏️ Fill This Form"):
1037
+ st.session_state.is_filling = True
1038
+ st.rerun()
1039
 
1040
+ else:
1041
+ st.info("πŸ‘† Please upload a PDF tax form to get started.")
1042
+ st.markdown("""
1043
+ **What you can do with uploaded forms:**
1044
+ - πŸ“„ View and preview the form
1045
+ - πŸ“‹ Get AI-generated summaries
1046
+ - πŸ” Analyze form fields and sections
1047
+ - ❓ Ask questions about the form
1048
+ - ✏️ Fill out interactive forms
1049
+ """)
 
 
 
 
 
 
 
1050
 
1051
  if __name__ == "__main__":
1052
  main()