Delete claude_analysis.py
Browse files- claude_analysis.py +0 -164
claude_analysis.py
DELETED
|
@@ -1,164 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
# -*- coding: utf-8 -*-
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
import sys
|
| 6 |
-
import json
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
|
| 9 |
-
try:
|
| 10 |
-
import anthropic
|
| 11 |
-
except ImportError:
|
| 12 |
-
print("\nERROR: anthropic library is not installed")
|
| 13 |
-
print("To install, run: pip install anthropic")
|
| 14 |
-
print("or: python -m pip install anthropic\n")
|
| 15 |
-
print(json.dumps({"error": "Missing anthropic library. Please run: pip install anthropic"}))
|
| 16 |
-
sys.exit(1)
|
| 17 |
-
|
| 18 |
-
# Проверяем версию anthropic
|
| 19 |
-
try:
|
| 20 |
-
anthropic_version = anthropic.__version__
|
| 21 |
-
print(f"\nDEBUG - Using anthropic version: {anthropic_version}")
|
| 22 |
-
except:
|
| 23 |
-
print("\nWARNING - Could not determine anthropic version")
|
| 24 |
-
|
| 25 |
-
api_key = os.environ.get("ANTHROPIC_API_KEY", "")
|
| 26 |
-
# api_key = "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"
|
| 27 |
-
if not api_key:
|
| 28 |
-
print(json.dumps({"error": "No ANTHROPIC_API_KEY provided"}))
|
| 29 |
-
sys.exit(0)
|
| 30 |
-
|
| 31 |
-
def get_txt_contents(local_path=None):
|
| 32 |
-
"""
|
| 33 |
-
Получает содержимое .txt файлов либо из локальной папки, либо из аргументов командной строки
|
| 34 |
-
"""
|
| 35 |
-
txt_contents = []
|
| 36 |
-
|
| 37 |
-
# Пробуем использовать локальную папку
|
| 38 |
-
if local_path:
|
| 39 |
-
try:
|
| 40 |
-
path = Path(local_path)
|
| 41 |
-
if path.exists():
|
| 42 |
-
print(f"\nDEBUG - Reading files from local path: {path}")
|
| 43 |
-
for txt_file in path.glob("*.txt"):
|
| 44 |
-
try:
|
| 45 |
-
with open(txt_file, 'r', encoding='utf-8') as f:
|
| 46 |
-
txt_contents.append(f.read())
|
| 47 |
-
print(f"DEBUG - Read file: {txt_file.name}")
|
| 48 |
-
except Exception as e:
|
| 49 |
-
print(f"DEBUG - Error reading {txt_file}: {e}")
|
| 50 |
-
return txt_contents
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f"\nDEBUG - Error accessing local path: {e}")
|
| 53 |
-
|
| 54 |
-
# Если локальная папка недоступна, используем аргументы командной строки
|
| 55 |
-
txt_file_list = sys.argv[1:]
|
| 56 |
-
if not txt_file_list:
|
| 57 |
-
return txt_contents
|
| 58 |
-
|
| 59 |
-
for f in txt_file_list:
|
| 60 |
-
try:
|
| 61 |
-
with open(f, 'r', encoding='utf-8') as ff:
|
| 62 |
-
txt_contents.append(ff.read())
|
| 63 |
-
except Exception as e:
|
| 64 |
-
print(f"DEBUG - Error reading {f}: {e}")
|
| 65 |
-
|
| 66 |
-
return txt_contents
|
| 67 |
-
|
| 68 |
-
# -----------------------------------------------------------------------------
|
| 69 |
-
# 1. Собираем список файлов
|
| 70 |
-
# -----------------------------------------------------------------------------
|
| 71 |
-
local_path = r"G:\My Drive\Kohya_SS\Flux\SoloBand\IconsGray"
|
| 72 |
-
txt_contents = get_txt_contents(local_path)
|
| 73 |
-
|
| 74 |
-
if not txt_contents:
|
| 75 |
-
print("\nDEBUG - No text files found in local path or arguments")
|
| 76 |
-
|
| 77 |
-
combined_text = "\n---\n".join(txt_contents)
|
| 78 |
-
|
| 79 |
-
# -----------------------------------------------------------------------------
|
| 80 |
-
# 2. Формируем промпт для Claude
|
| 81 |
-
# -----------------------------------------------------------------------------
|
| 82 |
-
prompt_content = f"""You are a highly creative AI art director. Your task is to generate an imaginative and specific JSON response. Never use "Unknown" values - instead, create interesting and unique artistic directions even without input files.
|
| 83 |
-
|
| 84 |
-
You MUST ALWAYS return ONLY a valid JSON object in this exact format, with no additional text:
|
| 85 |
-
|
| 86 |
-
{{
|
| 87 |
-
"token": "Choose exactly one: FD_AI (Fantasy & Dragons), SB_AI (Storybook), CM_AI (Comic), AN_AI (Anime), RT_AI (Realistic)",
|
| 88 |
-
"art_type": "Create a specific description like: 'Mystical Forest Portal', 'Neon Cyberpunk City', 'Cozy Cottage Interior', 'Ancient Dragon Lair', 'Futuristic Space Station'",
|
| 89 |
-
"style_name": "Define clear style like: 'Vibrant watercolor fantasy', 'Detailed pencil illustration', 'Dark gothic oil painting', 'Cute kawaii digital art', 'Cinematic 3D render'",
|
| 90 |
-
"model_name": "Will be auto-generated as {{token}}_{{art_type}}_V1",
|
| 91 |
-
"prompts": [
|
| 92 |
-
"Generate 6 detailed prompts that perfectly match the art_type and style_name",
|
| 93 |
-
"Each prompt should be unique and vivid",
|
| 94 |
-
"Include specific details about composition, lighting, mood, colors",
|
| 95 |
-
"Keep each prompt to 1-2 impactful sentences",
|
| 96 |
-
"Focus on visual elements rather than story",
|
| 97 |
-
"Maintain consistent style across all prompts"
|
| 98 |
-
]
|
| 99 |
-
}}
|
| 100 |
-
|
| 101 |
-
Even with no input files, you must generate a creative and complete response using the available tokens and styles. Never return "Unknown" values.
|
| 102 |
-
|
| 103 |
-
Input content to analyze (if any):
|
| 104 |
-
---
|
| 105 |
-
{combined_text}
|
| 106 |
-
"""
|
| 107 |
-
|
| 108 |
-
# Добавляем отладочный вывод промпта
|
| 109 |
-
print("\nDEBUG - Full prompt being sent to Claude:")
|
| 110 |
-
print("="*80)
|
| 111 |
-
print(prompt_content)
|
| 112 |
-
print("="*80)
|
| 113 |
-
|
| 114 |
-
# -----------------------------------------------------------------------------
|
| 115 |
-
# 3. Обращаемся к Anthropic (Claude) с указанной моделью
|
| 116 |
-
# -----------------------------------------------------------------------------
|
| 117 |
-
try:
|
| 118 |
-
client = anthropic.Anthropic(api_key=api_key)
|
| 119 |
-
message = client.messages.create(
|
| 120 |
-
model="claude-3-sonnet-20240229",
|
| 121 |
-
max_tokens=1024,
|
| 122 |
-
messages=[
|
| 123 |
-
{
|
| 124 |
-
"role": "user",
|
| 125 |
-
"content": prompt_content
|
| 126 |
-
}
|
| 127 |
-
],
|
| 128 |
-
temperature=0.7,
|
| 129 |
-
)
|
| 130 |
-
raw_reply = message.content[0].text
|
| 131 |
-
except Exception as e:
|
| 132 |
-
print(json.dumps({"error": f"Request to Claude failed: {str(e)}"}))
|
| 133 |
-
sys.exit(0)
|
| 134 |
-
|
| 135 |
-
# -----------------------------------------------------------------------------
|
| 136 |
-
# 4. Пытаемся интерпретировать ответ как JSON
|
| 137 |
-
# -----------------------------------------------------------------------------
|
| 138 |
-
try:
|
| 139 |
-
data = json.loads(raw_reply)
|
| 140 |
-
except:
|
| 141 |
-
# Если парсить напрямую не получается, отправим ошибку
|
| 142 |
-
print(json.dumps({"error": "Claude response is not valid JSON", "raw_reply": raw_reply}))
|
| 143 |
-
sys.exit(0)
|
| 144 |
-
|
| 145 |
-
# -----------------------------------------------------------------------------
|
| 146 |
-
# 5. (опционально) Проверяем, что в ответе есть нужные поля
|
| 147 |
-
# -----------------------------------------------------------------------------
|
| 148 |
-
token = data.get("token", "SB_AI")
|
| 149 |
-
art_type = data.get("art_type", "UnknownArtType")
|
| 150 |
-
style_name = data.get("style_name", "UnknownStyle")
|
| 151 |
-
model_name = data.get("model_name", f"{token}_{art_type}_V1")
|
| 152 |
-
prompts = data.get("prompts", [])
|
| 153 |
-
|
| 154 |
-
# -----------------------------------------------------------------------------
|
| 155 |
-
# 6. Выводим финальный JSON в stdout
|
| 156 |
-
# -----------------------------------------------------------------------------
|
| 157 |
-
out = {
|
| 158 |
-
"token": token,
|
| 159 |
-
"art_type": art_type,
|
| 160 |
-
"style_name": style_name,
|
| 161 |
-
"model_name": model_name,
|
| 162 |
-
"prompts": prompts
|
| 163 |
-
}
|
| 164 |
-
print(json.dumps(out, ensure_ascii=False))
|
|
|
|
|
|
|
|
|
|
|
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