Co sądzisz o poniższym kodzie

# ================================================================
# ✅ KOMÓRKA 3: ANALIZA I DOPASOWANIE BRAKUJĄCYCH PLIKÓW (POPRAWA)
# ================================================================

import os, json, shutil, time
from pathlib import Path
from google import genai
from google.genai.types import Part
from google.cloud import storage

# === KONFIGURACJA ŚCIEŻEK ===
FOLDER_POPRAWA = Path("/content/drive/MyDrive/XTTS/EFEKTY/POPRAWA")
OUTPUT_JSON = Path("/content/drive/MyDrive/XTTS/AI_AUDIO_CATALOG.json")
BASE_AUDIO_OUT = Path("/content/drive/MyDrive/XTTS/EFEKTY")
GCS_BUCKET = "xtts-audio-temp"
GCS_SUBFOLDER = "ai_audio_poprawa"

# === INICJALIZACJA GEMINI ===
client = genai.Client(vertexai=True, project=os.environ["GOOGLE_CLOUD_PROJECT"], location=os.environ["GOOGLE_CLOUD_LOCATION"])
storage_client = storage.Client(project=os.environ["GOOGLE_CLOUD_PROJECT"])
bucket = storage_client.bucket(GCS_BUCKET)

# === WCZYTANIE ISTNIEJĄCEGO KATALOGU ===
if OUTPUT_JSON.exists():
    with open(OUTPUT_JSON, encoding="utf-8") as f:
        results = json.load(f)
else:
    results = []

# === FUNKCJE POMOCNICZE ===
def build_prompt(filename):
    return (
        f"""FILENAME: {filename}
        TYPE:
        SUBTYPE:
        DESCRIPTION:
        INTENSITY:
        USAGE_CONTEXT:
        LOOPABLE:
        EMOTION:
        SOUND_SHAPE:
        RECOMMENDED_LAYER:
        AI_TAGS:
        NEW_FILENAME:"""
    )

def parse_response(response_text):
    parsed = {}
    for line in response_text.strip().split("\n"):
        if ":" in line:
            key, value = line.strip().split(":", 1)
            parsed[key.strip().upper()] = value.strip()
    return parsed

# === GŁÓWNY PROCES PRZETWARZANIA ===
for audio_file in sorted(FOLDER_POPRAWA.glob("*.wav")):
    print(f"🔄 Przetwarzanie pliku: {audio_file.name}")

    blob_path = f"{GCS_SUBFOLDER}/{audio_file.name}"
    bucket.blob(blob_path).upload_from_filename(str(audio_file))
    gcs_uri = f"gs://{GCS_BUCKET}/{blob_path}"
    part = Part.from_uri(file_uri=gcs_uri, mime_type="audio/wav")

    prompt = build_prompt(audio_file.name)

    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=[prompt, part]
    )
    time.sleep(2)

    parsed = parse_response(response.text)

    if "FILENAME" in parsed and parsed["FILENAME"] == audio_file.name:
        type_value = parsed.get("TYPE", "POPRAWA")
        new_filename = parsed.get("NEW_FILENAME", audio_file.name)

        target_folder = BASE_AUDIO_OUT / type_value
        target_folder.mkdir(parents=True, exist_ok=True)

        shutil.move(audio_file, target_folder / new_filename)

        result_entry = {
            "filename": audio_file.name,
            "type": type_value,
            "subtype": parsed.get("SUBTYPE", ""),
            "description": parsed.get("DESCRIPTION", ""),
            "intensity": parsed.get("INTENSITY", ""),
            "usage_context": parsed.get("USAGE_CONTEXT", ""),
            "loopable": parsed.get("LOOPABLE", "false").lower() == "true",
            "emotion": parsed.get("EMOTION", ""),
            "sound_shape": parsed.get("SOUND_SHAPE", ""),
            "recommended_layer": parsed.get("RECOMMENDED_LAYER", ""),
            "ai_tags": [tag.strip() for tag in parsed.get("AI_TAGS", "").split(",")],
            "new_filename": new_filename,
            "relative_path": str(target_folder / new_filename),
            "ai_slug": new_filename
        }

        results.append(result_entry)
        print(f"✅ Przeniesiono i zapisano: {audio_file.name} → {type_value}/{new_filename}")
    else:
        print(f"❌ Nieprawidłowa odpowiedź AI dla pliku: {audio_file.name}")

    # Cleanup bucket
    bucket.blob(blob_path).delete()

# === ZAPIS ZAKTUALIZOWANYCH WYNIKÓW ===
with open(OUTPUT_JSON, "w", encoding="utf-8") as f:
    json.dump(results, f, indent=2, ensure_ascii=False)

print(f"🎯 Zaktualizowano katalog AI: {OUTPUT_JSON} – łączna liczba wpisów: {len(results)}")