stevafernandes commited on
Commit
7bf085e
·
verified ·
1 Parent(s): 0a3160e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -47
app.py CHANGED
@@ -47,8 +47,8 @@ if "video_file" not in st.session_state:
47
  st.session_state.video_file = None
48
  if "video_name" not in st.session_state:
49
  st.session_state.video_name = None
50
- if "api_key" not in st.session_state:
51
- st.session_state.api_key = ""
52
 
53
  # Main app function
54
  def main():
@@ -57,72 +57,65 @@ def main():
57
  st.markdown("---")
58
 
59
  # Step 1: API Key input
60
- st.subheader("Step 1: API Configuration")
61
 
62
  # Check for environment variable first
63
  env_api_key = os.environ.get("GOOGLE_API_KEY", "")
64
 
65
  if env_api_key:
66
  st.info("Using API key from environment variable")
67
- st.session_state.api_key = env_api_key
68
  api_key = env_api_key
 
 
 
 
 
 
 
 
 
69
  else:
70
- # Create columns for better layout
71
- col1, col2 = st.columns([3, 1])
 
 
 
 
 
 
72
 
73
- with col1:
74
- # API key input field
75
- api_key_input = st.text_input(
76
- "Gemini API Key",
77
- value=st.session_state.api_key,
78
- type="password",
79
- placeholder="AIzaSy...",
80
- help="Get your API key from https://makersuite.google.com/app/apikey"
81
- )
82
-
83
- with col2:
84
- # Save button
85
- st.markdown("<br>", unsafe_allow_html=True) # Add spacing
86
- if st.button("Save API Key", type="primary"):
87
- if api_key_input:
88
- st.session_state.api_key = api_key_input
89
- st.success("API key saved successfully")
90
- # Reinitialize processor with new key
91
- st.session_state.video_processor = None
92
- else:
93
- st.error("Please enter an API key")
94
-
95
- api_key = st.session_state.api_key
96
-
97
- # Show instructions if no API key
98
  if not api_key:
99
- st.warning("Please enter your Gemini API key to continue")
100
  with st.expander("How to obtain your API key"):
101
  st.markdown("""
102
  1. Navigate to [Google AI Studio](https://makersuite.google.com/app/apikey)
103
  2. Sign in with your Google account
104
  3. Click on "Get API key" or "Create API key"
105
  4. Copy the generated API key
106
- 5. Paste it in the field above and click "Save API Key"
107
 
108
  **Important:** Keep your API key secure and do not share it publicly.
109
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- if not api_key:
112
- st.error("API key required. Please enter your API key to proceed.")
 
113
  st.stop()
114
 
115
- # Initialize processor with the API key if not already initialized
116
- if api_key and not st.session_state.video_processor:
117
- try:
118
- st.session_state.video_processor = VideoProcessor(api_key)
119
- st.success("API key validated and processor initialized")
120
- except Exception as e:
121
- st.error(f"Invalid API key or initialization error: {str(e)}")
122
- st.session_state.api_key = ""
123
- st.session_state.video_processor = None
124
- st.stop()
125
-
126
  st.markdown("---")
127
 
128
  # Step 2: Upload Video
@@ -206,7 +199,7 @@ def main():
206
  st.rerun()
207
 
208
  with col3:
209
- if st.button("Clear API Key"):
210
  for key in list(st.session_state.keys()):
211
  del st.session_state[key]
212
  st.rerun()
 
47
  st.session_state.video_file = None
48
  if "video_name" not in st.session_state:
49
  st.session_state.video_name = None
50
+ if "api_key_validated" not in st.session_state:
51
+ st.session_state.api_key_validated = False
52
 
53
  # Main app function
54
  def main():
 
57
  st.markdown("---")
58
 
59
  # Step 1: API Key input
60
+ st.subheader("Step 1: Enter Your API Key")
61
 
62
  # Check for environment variable first
63
  env_api_key = os.environ.get("GOOGLE_API_KEY", "")
64
 
65
  if env_api_key:
66
  st.info("Using API key from environment variable")
 
67
  api_key = env_api_key
68
+ # Auto-validate environment API key
69
+ if not st.session_state.api_key_validated:
70
+ try:
71
+ st.session_state.video_processor = VideoProcessor(api_key)
72
+ st.session_state.api_key_validated = True
73
+ st.success("API key validated successfully")
74
+ except Exception as e:
75
+ st.error(f"Invalid API key from environment: {str(e)}")
76
+ api_key = None
77
  else:
78
+ # Direct API key input - users enter their key here
79
+ api_key = st.text_input(
80
+ "Enter your Gemini API key below:",
81
+ type="password",
82
+ placeholder="AIzaSy...",
83
+ help="Get your API key from https://makersuite.google.com/app/apikey",
84
+ key="api_key_input"
85
+ )
86
 
87
+ # Show instructions if no API key entered
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  if not api_key:
89
+ st.info("Please enter your Gemini API key above to proceed")
90
  with st.expander("How to obtain your API key"):
91
  st.markdown("""
92
  1. Navigate to [Google AI Studio](https://makersuite.google.com/app/apikey)
93
  2. Sign in with your Google account
94
  3. Click on "Get API key" or "Create API key"
95
  4. Copy the generated API key
96
+ 5. Paste it in the field above
97
 
98
  **Important:** Keep your API key secure and do not share it publicly.
99
  """)
100
+ else:
101
+ # Validate the API key when entered
102
+ if not st.session_state.api_key_validated or (st.session_state.video_processor is None):
103
+ try:
104
+ with st.spinner("Validating API key..."):
105
+ st.session_state.video_processor = VideoProcessor(api_key)
106
+ st.session_state.api_key_validated = True
107
+ st.success("API key validated successfully - You can proceed to Step 2")
108
+ except Exception as e:
109
+ st.error(f"Invalid API key. Please check your key and try again: {str(e)}")
110
+ st.session_state.api_key_validated = False
111
+ st.session_state.video_processor = None
112
+ api_key = None
113
 
114
+ # Stop here if no valid API key
115
+ if not api_key or not st.session_state.api_key_validated:
116
+ st.warning("A valid API key is required to proceed to Steps 2 and 3")
117
  st.stop()
118
 
 
 
 
 
 
 
 
 
 
 
 
119
  st.markdown("---")
120
 
121
  # Step 2: Upload Video
 
199
  st.rerun()
200
 
201
  with col3:
202
+ if st.button("Clear All & Start Over"):
203
  for key in list(st.session_state.keys()):
204
  del st.session_state[key]
205
  st.rerun()