Redfire-1234 commited on
Commit
55658d1
ยท
verified ยท
1 Parent(s): a3e6552

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -505,6 +505,7 @@
505
  # print(f"\n๐Ÿš€ Starting Flask on 0.0.0.0:{port}\n")
506
  # app.run(host="0.0.0.0", port=port, debug=False)
507
 
 
508
  import pickle
509
  import faiss
510
  from flask import Flask, request, jsonify, render_template_string
@@ -524,14 +525,27 @@ print("=" * 50)
524
  # ------------------------------
525
  # Initialize Groq API Client
526
  # ------------------------------
527
- # Get your free API key from: https://console.groq.com/keys
528
- GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "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") # Set this in your environment
 
529
  if not GROQ_API_KEY:
530
- print("โš ๏ธ WARNING: GROQ_API_KEY not set. Get one from https://console.groq.com/keys")
531
- print("Set it with: export GROQ_API_KEY='your-key-here'")
 
 
 
 
 
 
 
 
532
 
533
- groq_client = Groq(api_key=GROQ_API_KEY)
534
- print("โœ“ Groq API client initialized")
 
 
 
 
535
 
536
  # ------------------------------
537
  # Load embedding model (CPU)
@@ -622,6 +636,10 @@ def rag_search(query, subject, k=5):
622
  # MCQ Generation using Groq API
623
  # ------------------------------
624
  def generate_mcqs(context, topic, subject):
 
 
 
 
625
  # Check cache first
626
  context_hash = hashlib.md5(context.encode()).hexdigest()[:8]
627
  cache_key = get_cache_key(topic, subject, context_hash)
@@ -1009,5 +1027,4 @@ def clear_cache():
1009
  if __name__ == "__main__":
1010
  port = int(os.environ.get("PORT", 7860))
1011
  print(f"\n๐Ÿš€ Starting Flask on 0.0.0.0:{port}\n")
1012
- app.run(host="0.0.0.0", port=port, debug=False)
1013
-
 
505
  # print(f"\n๐Ÿš€ Starting Flask on 0.0.0.0:{port}\n")
506
  # app.run(host="0.0.0.0", port=port, debug=False)
507
 
508
+
509
  import pickle
510
  import faiss
511
  from flask import Flask, request, jsonify, render_template_string
 
525
  # ------------------------------
526
  # Initialize Groq API Client
527
  # ------------------------------
528
+ # Get API key from Hugging Face Secrets or environment variable
529
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
530
+
531
  if not GROQ_API_KEY:
532
+ print("=" * 50)
533
+ print("โš ๏ธ ERROR: GROQ_API_KEY not found!")
534
+ print("=" * 50)
535
+ print("Please add it in:")
536
+ print("1. Hugging Face Space Settings โ†’ Repository secrets")
537
+ print("2. Or set environment variable: export GROQ_API_KEY='your-key'")
538
+ print("Get your free key from: https://console.groq.com/keys")
539
+ print("=" * 50)
540
+ else:
541
+ print(f"โœ“ Groq API key found (starts with: {GROQ_API_KEY[:10]}...)")
542
 
543
+ try:
544
+ groq_client = Groq(api_key=GROQ_API_KEY)
545
+ print("โœ“ Groq API client initialized successfully")
546
+ except Exception as e:
547
+ print(f"โŒ Failed to initialize Groq client: {e}")
548
+ groq_client = None
549
 
550
  # ------------------------------
551
  # Load embedding model (CPU)
 
636
  # MCQ Generation using Groq API
637
  # ------------------------------
638
  def generate_mcqs(context, topic, subject):
639
+ # Check if Groq client is available
640
+ if not groq_client:
641
+ return "Error: Groq API client not initialized. Please check your GROQ_API_KEY in Space settings."
642
+
643
  # Check cache first
644
  context_hash = hashlib.md5(context.encode()).hexdigest()[:8]
645
  cache_key = get_cache_key(topic, subject, context_hash)
 
1027
  if __name__ == "__main__":
1028
  port = int(os.environ.get("PORT", 7860))
1029
  print(f"\n๐Ÿš€ Starting Flask on 0.0.0.0:{port}\n")
1030
+ app.run(host="0.0.0.0", port=port, debug=False)