File size: 4,003 Bytes
ca6344d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     Script Name      : debug_tabs.py
     Placement        : HuggingFace Space β€” root/debug_tabs.py
     Type of Script   : Debug Utility
     Purpose          : Prints all gr.Tab registrations found in Space files.
                        Run this to diagnose duplicate tab issues.
                        Safe to delete after debugging.
     Version          : 1.0
     Usage            : python3 debug_tabs.py
     Last Updated     : 2026-03-10
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"""

import os
import ast
import sys

#==============================================================================
#  SCAN ALL .py FILES FOR gr.Tab() CALLS
#==============================================================================

SPACE_ROOT = os.path.dirname(os.path.abspath(__file__))

print("\n" + "="*70)
print("  SPIRAL CITY β€” TAB REGISTRATION DEBUG")
print("="*70)

py_files = [f for f in os.listdir(SPACE_ROOT) if f.endswith(".py")]
print(f"\nπŸ“ Found {len(py_files)} Python files: {py_files}\n")

tab_registry = []

for fname in sorted(py_files):
    fpath = os.path.join(SPACE_ROOT, fname)
    try:
        with open(fpath, "r", encoding="utf-8") as f:
            source = f.read()

        tree = ast.parse(source)
        found_tabs = []

        for node in ast.walk(tree):
            # Look for gr.Tab("...") calls
            if isinstance(node, ast.Call):
                func = node.func
                # gr.Tab(...)
                if (isinstance(func, ast.Attribute) and
                    func.attr == "Tab" and
                    isinstance(func.value, ast.Name) and
                    func.value.id == "gr"):
                    if node.args:
                        first_arg = node.args[0]
                        if isinstance(first_arg, ast.Constant):
                            tab_name = first_arg.value
                            found_tabs.append((node.lineno, tab_name))

        if found_tabs:
            print(f"πŸ“„ {fname}:")
            for lineno, tab_name in found_tabs:
                print(f"   Line {lineno:4d} β†’ gr.Tab(\"{tab_name}\")")
                tab_registry.append((fname, lineno, tab_name))
            print()
        else:
            print(f"πŸ“„ {fname}: no gr.Tab() calls\n")

    except Exception as e:
        print(f"❌ {fname}: parse error β€” {e}\n")

#==============================================================================
#  SUMMARY β€” LOOK FOR DUPLICATES
#==============================================================================

print("="*70)
print("  SUMMARY")
print("="*70)

from collections import Counter
tab_names = [t[2] for t in tab_registry]
counts = Counter(tab_names)

duplicates_found = False
for tab_name, count in counts.items():
    if count > 1:
        print(f"\n⚠️  DUPLICATE: \"{tab_name}\" appears {count} times!")
        for fname, lineno, name in tab_registry:
            if name == tab_name:
                print(f"     β†’ {fname} line {lineno}")
        duplicates_found = True

if not duplicates_found:
    print("\nβœ… No duplicate tabs found!")

print(f"\nπŸ“Š Total gr.Tab() calls across all files: {len(tab_registry)}")
print("\nFull tab list in registration order:")
for i, (fname, lineno, tab_name) in enumerate(tab_registry, 1):
    print(f"  {i:2d}. \"{tab_name}\" β€” {fname}:{lineno}")

print("\n" + "="*70)
print("  Also checking for demo.launch() calls (causes duplicate apps)")
print("="*70 + "\n")

for fname in sorted(py_files):
    fpath = os.path.join(SPACE_ROOT, fname)
    try:
        with open(fpath, "r", encoding="utf-8") as f:
            lines = f.readlines()
        for i, line in enumerate(lines, 1):
            if "demo.launch()" in line or ".launch()" in line:
                print(f"πŸš€ {fname} line {i}: {line.strip()}")
    except Exception as e:
        print(f"❌ {fname}: {e}")

print("\nβœ… Debug complete.\n")