Amarthya7's picture
Update run.py
497daff verified
raw
history blame
2.48 kB
"""
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", 7860)) # Set default port to 7860 for Hugging Face
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()