File size: 4,386 Bytes
f4c0f01
531fbee
 
ea4bdbe
8f83e1c
531fbee
 
8f83e1c
531fbee
 
 
 
8f83e1c
531fbee
bc85188
531fbee
 
 
6750126
531fbee
 
 
 
 
 
6750126
531fbee
6750126
531fbee
 
 
6750126
531fbee
c8b0d13
531fbee
 
 
 
 
 
 
ea4bdbe
531fbee
8f83e1c
531fbee
 
 
ea4bdbe
531fbee
8607988
531fbee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260ec72
531fbee
6750126
531fbee
 
 
c8b0d13
531fbee
 
 
c8b0d13
531fbee
 
 
 
 
 
 
 
64f3706
531fbee
6750126
531fbee
 
 
 
8f83e1c
531fbee
 
 
ea4bdbe
531fbee
8f83e1c
531fbee
 
 
ea4bdbe
531fbee
ea4bdbe
531fbee
 
 
ea4bdbe
531fbee
8607988
531fbee
5224f4e
8f83e1c
ea4bdbe
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
import os
import sys
import traceback
import gradio as gr

def import_with_error_tracking(module_name):
    """Try to import a module and return detailed error info if it fails"""
    try:
        module = __import__(module_name)
        return True, f"Successfully imported {module_name}"
    except ImportError as e:
        return False, f"Failed to import {module_name}: {str(e)}"
    except Exception as e:
        return False, f"Error importing {module_name}: {str(e)}"

def test_imports():
    # Create a report of all import attempts
    results = []
    
    # Core libraries
    for lib in ["torch", "numpy", "pandas", "tqdm", "PyPDF2", "transformers", 
                "sentence_transformers", "langchain", "langchain_community",
                "arabic_reshaper", "bidi", "rouge_score", "sacrebleu", "spaces"]:
        success, message = import_with_error_tracking(lib)
        results.append(f"{'βœ“' if success else 'βœ—'} {message}")
    
    # Check if specific model is available
    try:
        from transformers import AutoTokenizer
        tokenizer = AutoTokenizer.from_pretrained("ALLaM-AI/ALLaM-7B-Instruct-preview", trust_remote_code=True)
        results.append("βœ“ Model tokenizer accessible")
    except Exception as e:
        results.append(f"βœ— Model access error: {str(e)}")
    
    # Test vector store
    try:
        from langchain.embeddings import HuggingFaceEmbeddings
        from langchain_community.vectorstores import FAISS
        results.append("βœ“ Vector store components accessible")
    except Exception as e:
        results.append(f"βœ— Vector store error: {str(e)}")
    
    return "\n".join(results)

def check_pdfs():
    """Check if PDF files are readable"""
    results = []
    
    for pdf_file in ["saudi_vision203.pdf", "saudi_vision2030_ar.pdf"]:
        if not os.path.exists(pdf_file):
            results.append(f"{pdf_file}: Not found")
            continue
            
        size = os.path.getsize(pdf_file) / (1024 * 1024)  # Size in MB
        results.append(f"{pdf_file}: Found ({size:.2f} MB)")
        
        # Try to open and read the file
        try:
            import PyPDF2
            with open(pdf_file, 'rb') as f:
                reader = PyPDF2.PdfReader(f)
                num_pages = len(reader.pages)
                text_sample = reader.pages[0].extract_text()[:100] + "..."
                results.append(f"- Pages: {num_pages}")
                results.append(f"- Sample text: {text_sample}")
        except Exception as e:
            results.append(f"- Error reading file: {str(e)}")
    
    return "\n".join(results)

def check_environment():
    """Get information about the Python environment"""
    results = []
    
    results.append(f"Python version: {sys.version}")
    results.append(f"Python executable: {sys.executable}")
    results.append(f"Working directory: {os.getcwd()}")
    
    # List all installed packages
    try:
        import pkg_resources
        installed_packages = [f"{pkg.key}=={pkg.version}" for pkg in pkg_resources.working_set]
        results.append(f"Installed packages ({len(installed_packages)}):")
        results.append("\n".join(installed_packages))
    except:
        results.append("Could not list installed packages")
    
    return "\n".join(results)

def main():
    with gr.Blocks(title="Vision 2030 Assistant - Debug Mode") as interface:
        gr.Markdown("# Vision 2030 Assistant - Debug Mode")
        gr.Markdown("This interface helps identify import and initialization issues.")
        
        with gr.Tab("Import Testing"):
            test_btn = gr.Button("Test Imports")
            import_results = gr.Textbox(label="Import Test Results", lines=20)
            
            test_btn.click(test_imports, inputs=[], outputs=[import_results])
        
        with gr.Tab("PDF Testing"):
            pdf_btn = gr.Button("Test PDFs")
            pdf_results = gr.Textbox(label="PDF Test Results", lines=20)
            
            pdf_btn.click(check_pdfs, inputs=[], outputs=[pdf_results])
        
        with gr.Tab("Environment"):
            env_btn = gr.Button("Check Environment")
            env_results = gr.Textbox(label="Environment Information", lines=30)
            
            env_btn.click(check_environment, inputs=[], outputs=[env_results])
    
    interface.launch()

if __name__ == "__main__":
    main()