Amarthya7 commited on
Commit
5b6f1b6
·
verified ·
1 Parent(s): 0757241

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +107 -90
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 main():
44
- """Main function to run the application"""
45
- print("Visual Question Answering - Multi-Modal AI Application with Streamlit")
46
-
47
- # Check requirements
48
- if not check_requirements_installed():
49
- sys.exit(1)
50
-
51
- # Ensure directories exist
52
- ensure_directories()
53
-
54
- # Set environment variables
55
- os.environ["VQA_MODEL"] = os.environ.get(
56
- "VQA_MODEL", "blip"
57
- ) # Default to 'blip' model
58
-
59
- # Get the app.py path
60
- app_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "app.py")
61
- if not os.path.exists(app_path):
62
- print(f"Error: Streamlit app not found at {app_path}")
63
- sys.exit(1)
64
-
65
- # Print startup information
66
- port = int(os.environ.get("PORT", 8501)) # Streamlit default port is 8501
67
- print(f"Starting VQA application on http://localhost:{port}")
68
- print(f"Using VQA model: {os.environ.get('VQA_MODEL', 'blip')}")
69
- print("Press Ctrl+C to exit")
70
-
71
- # Run the Streamlit app
72
- cmd = [
73
- "streamlit",
74
- "run",
75
- app_path,
76
- "--server.port",
77
- str(port),
78
- "--server.address",
79
- "0.0.0.0",
80
- ]
81
- try:
82
- subprocess.run(cmd)
83
- except KeyboardInterrupt:
84
- print("\nShutting down the application...")
85
- except Exception as e:
86
- print(f"Error launching Streamlit: {e}")
87
-
88
-
89
- if __name__ == "__main__":
90
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()