Update tokenzzzsimple.py
Browse files- tokenzzzsimple.py +75 -75
tokenzzzsimple.py
CHANGED
|
@@ -1,76 +1,76 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import requests
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
from tqdm import tqdm
|
| 6 |
-
import re
|
| 7 |
-
def natural_sort_key(filename):
|
| 8 |
-
def convert(text):
|
| 9 |
-
return int(text) if text.isdigit() else text.lower()
|
| 10 |
-
return [convert(c) for c in re.split('([0-9]+)', str(filename))]
|
| 11 |
-
def get_sorted_files(folder_path, pattern="*.txt"):
|
| 12 |
-
folder = Path(folder_path)
|
| 13 |
-
if not folder.exists():
|
| 14 |
-
return []
|
| 15 |
-
files = list(folder.glob(pattern))
|
| 16 |
-
files.sort(key=natural_sort_key)
|
| 17 |
-
return files
|
| 18 |
-
def call_ollama(prompt):
|
| 19 |
-
try:
|
| 20 |
-
response = requests.post(
|
| 21 |
-
'http://localhost:11434/api/generate',
|
| 22 |
-
json={
|
| 23 |
-
'model': 'cyanelis/ElisNovel-V1:14b',
|
| 24 |
-
'prompt': prompt,
|
| 25 |
-
'stream': False,
|
| 26 |
-
'options': {
|
| 27 |
-
'num_ctx': 5770
|
| 28 |
-
}
|
| 29 |
-
},
|
| 30 |
-
timeout=600
|
| 31 |
-
)
|
| 32 |
-
if response.status_code == 200:
|
| 33 |
-
result = response.json()
|
| 34 |
-
return result.get('response', '')
|
| 35 |
-
else:
|
| 36 |
-
print(f"Ollama API错误: {response.status_code}")
|
| 37 |
-
return ""
|
| 38 |
-
except requests.exceptions.RequestException as e:
|
| 39 |
-
print(f"调用Ollama时发生错误: {e}")
|
| 40 |
-
return ""
|
| 41 |
-
def main():
|
| 42 |
-
z_folder = Path("Z109c")
|
| 43 |
-
z_folder.mkdir(exist_ok=True)
|
| 44 |
-
g_files = get_sorted_files("G109b", "*.txt")
|
| 45 |
-
if len(g_files) < 2:
|
| 46 |
-
print("需要至少2个txt文件才能运行")
|
| 47 |
-
return
|
| 48 |
-
n = len(g_files)
|
| 49 |
-
print(f"找到 {n} 个文件,开始推理...")
|
| 50 |
-
pbar = tqdm(total=n, desc="推理进度", unit="file")
|
| 51 |
-
for i, g_file in enumerate(g_files):
|
| 52 |
-
next_index = (i + 1) % n
|
| 53 |
-
next_file = g_files[next_index]
|
| 54 |
-
output_filename = f"Z{next_index+1}.txt" if n > 1 else "Z1.txt"
|
| 55 |
-
output_path = z_folder / output_filename
|
| 56 |
-
if output_path.exists():
|
| 57 |
-
pbar.update(1)
|
| 58 |
-
continue
|
| 59 |
-
pbar.set_description(f"处理: {g_file.name} → {output_filename}")
|
| 60 |
-
try:
|
| 61 |
-
g_content = g_file.read_text(encoding='utf-8')
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"读取文件 {g_file.name} 失败: {e}")
|
| 64 |
-
pbar.update(1)
|
| 65 |
-
continue
|
| 66 |
-
z_content = call_ollama(g_content)
|
| 67 |
-
if z_content:
|
| 68 |
-
output_path.write_text(z_content, encoding='utf-8')
|
| 69 |
-
print(f"✓ 成功生成: {output_filename}")
|
| 70 |
-
else:
|
| 71 |
-
print(f"✗ 推理失败: {g_file.name} → {output_filename}")
|
| 72 |
-
pbar.update(1)
|
| 73 |
-
pbar.close()
|
| 74 |
-
print("推理完成!")
|
| 75 |
-
if __name__ == "__main__":
|
| 76 |
main()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
import re
|
| 7 |
+
def natural_sort_key(filename):
|
| 8 |
+
def convert(text):
|
| 9 |
+
return int(text) if text.isdigit() else text.lower()
|
| 10 |
+
return [convert(c) for c in re.split('([0-9]+)', str(filename))]
|
| 11 |
+
def get_sorted_files(folder_path, pattern="*.txt"):
|
| 12 |
+
folder = Path(folder_path)
|
| 13 |
+
if not folder.exists():
|
| 14 |
+
return []
|
| 15 |
+
files = list(folder.glob(pattern))
|
| 16 |
+
files.sort(key=natural_sort_key)
|
| 17 |
+
return files
|
| 18 |
+
def call_ollama(prompt):
|
| 19 |
+
try:
|
| 20 |
+
response = requests.post(
|
| 21 |
+
'http://localhost:11434/api/generate',
|
| 22 |
+
json={
|
| 23 |
+
'model': 'cyanelis/ElisNovel-V1:14b', //ElisNovel-V1-14B-Q4_K_M:latest 或 ElisNovel-V1-14B-Q8_0:latest 或 ElisNovel-V1-14B-F16:latest
|
| 24 |
+
'prompt': prompt,
|
| 25 |
+
'stream': False,
|
| 26 |
+
'options': {
|
| 27 |
+
'num_ctx': 5770
|
| 28 |
+
}
|
| 29 |
+
},
|
| 30 |
+
timeout=600
|
| 31 |
+
)
|
| 32 |
+
if response.status_code == 200:
|
| 33 |
+
result = response.json()
|
| 34 |
+
return result.get('response', '')
|
| 35 |
+
else:
|
| 36 |
+
print(f"Ollama API错误: {response.status_code}")
|
| 37 |
+
return ""
|
| 38 |
+
except requests.exceptions.RequestException as e:
|
| 39 |
+
print(f"调用Ollama时发生错误: {e}")
|
| 40 |
+
return ""
|
| 41 |
+
def main():
|
| 42 |
+
z_folder = Path("Z109c")
|
| 43 |
+
z_folder.mkdir(exist_ok=True)
|
| 44 |
+
g_files = get_sorted_files("G109b", "*.txt")
|
| 45 |
+
if len(g_files) < 2:
|
| 46 |
+
print("需要至少2个txt文件才能运行")
|
| 47 |
+
return
|
| 48 |
+
n = len(g_files)
|
| 49 |
+
print(f"找到 {n} 个文件,开始推理...")
|
| 50 |
+
pbar = tqdm(total=n, desc="推理进度", unit="file")
|
| 51 |
+
for i, g_file in enumerate(g_files):
|
| 52 |
+
next_index = (i + 1) % n
|
| 53 |
+
next_file = g_files[next_index]
|
| 54 |
+
output_filename = f"Z{next_index+1}.txt" if n > 1 else "Z1.txt"
|
| 55 |
+
output_path = z_folder / output_filename
|
| 56 |
+
if output_path.exists():
|
| 57 |
+
pbar.update(1)
|
| 58 |
+
continue
|
| 59 |
+
pbar.set_description(f"处理: {g_file.name} → {output_filename}")
|
| 60 |
+
try:
|
| 61 |
+
g_content = g_file.read_text(encoding='utf-8')
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"读取文件 {g_file.name} 失败: {e}")
|
| 64 |
+
pbar.update(1)
|
| 65 |
+
continue
|
| 66 |
+
z_content = call_ollama(g_content)
|
| 67 |
+
if z_content:
|
| 68 |
+
output_path.write_text(z_content, encoding='utf-8')
|
| 69 |
+
print(f"✓ 成功生成: {output_filename}")
|
| 70 |
+
else:
|
| 71 |
+
print(f"✗ 推理失败: {g_file.name} → {output_filename}")
|
| 72 |
+
pbar.update(1)
|
| 73 |
+
pbar.close()
|
| 74 |
+
print("推理完成!")
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
main()
|