DrishtiSharma commited on
Commit
40c5626
·
verified ·
1 Parent(s): a3a81b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -14
app.py CHANGED
@@ -538,23 +538,50 @@ if st.button("Process Documents"):
538
  st.warning("No files uploaded.")
539
 
540
  # Step 2: Generate Summary
541
- if st.button("Generate Summary"):
542
- if hasattr(st.session_state.rag_system, "document_text") and st.session_state.rag_system.document_text:
543
- with st.spinner("Generating summary and visual..."):
544
- summary = st.session_state.rag_system.generate_summary(
545
- st.session_state.rag_system.document_text,
546
- summary_language
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547
  )
548
 
549
- if summary:
550
- st.session_state.rag_system.document_summary = summary
551
- st.text_area("Document Summary", summary, height=250)
552
- st.success("Summary generated successfully!")
 
 
 
553
 
554
- else:
555
- st.error("Failed to generate summary.")
556
- else:
557
- st.info("Please process documents first to generate summary.")
558
 
559
 
560
  # Step 3: Ask Questions
 
538
  st.warning("No files uploaded.")
539
 
540
  # Step 2: Generate Summary
541
+ def generate_summary(self, text, language):
542
+ """Generate a structured summary from all chunks of the document."""
543
+ if not self.api_key:
544
+ return "API Key not set. Please set it in the environment variables."
545
+
546
+ try:
547
+ client = OpenAI(api_key=self.api_key)
548
+
549
+ # Split into chunks to avoid token limit issues
550
+ chunks = [text[i:i + 3000] for i in range(0, len(text), 3000)]
551
+ summaries = []
552
+
553
+ for i, chunk in enumerate(chunks):
554
+ response = client.chat.completions.create(
555
+ model="gpt-4",
556
+ messages=[
557
+ {"role": "system", "content": f"""
558
+ You are a multilingual academic summarization assistant.
559
+
560
+ Your job is to summarize scientific or technical documents in a structured and concise way, **in {language}**.
561
+
562
+ Follow this structured format if applicable:
563
+ - Abstract
564
+ - Key Contributions
565
+ - Results/Findings
566
+ - Conclusion
567
+ - Limitations
568
+ - Future Work
569
+
570
+ If a section is not present in the document, skip it without guessing. Keep language formal, clear, and audience-appropriate.
571
+ """},
572
+ {"role": "user", "content": chunk}
573
+ ],
574
+ temperature=0.4
575
  )
576
 
577
+ summaries.append(f"### Part {i+1}\n{response.choices[0].message.content.strip()}")
578
+
579
+ return "\n\n".join(summaries)
580
+
581
+ except Exception as e:
582
+ return f"Error generating summary: {str(e)}"
583
+
584
 
 
 
 
 
585
 
586
 
587
  # Step 3: Ask Questions