File size: 2,472 Bytes
5b6f1b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9829a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b6f1b6
 
 
 
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
"""
Visual Question Answering Application - Run Script for Streamlit
"""

import os
import subprocess
import sys

# Configure minimal environment settings
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"  # Suppress TensorFlow logging


def check_requirements_installed():
    """Check if requirements are installed"""
    try:
        import streamlit
        import torch
        import transformers
        from PIL import Image

        return True
    except ImportError as e:
        print(f"Error: Required package not installed - {e}")
        print("Please install requirements using: pip install -r requirements.txt")
        return False


def ensure_directories():
    """Ensure all required directories exist"""
    # Get the base directory
    base_dir = os.path.dirname(os.path.abspath(__file__))

    # Create uploads directory
    uploads_dir = os.path.join(base_dir, "static", "uploads")
    os.makedirs(uploads_dir, exist_ok=True)
    print(f"Uploads directory: {uploads_dir}")

    # Create logs directory
    logs_dir = os.path.join(base_dir, "logs")
    os.makedirs(logs_dir, exist_ok=True)


def main():
    """Main function to run the application"""
    print("Visual Question Answering - Multi-Modal AI Application with Streamlit")

    # Check requirements
    if not check_requirements_installed():
        sys.exit(1)

    # Ensure directories exist
    ensure_directories()

    # Set environment variables
    os.environ["VQA_MODEL"] = os.environ.get(
        "VQA_MODEL", "blip"
    )  # Default to 'blip' model

    # Get the app.py path
    app_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "app.py")
    if not os.path.exists(app_path):
        print(f"Error: Streamlit app not found at {app_path}")
        sys.exit(1)

    # Print startup information
    port = int(os.environ.get("PORT", 8501))  # Streamlit default port is 8501
    print(f"Starting VQA application on http://localhost:{port}")
    print(f"Using VQA model: {os.environ.get('VQA_MODEL', 'blip')}")
    print("Press Ctrl+C to exit")

    # Run the Streamlit app
    cmd = [
        "streamlit",
        "run",
        app_path,
        "--server.port",
        str(port),
        "--server.address",
        "0.0.0.0",
    ]
    try:
        subprocess.run(cmd)
    except KeyboardInterrupt:
        print("\nShutting down the application...")
    except Exception as e:
        print(f"Error launching Streamlit: {e}")


if __name__ == "__main__":
    main()