@@ -19,7 +19,7 @@ def load_model():
19
19
20
20
@st .cache_resource
21
21
def convert_to_sections (uploaded_file , output_dir ):
22
- document_to_sections_dir (
22
+ return document_to_sections_dir (
23
23
pymupdf .open ("type" , BytesIO (uploaded_file .read ())),
24
24
output_dir ,
25
25
)
@@ -39,23 +39,55 @@ def convert_to_sections(uploaded_file, output_dir):
39
39
st .markdown ("[Docs for this Step]()" )
40
40
st .divider ()
41
41
42
- convert_to_sections (uploaded_file , f"example_outputs/{ uploaded_file .name } " )
43
-
44
- sections = [f .stem for f in Path (f"example_outputs/{ uploaded_file .name } " ).iterdir ()]
45
- st .json (sections )
46
-
47
- model = load_model ()
48
- question = st .text_input ("Enter a question:" )
49
- if question :
50
- with st .spinner ("Answering..." ):
51
- answer , sections_checked = find_retrieve_answer (
52
- model = model ,
53
- sections_dir = f"example_outputs/{ uploaded_file .name } " ,
54
- question = question ,
55
- find_prompt = FIND_PROMPT ,
56
- answer_prompt = ANSWER_PROMPT ,
42
+ try :
43
+ with st .spinner ("Converting document to sections..." ):
44
+ section_names = convert_to_sections (
45
+ uploaded_file , f"example_outputs/{ uploaded_file .name } "
57
46
)
58
- st .text ("Sections checked:" )
59
- st .json (sections_checked )
60
- st .text ("Answer:" )
61
- st .text (answer )
47
+ sections = [
48
+ f .stem for f in Path (f"example_outputs/{ uploaded_file .name } " ).iterdir ()
49
+ ]
50
+
51
+ # Provide feedback about segmentation
52
+ st .success (
53
+ f"Successfully extracted { len (sections )} sections from the document."
54
+ )
55
+
56
+ # Check for potential segmentation issues
57
+ if len (sections ) == 1 :
58
+ st .warning (
59
+ "⚠️ Only one section was found. This might indicate that the document structure wasn't properly detected."
60
+ )
61
+ elif len (sections ) == 0 :
62
+ st .error (
63
+ "❌ No sections were found in the document. The document might not have a clear structure or might be in an unsupported format."
64
+ )
65
+ elif "INTRO" in sections and len (sections ) < 3 :
66
+ st .warning (
67
+ "⚠️ Only found default sections. The document structure might not have been properly detected."
68
+ )
69
+
70
+ # Show sections
71
+ st .text ("Detected Sections:" )
72
+ st .json (sections )
73
+
74
+ model = load_model ()
75
+ question = st .text_input ("Enter a question:" )
76
+ if question :
77
+ with st .spinner ("Answering..." ):
78
+ answer , sections_checked = find_retrieve_answer (
79
+ model = model ,
80
+ sections_dir = f"example_outputs/{ uploaded_file .name } " ,
81
+ question = question ,
82
+ find_prompt = FIND_PROMPT ,
83
+ answer_prompt = ANSWER_PROMPT ,
84
+ )
85
+ st .text ("Sections checked:" )
86
+ st .json (sections_checked )
87
+ st .text ("Answer:" )
88
+ st .text (answer )
89
+ except Exception as e :
90
+ st .error (f"❌ Error processing document: { str (e )} " )
91
+ st .info (
92
+ "💡 Try uploading a different document or check if the file is corrupted."
93
+ )
0 commit comments