Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -81,19 +81,32 @@ excluding the title, author name, source information, chapter number, annotation
|
|
| 81 |
{text}
|
| 82 |
"""
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
# =========================
|
| 94 |
-
# 指標(FRE + 単語数)
|
| 95 |
-
# =========================
|
| 96 |
-
_word_re = re.compile(r"[A-Za-z]+(?:'[A-Za-z]+)?")
|
| 97 |
|
| 98 |
def count_words_english(text: str) -> int:
|
| 99 |
return len(_word_re.findall(text))
|
|
|
|
| 81 |
{text}
|
| 82 |
"""
|
| 83 |
|
| 84 |
+
# 安全な max_tokens 候補(大→小)
|
| 85 |
+
max_tokens_candidates = [3000, 2000, 1500, 1000]
|
| 86 |
+
|
| 87 |
+
last_error = None
|
| 88 |
+
for mt in max_tokens_candidates:
|
| 89 |
+
try:
|
| 90 |
+
with _rewrite_sem:
|
| 91 |
+
resp = client.chat.completions.create(
|
| 92 |
+
model=MODEL,
|
| 93 |
+
messages=[{"role": "user", "content": prompt}],
|
| 94 |
+
temperature=0.4,
|
| 95 |
+
max_tokens=mt
|
| 96 |
+
)
|
| 97 |
+
return resp.choices[0].message.content.strip()
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
last_error = e
|
| 101 |
+
# 402(クレジット不足 or トークン過多)の場合は縮めて再試行
|
| 102 |
+
if "402" in str(e) or "more credits" in str(e):
|
| 103 |
+
continue
|
| 104 |
+
else:
|
| 105 |
+
raise e
|
| 106 |
+
|
| 107 |
+
# 全部ダメなら最後のエラーを投げる
|
| 108 |
+
raise RuntimeError(f"Rewrite failed after retries: {last_error}")
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
def count_words_english(text: str) -> int:
|
| 112 |
return len(_word_re.findall(text))
|