bep40 commited on
Commit
005324e
·
verified ·
1 Parent(s): bd3dc36

Improve opinion post: increase AI timeout + improve fallback

Browse files
Files changed (1) hide show
  1. ai_ext.py +25 -5
ai_ext.py CHANGED
@@ -143,7 +143,7 @@ async def qwen_generate(prompt: str, image_url: str = None, max_tokens: int = 12
143
  "https://router.huggingface.co/v1/chat/completions",
144
  headers=headers,
145
  json=payload,
146
- timeout=95
147
  )
148
 
149
  if r.status_code >= 300:
@@ -167,16 +167,29 @@ async def qwen_generate(prompt: str, image_url: str = None, max_tokens: int = 12
167
 
168
 
169
  def _fallback_summary_from_prompt(prompt: str, max_units: int = 6) -> str:
170
- """Generate a simple fallback summary when AI is unavailable."""
171
  text = prompt or ""
172
- for marker in ["Nội dung nguồn:", "Nội dung bài:", "Nội dung gốc:", "Nội dung:", "Nguồn/bối cảnh internet:"]:
 
 
 
173
  if marker in text:
174
- text = text.split(marker, 1)[1]
 
 
 
 
 
 
175
  break
 
 
176
  text = re.sub(r"https?://\S+", "", text)
 
 
177
  text = re.sub(r"\s+", " ", text).strip()
178
 
179
- # Split into sentences
180
  sentences = re.split(r"(?<=[.!?])\s+(?=[A-ZÀ-Ỹ0-9])", text)
181
  units = []
182
  for s in sentences:
@@ -186,6 +199,9 @@ def _fallback_summary_from_prompt(prompt: str, max_units: int = 6) -> str:
186
 
187
  if units:
188
  result_units = units[:max_units]
 
 
 
189
  return "\n".join("• " + u for u in result_units)
190
  if text:
191
  chunks = []
@@ -195,8 +211,12 @@ def _fallback_summary_from_prompt(prompt: str, max_units: int = 6) -> str:
195
  chunks.append(chunk)
196
  if len(chunks) >= max_units:
197
  break
 
 
198
  if chunks:
199
  return "\n".join("• " + c for c in chunks)
 
 
200
  return "• Không có đủ nội dung để tóm tắt."
201
 
202
  # ===== URL scraping & article processing =====
 
143
  "https://router.huggingface.co/v1/chat/completions",
144
  headers=headers,
145
  json=payload,
146
+ timeout=120
147
  )
148
 
149
  if r.status_code >= 300:
 
167
 
168
 
169
  def _fallback_summary_from_prompt(prompt: str, max_units: int = 6) -> str:
170
+ """Generate a fallback summary incorporating opinion + sources."""
171
  text = prompt or ""
172
+ opinion = ""
173
+ # Extract opinion section if present
174
+ opinion_marker_found = False
175
+ for marker in ["QUAN ĐIỂM:", "Nội dung nguồn:", "Nội dung bài:", "Nguồn/bối cảnh internet:"]:
176
  if marker in text:
177
+ part_after = text.split(marker, 1)[1]
178
+ if marker == "QUAN ĐIỂM:":
179
+ opinion = part_after.split("\n")[0].strip()[:500]
180
+ opinion_marker_found = True
181
+ text = part_after[len(opinion)+1:] if len(part_after) > len(opinion) else ""
182
+ else:
183
+ text = part_after
184
  break
185
+
186
+ # Clean up the remaining text (source content)
187
  text = re.sub(r"https?://\S+", "", text)
188
+ text = re.sub(r"===.*===\n?", "", text)
189
+ text = re.sub(r"[*-]\s*SLIDE\s*\d+[^\n]*\n?", "", text)
190
  text = re.sub(r"\s+", " ", text).strip()
191
 
192
+ # Build opinion-driven bullets from source sentences
193
  sentences = re.split(r"(?<=[.!?])\s+(?=[A-ZÀ-Ỹ0-9])", text)
194
  units = []
195
  for s in sentences:
 
199
 
200
  if units:
201
  result_units = units[:max_units]
202
+ # Prepend opinion to first bullet if available
203
+ if opinion:
204
+ result_units[0] = f"{opinion}. {result_units[0]}"
205
  return "\n".join("• " + u for u in result_units)
206
  if text:
207
  chunks = []
 
211
  chunks.append(chunk)
212
  if len(chunks) >= max_units:
213
  break
214
+ if chunks and opinion:
215
+ chunks[0] = f"{opinion}. {chunks[0]}"
216
  if chunks:
217
  return "\n".join("• " + c for c in chunks)
218
+ if opinion:
219
+ return f"{opinion}."
220
  return "• Không có đủ nội dung để tóm tắt."
221
 
222
  # ===== URL scraping & article processing =====