File size: 3,102 Bytes
346d87a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from wrapper.chunk_loader import load_chunks, save_chunks
from wrapper.chunk_search import search_chunks
from wrapper.chunk_editor import update_chunk
from wrapper.chunk_player import play_chunk_audio
from wrapper.chunk_synthesizer import synthesize_chunk
from wrapper.chunk_revisions import accept_revision
import os
from config.config import AUDIOBOOK_ROOT
AUDIO_DIR = AUDIOBOOK_ROO

CHUNK_PATH = "Text_Input/my_book_chunks.json"


def run_chunk_repair_tool():
    print("\n๐Ÿ› ๏ธ Chunk Repair & Revision Tool")
    chunks = load_chunks(CHUNK_PATH)

    while True:
        query = input("\nSearch for text fragment (or 'Q' to quit): ").strip()
        if query.lower() == "q":
            print("Exiting revision tool.")
            break

        results = search_chunks(chunks, query)
        if not results:
            print("โŒ No matching chunks found.")
            continue

        print(f"\n๐Ÿ” Found {len(results)} match(es):")
        for i, chunk in enumerate(results):
            print(f"[{i}] \"{chunk['text'][:60]}...\" | Index: {chunk['index']}")

        sel = input("Select chunk index to revise: ").strip()
        if not sel.isdigit() or int(sel) >= len(results):
            print("Invalid selection.")
            continue

        chunk = results[int(sel)]
        index = chunk['index']
        chunk_path = os.path.join(AUDIO_DIR, f"chunk_{index:03}.wav")

        while True:
            print(f"\n๐Ÿ“ Chunk: \"{chunk['text']}\"")
            print(f"  Boundary: {chunk['boundary_type']}, Sentiment: {chunk.get('sentiment_score', 'N/A')}, Pause: {chunk.get('pause_duration', 'N/A')}")
            print("\nOptions:")
            print(" 1. Play original")
            print(" 2. Edit values")
            print(" 3. Resynthesize")
            print(" 4. Play revised")
            print(" 5. Accept revision")
            print(" 6. Back to search")

            choice = input("Enter option number: ").strip()
            if choice == "1":
                play_chunk_audio(chunk_path)
            elif choice == "2":
                boundary = input("New boundary type (or Enter to skip): ").strip()
                sentiment = input("New sentiment score (or Enter to skip): ").strip()
                pause = input("New pause duration (or Enter to skip): ").strip()

                update_chunk(
                    chunk,
                    boundary_type=boundary if boundary else None,
                    sentiment_score=float(sentiment) if sentiment else None,
                    pause_duration=float(pause) if pause else None
                )
                save_chunks(CHUNK_PATH, chunks)
            elif choice == "3":
                synthesize_chunk(chunk, index, revision=True)
            elif choice == "4":
                rev_path = os.path.join(AUDIO_DIR, f"chunk_{index:03}_rev.wav")
                play_chunk_audio(rev_path)
            elif choice == "5":
                accept_revision(index)
                break
            elif choice == "6":
                break
            else:
                print("Invalid input. Try again.")