File size: 8,714 Bytes
6db3cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60df088
6db3cca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env bash
# Batch-extract PDFs to markdown via local Nougat Gradio API.
#
# Usage:
#   batch_extract.sh --input ~/papers/ --output ~/formulas/
#   batch_extract.sh --input ~/papers/ --output ~/formulas/ --port 7860,7861,7862
#   batch_extract.sh --input ~/papers/ --output ~/formulas/ --host 10.0.0.1 --force
#
# Requires: bash, curl, python3
set -uo pipefail

# --- Defaults ---
INPUT_DIR=""
OUTPUT_DIR=""
PORTS="7860"
HOST="localhost"
FORCE=false
TIMEOUT=600

# --- Counters ---
TOTAL=0
EXTRACTED=0
SKIPPED=0
FAILED=0
TOTAL_CHARS=0
TOTAL_DISPLAY_EQ=0
TOTAL_INLINE_EQ=0
FAILED_FILES=()

usage() {
    cat <<'USAGE'
Usage: batch_extract.sh [OPTIONS]

Required:
  --input  DIR    Directory containing PDF files
  --output DIR    Directory for markdown output files

Optional:
  --port   PORTS  Comma-separated ports (default: 7860)
                  Multiple ports enable parallel extraction
  --host   HOST   Server hostname/IP (default: localhost)
  --force         Re-extract even if output .md exists
  --timeout SECS  Max seconds per PDF (default: 600)
  --help          Show this help
USAGE
    exit 0
}

# --- Argument parsing ---
while [[ $# -gt 0 ]]; do
    case "$1" in
        --input)   INPUT_DIR="$2"; shift 2 ;;
        --output)  OUTPUT_DIR="$2"; shift 2 ;;
        --port)    PORTS="$2"; shift 2 ;;
        --host)    HOST="$2"; shift 2 ;;
        --force)   FORCE=true; shift ;;
        --timeout) TIMEOUT="$2"; shift 2 ;;
        --help)    usage ;;
        *) echo "Unknown argument: $1" >&2; exit 1 ;;
    esac
done

if [[ -z "$INPUT_DIR" || -z "$OUTPUT_DIR" ]]; then
    echo "Error: --input and --output are required" >&2
    usage
fi

if [[ ! -d "$INPUT_DIR" ]]; then
    echo "Error: input directory does not exist: $INPUT_DIR" >&2
    exit 1
fi

mkdir -p "$OUTPUT_DIR"

# --- Split ports ---
IFS=',' read -ra PORT_ARRAY <<< "$PORTS"
NUM_PORTS=${#PORT_ARRAY[@]}

# --- Health check ---
check_server() {
    local port="$1"
    local url="http://${HOST}:${port}/"
    if ! curl -sf --connect-timeout 5 -o /dev/null "$url"; then
        echo "Error: Nougat not reachable at $url" >&2
        return 1
    fi
}

echo "Checking $NUM_PORTS server(s)..."
for port in "${PORT_ARRAY[@]}"; do
    if ! check_server "$port"; then
        exit 1
    fi
    echo "  Port $port: OK"
done

# --- Core extraction ---
extract_pdf() {
    local pdf_path="$1"
    local output_path="$2"
    local port="$3"
    local base_url="http://${HOST}:${port}/gradio_api"
    local pdf_name
    pdf_name="$(basename "$pdf_path")"

    # Upload
    local upload
    upload=$(curl -sf --max-time 30 -X POST "$base_url/upload" \
        -F "files=@\"$pdf_path\"") || { echo "FAIL upload: $pdf_name" >&2; return 1; }

    local fpath
    fpath=$(echo "$upload" | python3 -c "import sys,json; print(json.load(sys.stdin)[0])") \
        || { echo "FAIL parse upload: $pdf_name" >&2; return 1; }

    # Call inference
    local event
    event=$(curl -sf --max-time 30 -X POST "$base_url/call/inference" \
        -H "Content-Type: application/json" \
        -d "{\"data\": [{\"path\": \"$fpath\", \"orig_name\": \"$pdf_name\", \"meta\": {\"_type\": \"gradio.FileData\"}}, \"\"]}") \
        || { echo "FAIL inference call: $pdf_name" >&2; return 1; }

    local event_id
    event_id=$(echo "$event" | python3 -c "import sys,json; print(json.load(sys.stdin)['event_id'])") \
        || { echo "FAIL parse event_id: $pdf_name" >&2; return 1; }

    # Poll result
    local result
    result=$(curl -sf --max-time "$TIMEOUT" -N "$base_url/call/inference/$event_id") \
        || { echo "FAIL polling: $pdf_name (timeout=${TIMEOUT}s)" >&2; return 1; }

    # Parse SSE and save
    echo "$result" | python3 -c "
import sys, json, re

lines = sys.stdin.read().strip().split('\n')
data_lines = [l for l in lines if l.startswith('data:')]
if not data_lines:
    print('ERROR: no data lines', file=sys.stderr)
    sys.exit(1)
last = data_lines[-1].replace('data: ', '', 1)
data = json.loads(last, strict=False)
text = data[0]

display = len(re.findall(r'\\\$\\\$(.+?)\\\$\\\$', text, re.DOTALL))
inline = len(re.findall(r'(?<!\\\$)\\\$(?!\\\$)(.+?)(?<!\\\$)\\\$(?!\\\$)', text))

# Stats line to stderr: chars,display,inline
print(f'{len(text)},{display},{inline}', file=sys.stderr)
print(text)
" > "$output_path" 2>"${output_path}.stats"

    local exit_code=$?
    if [[ $exit_code -ne 0 ]]; then
        echo "FAIL parse: $pdf_name" >&2
        rm -f "$output_path" "${output_path}.stats"
        return 1
    fi

    # Read stats
    local stats
    stats=$(cat "${output_path}.stats")
    rm -f "${output_path}.stats"
    echo "$stats"
    return 0
}

# --- Build file list ---
mapfile -t PDF_FILES < <(find "$INPUT_DIR" -maxdepth 1 -name '*.pdf' -type f | sort)
TOTAL=${#PDF_FILES[@]}

if [[ $TOTAL -eq 0 ]]; then
    echo "No PDF files found in $INPUT_DIR"
    exit 0
fi

echo ""
echo "Found $TOTAL PDF(s) in $INPUT_DIR"
echo "Output: $OUTPUT_DIR"
echo "Ports: ${PORTS} (${NUM_PORTS} instance(s))"
echo ""

# --- Process PDFs ---
process_pdf() {
    local idx="$1"
    local pdf="$2"
    local port="$3"
    local pdf_name
    pdf_name="$(basename "$pdf")"
    local md_name="${pdf_name%.pdf}.md"
    local output_path="$OUTPUT_DIR/$md_name"

    # Skip check
    if [[ "$FORCE" != true && -f "$output_path" ]]; then
        echo "[$idx/$TOTAL] SKIP $pdf_name (exists)"
        echo "SKIPPED"
        return 0
    fi

    echo "[$idx/$TOTAL] Extracting $pdf_name (port $port)..."
    local stats
    stats=$(extract_pdf "$pdf" "$output_path" "$port")
    local rc=$?

    if [[ $rc -ne 0 ]]; then
        echo "[$idx/$TOTAL] FAILED $pdf_name"
        echo "FAILED:$pdf_name"
        return 1
    fi

    local chars display inline
    IFS=',' read -r chars display inline <<< "$stats"
    echo "[$idx/$TOTAL] OK $pdf_name${chars} chars, ${display} display + ${inline} inline eqs"
    echo "OK:${chars}:${display}:${inline}"
    return 0
}

START_TIME=$SECONDS

if [[ $NUM_PORTS -eq 1 ]]; then
    # Sequential mode
    for i in "${!PDF_FILES[@]}"; do
        idx=$((i + 1))
        result=$(process_pdf "$idx" "${PDF_FILES[$i]}" "${PORT_ARRAY[0]}")
        status=$(echo "$result" | tail -1)

        case "$status" in
            SKIPPED)        ((SKIPPED++)) ;;
            FAILED:*)       ((FAILED++)); FAILED_FILES+=("${status#FAILED:}") ;;
            OK:*)
                ((EXTRACTED++))
                IFS=':' read -r _ chars display inline <<< "$status"
                TOTAL_CHARS=$((TOTAL_CHARS + chars))
                TOTAL_DISPLAY_EQ=$((TOTAL_DISPLAY_EQ + display))
                TOTAL_INLINE_EQ=$((TOTAL_INLINE_EQ + inline))
                ;;
        esac
    done
else
    # Parallel mode: round-robin across ports
    TMPDIR_RESULTS=$(mktemp -d)
    trap 'rm -rf "$TMPDIR_RESULTS"' EXIT

    running=0
    for i in "${!PDF_FILES[@]}"; do
        idx=$((i + 1))
        port_idx=$((i % NUM_PORTS))
        port="${PORT_ARRAY[$port_idx]}"

        (
            result=$(process_pdf "$idx" "${PDF_FILES[$i]}" "$port")
            status=$(echo "$result" | tail -1)
            echo "$status" > "$TMPDIR_RESULTS/$idx.result"
        ) &

        ((running++))
        if [[ $running -ge $NUM_PORTS ]]; then
            wait -n 2>/dev/null || wait
            ((running--))
        fi
    done
    wait

    # Collect results
    for i in "${!PDF_FILES[@]}"; do
        idx=$((i + 1))
        if [[ -f "$TMPDIR_RESULTS/$idx.result" ]]; then
            status=$(cat "$TMPDIR_RESULTS/$idx.result")
            case "$status" in
                SKIPPED)        ((SKIPPED++)) ;;
                FAILED:*)       ((FAILED++)); FAILED_FILES+=("${status#FAILED:}") ;;
                OK:*)
                    ((EXTRACTED++))
                    IFS=':' read -r _ chars display inline <<< "$status"
                    TOTAL_CHARS=$((TOTAL_CHARS + chars))
                    TOTAL_DISPLAY_EQ=$((TOTAL_DISPLAY_EQ + display))
                    TOTAL_INLINE_EQ=$((TOTAL_INLINE_EQ + inline))
                    ;;
            esac
        else
            ((FAILED++))
        fi
    done
fi

ELAPSED=$((SECONDS - START_TIME))

# --- Summary ---
echo ""
echo "=============================="
echo "BATCH EXTRACTION COMPLETE"
echo "=============================="
echo "Total PDFs:       $TOTAL"
echo "Extracted:        $EXTRACTED"
echo "Skipped:          $SKIPPED"
echo "Failed:           $FAILED"
echo "Total chars:      $TOTAL_CHARS"
echo "Display equations: $TOTAL_DISPLAY_EQ"
echo "Inline equations: $TOTAL_INLINE_EQ"
echo "Elapsed:          ${ELAPSED}s"

if [[ $FAILED -gt 0 ]]; then
    echo ""
    echo "Failed files:"
    for f in "${FAILED_FILES[@]}"; do
        echo "  - $f"
    done
fi