Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
547 |
)
|
548 |
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
|
|
|
|
|
|
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
|