chenemii commited on
Commit
d7deb0b
·
1 Parent(s): c00cb16
Files changed (2) hide show
  1. app/golf_swing_rag.py +55 -11
  2. app/streamlit_app.py +30 -3
app/golf_swing_rag.py CHANGED
@@ -210,21 +210,65 @@ class GolfSwingRAG:
210
 
211
  # Initialize OpenAI client using Streamlit secrets
212
  print("Initializing OpenAI client...")
 
 
 
213
  try:
214
- openai_key = st.secrets.get("openai", {}).get("api_key", "")
 
215
  if openai_key:
216
- self.openai_client = openai.OpenAI(api_key=openai_key)
217
- print("✓ OpenAI client initialized from Streamlit secrets")
218
  else:
219
- print("No OpenAI key found in Streamlit secrets")
220
- except (KeyError, FileNotFoundError, AttributeError) as e:
221
- print(f"Error accessing Streamlit secrets: {e}")
222
- # Fallback to environment variable if secrets not available
223
- if os.getenv("OPENAI_API_KEY"):
224
- self.openai_client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
225
- print("✓ OpenAI client initialized from environment variable")
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  else:
227
- print("No OpenAI API key found in environment variables either")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
  print("=== End GolfSwingRAG Initialization Debug ===")
230
  print("")
 
210
 
211
  # Initialize OpenAI client using Streamlit secrets
212
  print("Initializing OpenAI client...")
213
+ openai_key = None
214
+
215
+ # Try multiple methods to get the OpenAI API key
216
  try:
217
+ # Method 1: Direct access to OPENAI_API_KEY secret (most common for HF Spaces)
218
+ openai_key = st.secrets.get("OPENAI_API_KEY", "")
219
  if openai_key:
220
+ print("✓ Found OpenAI key in st.secrets['OPENAI_API_KEY']")
 
221
  else:
222
+ print("No key found in st.secrets['OPENAI_API_KEY']")
223
+ except Exception as e:
224
+ print(f"Error accessing st.secrets['OPENAI_API_KEY']: {e}")
225
+
226
+ # Method 2: Try nested openai structure (fallback)
227
+ if not openai_key:
228
+ try:
229
+ openai_key = st.secrets.get("openai", {}).get("api_key", "")
230
+ if openai_key:
231
+ print("✓ Found OpenAI key in nested st.secrets['openai']['api_key']")
232
+ else:
233
+ print("No key found in st.secrets['openai']['api_key']")
234
+ except Exception as e:
235
+ print(f"Error accessing nested openai secrets: {e}")
236
+
237
+ # Method 3: Environment variable fallback
238
+ if not openai_key:
239
+ openai_key = os.getenv("OPENAI_API_KEY", "")
240
+ if openai_key:
241
+ print("✓ Found OpenAI key in environment variable OPENAI_API_KEY")
242
  else:
243
+ print("No key found in environment variable OPENAI_API_KEY")
244
+
245
+ # Initialize client if we found a key
246
+ if openai_key and openai_key.startswith("sk-"):
247
+ try:
248
+ self.openai_client = openai.OpenAI(api_key=openai_key)
249
+ print("✓ OpenAI client initialized successfully")
250
+
251
+ # Test the client with a simple request to verify it works
252
+ try:
253
+ # Make a minimal test request
254
+ test_response = self.openai_client.chat.completions.create(
255
+ model="gpt-4o-mini",
256
+ messages=[{"role": "user", "content": "Hi"}],
257
+ max_tokens=5
258
+ )
259
+ print("✓ OpenAI client test successful")
260
+ except Exception as test_e:
261
+ print(f"⚠️ OpenAI client test failed: {test_e}")
262
+ self.openai_client = None
263
+
264
+ except Exception as e:
265
+ print(f"✗ Error initializing OpenAI client: {e}")
266
+ self.openai_client = None
267
+ else:
268
+ print("✗ No valid OpenAI API key found (should start with 'sk-')")
269
+ if openai_key:
270
+ print(f"Found key starting with: {openai_key[:10]}...")
271
+ self.openai_client = None
272
 
273
  print("=== End GolfSwingRAG Initialization Debug ===")
274
  print("")
app/streamlit_app.py CHANGED
@@ -256,11 +256,12 @@ Performance Estimates:
256
  with col1:
257
  # Removed subheader
258
 
259
- # Question input (removed label)
260
  question = st.text_area(
261
- "", # Removed label
262
  height=100,
263
- placeholder="Ask about your golf swing technique..."
 
264
  )
265
 
266
  # Removed settings section - using smart defaults instead
@@ -590,6 +591,32 @@ def main():
590
  st.title("Par-ity Project: Golf Swing Analysis 🏌️‍♀️")
591
  st.write("Founded to address the gender gap in golf participation and access to quality coaching resources, Par-ity Project is a technology-driven initiative empowering girls in golf through innovative AI based swing analysis. This technology uses computer vision and machine learning algorithms to analyze golf swings and provide personalized feedback to improve technique and performance.")
592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
  # Initialize session state for storing analysis results
594
  if 'video_analyzed' not in st.session_state:
595
  st.session_state.video_analyzed = False
 
256
  with col1:
257
  # Removed subheader
258
 
259
+ # Question input (with proper label)
260
  question = st.text_area(
261
+ "Question", # Proper label for accessibility
262
  height=100,
263
+ placeholder="Ask about your golf swing technique...",
264
+ label_visibility="collapsed" # Hide the label visually while keeping it for accessibility
265
  )
266
 
267
  # Removed settings section - using smart defaults instead
 
591
  st.title("Par-ity Project: Golf Swing Analysis 🏌️‍♀️")
592
  st.write("Founded to address the gender gap in golf participation and access to quality coaching resources, Par-ity Project is a technology-driven initiative empowering girls in golf through innovative AI based swing analysis. This technology uses computer vision and machine learning algorithms to analyze golf swings and provide personalized feedback to improve technique and performance.")
593
 
594
+ # Check and display OpenAI client status immediately
595
+ st.markdown("---")
596
+ st.subheader("🔧 System Status")
597
+
598
+ if RAG_AVAILABLE:
599
+ try:
600
+ # Create a temporary RAG instance to check OpenAI client status
601
+ with st.spinner("Checking OpenAI API connection..."):
602
+ temp_rag = GolfSwingRAG()
603
+
604
+ if temp_rag.openai_client:
605
+ st.success("✅ **OpenAI API Client**: Successfully initialized and tested")
606
+ st.info("💡 **ChatBot & AI Analysis**: Available - You can use the Golf Swing Chatbot and AI-powered improvement recommendations")
607
+ else:
608
+ st.error("❌ **OpenAI API Client**: Not available")
609
+ st.warning("⚠️ **Impact**: ChatBot will use fallback responses, AI analysis may be limited")
610
+ st.info("🔑 **Fix**: Check that your OPENAI_API_KEY is set correctly in Hugging Face Space secrets")
611
+ except Exception as e:
612
+ st.error(f"❌ **OpenAI API Client**: Error during initialization - {str(e)}")
613
+ st.warning("⚠️ **Impact**: ChatBot and AI features will use fallback mode")
614
+ else:
615
+ st.error("❌ **RAG System**: Not available due to missing dependencies")
616
+ st.warning("⚠️ **Impact**: ChatBot feature will be disabled")
617
+
618
+ st.markdown("---")
619
+
620
  # Initialize session state for storing analysis results
621
  if 'video_analyzed' not in st.session_state:
622
  st.session_state.video_analyzed = False