|
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(): |
|
|
|
results = [] |
|
|
|
|
|
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}") |
|
|
|
|
|
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)}") |
|
|
|
|
|
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) |
|
results.append(f"{pdf_file}: Found ({size:.2f} MB)") |
|
|
|
|
|
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()}") |
|
|
|
|
|
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() |