Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -525,27 +525,59 @@ print("=" * 50)
|
|
| 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("
|
| 533 |
-
print("
|
| 534 |
-
print("=" * 50)
|
| 535 |
print("Please add it in:")
|
| 536 |
print("1. Hugging Face Space Settings → Repository secrets")
|
| 537 |
-
print("
|
| 538 |
-
print("
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 525 |
# ------------------------------
|
| 526 |
# Initialize Groq API Client
|
| 527 |
# ------------------------------
|
| 528 |
+
import sys
|
| 529 |
+
|
| 530 |
# Get API key from Hugging Face Secrets or environment variable
|
| 531 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "").strip()
|
| 532 |
+
|
| 533 |
+
print("=" * 50)
|
| 534 |
+
print("Checking Groq API Key...")
|
| 535 |
+
print("=" * 50)
|
| 536 |
|
| 537 |
if not GROQ_API_KEY:
|
| 538 |
+
print("❌ ERROR: GROQ_API_KEY not found in environment!")
|
| 539 |
+
print("")
|
|
|
|
| 540 |
print("Please add it in:")
|
| 541 |
print("1. Hugging Face Space Settings → Repository secrets")
|
| 542 |
+
print(" Name: GROQ_API_KEY")
|
| 543 |
+
print(" Value: Your Groq API key")
|
| 544 |
+
print("")
|
| 545 |
+
print("2. Get your free key from: https://console.groq.com/keys")
|
| 546 |
print("=" * 50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
groq_client = None
|
| 548 |
+
else:
|
| 549 |
+
print(f"✓ Groq API key found!")
|
| 550 |
+
print(f" Key length: {len(GROQ_API_KEY)} characters")
|
| 551 |
+
print(f" Starts with: {GROQ_API_KEY[:15]}...")
|
| 552 |
+
print(f" Ends with: ...{GROQ_API_KEY[-10:]}")
|
| 553 |
+
|
| 554 |
+
try:
|
| 555 |
+
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 556 |
+
|
| 557 |
+
# Test the connection with a simple request
|
| 558 |
+
print("Testing Groq API connection...")
|
| 559 |
+
test_response = groq_client.chat.completions.create(
|
| 560 |
+
messages=[{"role": "user", "content": "Hi"}],
|
| 561 |
+
model="llama-3.3-70b-versatile",
|
| 562 |
+
max_tokens=10
|
| 563 |
+
)
|
| 564 |
+
print("✓ Groq API client initialized and tested successfully!")
|
| 565 |
+
print("=" * 50)
|
| 566 |
+
|
| 567 |
+
except Exception as e:
|
| 568 |
+
print(f"❌ Failed to initialize Groq client!")
|
| 569 |
+
print(f"Error type: {type(e).__name__}")
|
| 570 |
+
print(f"Error message: {str(e)}")
|
| 571 |
+
print("=" * 50)
|
| 572 |
+
print("")
|
| 573 |
+
print("Common causes:")
|
| 574 |
+
print("1. Invalid API key - Get a new one from https://console.groq.com/keys")
|
| 575 |
+
print("2. API key has extra spaces - Check for whitespace")
|
| 576 |
+
print("3. Network issue - Check internet connection")
|
| 577 |
+
print("=" * 50)
|
| 578 |
+
groq_client = None
|
| 579 |
+
import traceback
|
| 580 |
+
traceback.print_exc()
|
| 581 |
|
| 582 |
# ------------------------------
|
| 583 |
# Load embedding model (CPU)
|