bano1 commited on
Commit
68ec0d7
·
verified ·
1 Parent(s): da07d96

Update components/upload.py

Browse files
Files changed (1) hide show
  1. components/upload.py +60 -110
components/upload.py CHANGED
@@ -1,13 +1,11 @@
1
  import streamlit as st
 
2
  from src.grobid import extract_metadata_grobid
3
  from src.pdf_parser import extract_text_from_pdf
4
 
5
  from src.rag_pipeline import (
6
  create_rag_database,
7
- summarize_abstract,
8
- generate_summary,
9
- generate_limitations,
10
- generate_research_gaps
11
  )
12
 
13
 
@@ -21,146 +19,98 @@ def upload_section():
21
  accept_multiple_files=True
22
  )
23
 
24
- if uploaded_files:
25
-
26
- new_papers = []
27
-
28
- progress = st.progress(0)
29
- status = st.empty()
30
-
31
- total_files = len(uploaded_files)
32
-
33
- for idx, file in enumerate(uploaded_files):
34
-
35
- status.write(
36
- f"Processing **{file.name}**..."
37
- )
38
 
39
- # ---------------------------------------
40
- # Extract Metadata
41
- # ---------------------------------------
42
- try:
43
- meta = extract_metadata_grobid(file)
44
- except Exception:
45
- meta = {
46
 
47
- "title": file.name.replace(".pdf", ""),
48
-
49
- "authors": ["Unknown"],
50
-
51
- "abstract": ""
52
-
53
- }
54
-
55
- # Reset file pointer
56
- file.seek(0)
57
 
58
- # ---------------------------------------
59
- # Extract Full Paper Text
60
- # ---------------------------------------
61
 
62
- text = extract_text_from_pdf(file)
63
 
64
- meta["text"] = text
65
 
66
- # ---------------------------------------
67
- # Generate Abstract Summary
68
- # ---------------------------------------
69
 
70
- try:
 
 
71
 
72
- if meta.get("abstract"):
73
 
74
- meta["abstract_summary"] = summarize_abstract(
75
- meta["abstract"]
76
- )
77
 
78
- else:
79
 
80
- meta["abstract_summary"] = (
81
- "Abstract not available."
82
- )
83
 
84
- except Exception:
85
 
86
- meta["abstract_summary"] = (
87
- "Abstract summary could not be generated."
88
- )
89
 
90
- # ---------------------------------------
91
- # Generate AI Paper Summary
92
- # ---------------------------------------
93
 
94
- try:
95
 
96
- meta["summary"] = generate_summary(
97
- text
98
- )
99
 
100
- except Exception:
101
 
102
- meta["summary"] = (
103
- "Summary could not be generated."
104
- )
105
 
106
- # ---------------------------------------
107
- # Generate Limitations
108
- # ---------------------------------------
109
 
110
- try:
111
 
112
- meta["limitations"] = (
113
- generate_limitations(text)
114
- )
115
 
116
- except Exception:
 
 
117
 
118
- meta["limitations"] = (
119
- "Limitations could not be extracted."
120
- )
121
 
122
- # ---------------------------------------
123
- # Generate Research Gaps
124
- # ---------------------------------------
 
125
 
126
- try:
127
 
128
- meta["research_gaps"] = (
129
- generate_research_gaps(text)
130
- )
131
 
132
- except Exception:
133
 
134
- meta["research_gaps"] = (
135
- "Research gaps could not be generated."
136
- )
137
 
138
- # Save paper
139
- new_papers.append(meta)
140
 
141
- progress.progress(
142
- (idx + 1) / total_files
143
- )
144
 
145
- # ---------------------------------------
146
- # Store Papers
147
- # ---------------------------------------
148
 
149
- st.session_state.papers.extend(
150
- new_papers
151
  )
152
 
153
- # ---------------------------------------
154
- # Build FAISS Vector Database
155
- # ---------------------------------------
156
 
157
- create_rag_database(
158
- st.session_state.papers
159
- )
 
 
160
 
161
- progress.empty()
162
- status.empty()
163
 
164
- st.success(
165
- f"Successfully processed {len(new_papers)} paper(s)."
166
- )
 
1
  import streamlit as st
2
+
3
  from src.grobid import extract_metadata_grobid
4
  from src.pdf_parser import extract_text_from_pdf
5
 
6
  from src.rag_pipeline import (
7
  create_rag_database,
8
+ analyze_paper
 
 
 
9
  )
10
 
11
 
 
19
  accept_multiple_files=True
20
  )
21
 
22
+ if not uploaded_files:
23
+ return
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ new_papers = []
 
 
 
 
 
 
26
 
27
+ progress = st.progress(0)
 
 
 
 
 
 
 
 
 
28
 
29
+ status = st.empty()
 
 
30
 
31
+ total_files = len(uploaded_files)
32
 
33
+ for idx, file in enumerate(uploaded_files):
34
 
35
+ status.write(
36
+ f"Processing **{file.name}**..."
37
+ )
38
 
39
+ # ----------------------------
40
+ # GROBID Metadata
41
+ # ----------------------------
42
 
43
+ try:
44
 
45
+ meta = extract_metadata_grobid(file)
 
 
46
 
47
+ except Exception:
48
 
49
+ meta = {
 
 
50
 
51
+ "title": file.name.replace(".pdf", ""),
52
 
53
+ "authors": ["Unknown"],
 
 
54
 
55
+ "abstract": ""
 
 
56
 
57
+ }
58
 
59
+ # ----------------------------
60
+ # Read Full Paper
61
+ # ----------------------------
62
 
63
+ file.seek(0)
64
 
65
+ text = extract_text_from_pdf(file)
 
 
66
 
67
+ meta["text"] = text
 
 
68
 
69
+ if not meta.get("abstract"):
70
 
71
+ meta["abstract"] = text[:3000]
 
 
72
 
73
+ # ----------------------------
74
+ # One AI Call
75
+ # ----------------------------
76
 
77
+ try:
 
 
78
 
79
+ ai_result = analyze_paper(
80
+ text=text,
81
+ abstract=meta["abstract"]
82
+ )
83
 
84
+ meta.update(ai_result)
85
 
86
+ except Exception:
 
 
87
 
88
+ meta["summary"] = "Summary could not be generated."
89
 
90
+ meta["abstract_summary"] = "Abstract summary unavailable."
 
 
91
 
92
+ meta["limitations"] = "Limitations unavailable."
 
93
 
94
+ meta["research_gaps"] = "Research gaps unavailable."
 
 
95
 
96
+ new_papers.append(meta)
 
 
97
 
98
+ progress.progress(
99
+ (idx + 1) / total_files
100
  )
101
 
102
+ st.session_state.papers.extend(
103
+ new_papers
104
+ )
105
 
106
+ create_rag_database(
107
+ st.session_state.papers
108
+ )
109
+
110
+ progress.empty()
111
 
112
+ status.empty()
 
113
 
114
+ st.success(
115
+ f"Successfully processed {len(new_papers)} paper(s)."
116
+ )