Spaces:
Runtime error
Runtime error
Update run.py
Browse files
run.py
CHANGED
@@ -1,90 +1,107 @@
|
|
1 |
-
"""
|
2 |
-
Visual Question Answering Application - Run Script for Streamlit
|
3 |
-
"""
|
4 |
-
|
5 |
-
import os
|
6 |
-
import subprocess
|
7 |
-
import sys
|
8 |
-
|
9 |
-
# Configure minimal environment settings
|
10 |
-
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" # Suppress TensorFlow logging
|
11 |
-
|
12 |
-
|
13 |
-
def check_requirements_installed():
|
14 |
-
"""Check if requirements are installed"""
|
15 |
-
try:
|
16 |
-
import streamlit
|
17 |
-
import torch
|
18 |
-
import transformers
|
19 |
-
from PIL import Image
|
20 |
-
|
21 |
-
return True
|
22 |
-
except ImportError as e:
|
23 |
-
print(f"Error: Required package not installed - {e}")
|
24 |
-
print("Please install requirements using: pip install -r requirements.txt")
|
25 |
-
return False
|
26 |
-
|
27 |
-
|
28 |
-
def ensure_directories():
|
29 |
-
"""Ensure all required directories exist"""
|
30 |
-
# Get the base directory
|
31 |
-
base_dir = os.path.dirname(os.path.abspath(__file__))
|
32 |
-
|
33 |
-
# Create uploads directory
|
34 |
-
uploads_dir = os.path.join(base_dir, "static", "uploads")
|
35 |
-
os.makedirs(uploads_dir, exist_ok=True)
|
36 |
-
print(f"Uploads directory: {uploads_dir}")
|
37 |
-
|
38 |
-
# Create logs directory
|
39 |
-
logs_dir = os.path.join(base_dir, "logs")
|
40 |
-
os.makedirs(logs_dir, exist_ok=True)
|
41 |
-
|
42 |
-
|
43 |
-
def
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Visual Question Answering Application - Run Script for Streamlit
|
3 |
+
"""
|
4 |
+
|
5 |
+
import os
|
6 |
+
import subprocess
|
7 |
+
import sys
|
8 |
+
|
9 |
+
# Configure minimal environment settings
|
10 |
+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" # Suppress TensorFlow logging
|
11 |
+
|
12 |
+
|
13 |
+
def check_requirements_installed():
|
14 |
+
"""Check if requirements are installed"""
|
15 |
+
try:
|
16 |
+
import streamlit
|
17 |
+
import torch
|
18 |
+
import transformers
|
19 |
+
from PIL import Image
|
20 |
+
|
21 |
+
return True
|
22 |
+
except ImportError as e:
|
23 |
+
print(f"Error: Required package not installed - {e}")
|
24 |
+
print("Please install requirements using: pip install -r requirements.txt")
|
25 |
+
return False
|
26 |
+
|
27 |
+
|
28 |
+
def ensure_directories():
|
29 |
+
"""Ensure all required directories exist"""
|
30 |
+
# Get the base directory
|
31 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
32 |
+
|
33 |
+
# Create uploads directory
|
34 |
+
uploads_dir = os.path.join(base_dir, "static", "uploads")
|
35 |
+
os.makedirs(uploads_dir, exist_ok=True)
|
36 |
+
print(f"Uploads directory: {uploads_dir}")
|
37 |
+
|
38 |
+
# Create logs directory
|
39 |
+
logs_dir = os.path.join(base_dir, "logs")
|
40 |
+
os.makedirs(logs_dir, exist_ok=True)
|
41 |
+
|
42 |
+
|
43 |
+
def is_huggingface_space():
|
44 |
+
"""Check if the app is running on Hugging Face Spaces"""
|
45 |
+
return "SPACE_ID" in os.environ
|
46 |
+
|
47 |
+
|
48 |
+
def main():
|
49 |
+
"""Main function to run the application"""
|
50 |
+
print("Visual Question Answering - Multi-Modal AI Application with Streamlit")
|
51 |
+
|
52 |
+
# Check requirements
|
53 |
+
if not check_requirements_installed():
|
54 |
+
sys.exit(1)
|
55 |
+
|
56 |
+
# Ensure directories exist
|
57 |
+
ensure_directories()
|
58 |
+
|
59 |
+
# Set environment variables
|
60 |
+
os.environ["VQA_MODEL"] = os.environ.get(
|
61 |
+
"VQA_MODEL", "blip"
|
62 |
+
) # Default to 'blip' model
|
63 |
+
|
64 |
+
# Get the app.py path
|
65 |
+
app_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "app.py")
|
66 |
+
if not os.path.exists(app_path):
|
67 |
+
print(f"Error: Streamlit app not found at {app_path}")
|
68 |
+
sys.exit(1)
|
69 |
+
|
70 |
+
# Print startup information
|
71 |
+
port = int(os.environ.get("PORT", 8501)) # Streamlit default port is 8501
|
72 |
+
print(f"Starting VQA application on http://localhost:{port}")
|
73 |
+
print(f"Using VQA model: {os.environ.get('VQA_MODEL', 'blip')}")
|
74 |
+
print("Press Ctrl+C to exit")
|
75 |
+
|
76 |
+
# Run the Streamlit app
|
77 |
+
if is_huggingface_space():
|
78 |
+
# In Hugging Face Spaces, we don't need to launch Streamlit manually
|
79 |
+
# Just import the app and it will be picked up by Streamlit when this script is run
|
80 |
+
print("Running in Hugging Face Spaces environment")
|
81 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
82 |
+
try:
|
83 |
+
# This will make the app importable by Streamlit
|
84 |
+
pass
|
85 |
+
except Exception as e:
|
86 |
+
print(f"Error importing app.py: {e}")
|
87 |
+
else:
|
88 |
+
# For local development, we launch Streamlit via subprocess
|
89 |
+
cmd = [
|
90 |
+
"streamlit",
|
91 |
+
"run",
|
92 |
+
app_path,
|
93 |
+
"--server.port",
|
94 |
+
str(port),
|
95 |
+
"--server.address",
|
96 |
+
"0.0.0.0",
|
97 |
+
]
|
98 |
+
try:
|
99 |
+
subprocess.run(cmd)
|
100 |
+
except KeyboardInterrupt:
|
101 |
+
print("\nShutting down the application...")
|
102 |
+
except Exception as e:
|
103 |
+
print(f"Error launching Streamlit: {e}")
|
104 |
+
|
105 |
+
|
106 |
+
if __name__ == "__main__":
|
107 |
+
main()
|