Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -173,32 +173,48 @@ class DocumentRAG:
|
|
| 173 |
return f"Error processing documents: {str(e)}"
|
| 174 |
|
| 175 |
def generate_summary(self, text, language):
|
| 176 |
-
"""Generate a summary
|
| 177 |
if not self.api_key:
|
| 178 |
return "API Key not set. Please set it in the environment variables."
|
|
|
|
| 179 |
try:
|
| 180 |
client = OpenAI(api_key=self.api_key)
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
except Exception as e:
|
| 200 |
return f"Error generating summary: {str(e)}"
|
| 201 |
|
|
|
|
| 202 |
def create_podcast(self, language):
|
| 203 |
"""Generate a podcast script and audio based on doc summary in the specified language."""
|
| 204 |
if not self.document_summary:
|
|
|
|
| 173 |
return f"Error processing documents: {str(e)}"
|
| 174 |
|
| 175 |
def generate_summary(self, text, language):
|
| 176 |
+
"""Generate a structured summary from all chunks of the document."""
|
| 177 |
if not self.api_key:
|
| 178 |
return "API Key not set. Please set it in the environment variables."
|
| 179 |
+
|
| 180 |
try:
|
| 181 |
client = OpenAI(api_key=self.api_key)
|
| 182 |
+
|
| 183 |
+
# Split into chunks
|
| 184 |
+
chunks = [text[i:i + 3000] for i in range(0, len(text), 3000)]
|
| 185 |
+
summaries = []
|
| 186 |
+
|
| 187 |
+
for i, chunk in enumerate(chunks):
|
| 188 |
+
response = client.chat.completions.create(
|
| 189 |
+
model="gpt-4",
|
| 190 |
+
messages=[
|
| 191 |
+
{"role": "system", "content": f"""
|
| 192 |
+
You are a scientific summarization assistant.
|
| 193 |
+
Summarize the input below in {language} in a structured format, covering:
|
| 194 |
+
- Abstract (if present)
|
| 195 |
+
- Key Contributions
|
| 196 |
+
- Results/Findings
|
| 197 |
+
- Conclusion
|
| 198 |
+
- Limitations
|
| 199 |
+
- Future Work
|
| 200 |
+
|
| 201 |
+
If any section is missing, just skip it. Keep the language clear and concise.
|
| 202 |
+
"""},
|
| 203 |
+
{"role": "user", "content": chunk}
|
| 204 |
+
],
|
| 205 |
+
temperature=0.4
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
content = response.choices[0].message.content.strip()
|
| 209 |
+
summaries.append(f"### Part {i+1}\n{content}")
|
| 210 |
+
|
| 211 |
+
full_summary = "\n\n".join(summaries)
|
| 212 |
+
return full_summary
|
| 213 |
+
|
| 214 |
except Exception as e:
|
| 215 |
return f"Error generating summary: {str(e)}"
|
| 216 |
|
| 217 |
+
|
| 218 |
def create_podcast(self, language):
|
| 219 |
"""Generate a podcast script and audio based on doc summary in the specified language."""
|
| 220 |
if not self.document_summary:
|