Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
@@ -33,7 +33,17 @@ def setup_environment():
|
|
33 |
else:
|
34 |
print("β
NVIDIA_API_KEY found in Space secrets")
|
35 |
print(f" Key length: {len(api_key)} characters")
|
36 |
-
print(f" Key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Set CUDA device for Spaces (usually limited resources)
|
39 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
@@ -50,6 +60,17 @@ def setup_environment():
|
|
50 |
print(" or set the environment variable manually.")
|
51 |
else:
|
52 |
print(f"β
NVIDIA_API_KEY found locally (length: {len(api_key)})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
def main():
|
55 |
"""Main function to launch the application"""
|
|
|
33 |
else:
|
34 |
print("β
NVIDIA_API_KEY found in Space secrets")
|
35 |
print(f" Key length: {len(api_key)} characters")
|
36 |
+
print(f" Key starts with: {api_key[:8]}...")
|
37 |
+
print(f" Key ends with: ...{api_key[-4:]}")
|
38 |
+
print(f" Key is all uppercase: {'Yes' if api_key.isupper() else 'No'}")
|
39 |
+
|
40 |
+
# Check for common API key patterns
|
41 |
+
if api_key.startswith('nvapi-'):
|
42 |
+
print(" β
Key format appears correct (starts with 'nvapi-')")
|
43 |
+
elif api_key.startswith('nv-'):
|
44 |
+
print(" β
Key format appears correct (starts with 'nv-')")
|
45 |
+
else:
|
46 |
+
print(" β οΈ Key format may be incorrect (should start with 'nvapi-' or 'nv-')")
|
47 |
|
48 |
# Set CUDA device for Spaces (usually limited resources)
|
49 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
|
|
60 |
print(" or set the environment variable manually.")
|
61 |
else:
|
62 |
print(f"β
NVIDIA_API_KEY found locally (length: {len(api_key)})")
|
63 |
+
print(f" Key starts with: {api_key[:8]}...")
|
64 |
+
print(f" Key ends with: ...{api_key[-4:]}")
|
65 |
+
print(f" Key is all uppercase: {'Yes' if api_key.isupper() else 'No'}")
|
66 |
+
|
67 |
+
# Check for common API key patterns
|
68 |
+
if api_key.startswith('nvapi-'):
|
69 |
+
print(" β
Key format appears correct (starts with 'nvapi-')")
|
70 |
+
elif api_key.startswith('nv-'):
|
71 |
+
print(" β
Key format appears correct (starts with 'nv-')")
|
72 |
+
else:
|
73 |
+
print(" β οΈ Key format may be incorrect (should start with 'nvapi-' or 'nv-')")
|
74 |
|
75 |
def main():
|
76 |
"""Main function to launch the application"""
|
demo.py
CHANGED
@@ -105,6 +105,23 @@ def initialize_nvidia_client():
|
|
105 |
print(" For Hugging Face Spaces: Set NVIDIA_API_KEY in Repository Secrets")
|
106 |
return None
|
107 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
try:
|
109 |
client = OpenAI(
|
110 |
base_url=NVIDIA_BASE_URL,
|
@@ -118,6 +135,50 @@ def initialize_nvidia_client():
|
|
118 |
print(f"β Failed to initialize NVIDIA API client: {e}")
|
119 |
return None
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
# Initialize the client
|
122 |
client = initialize_nvidia_client()
|
123 |
|
@@ -171,7 +232,17 @@ def model_prompting(
|
|
171 |
# Debug information
|
172 |
api_key = os.getenv("NVIDIA_API_KEY")
|
173 |
if api_key:
|
174 |
-
print(f"π API Key available: {api_key[:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
else:
|
176 |
print("β No API key found in environment")
|
177 |
|
|
|
105 |
print(" For Hugging Face Spaces: Set NVIDIA_API_KEY in Repository Secrets")
|
106 |
return None
|
107 |
else:
|
108 |
+
# Validate the API key format
|
109 |
+
print("π Validating API key format...")
|
110 |
+
validation = validate_api_key(api_key)
|
111 |
+
|
112 |
+
if not validation['valid']:
|
113 |
+
print("β API key validation failed:")
|
114 |
+
for issue in validation['issues']:
|
115 |
+
print(f" - {issue}")
|
116 |
+
print(f" Key preview: {api_key[:8]}...{api_key[-4:]}")
|
117 |
+
print(" Please check your API key format and try again.")
|
118 |
+
return None
|
119 |
+
|
120 |
+
print("β
API key format validation passed")
|
121 |
+
print(f" Key format: {'β
' if validation['format_ok'] else 'β'}")
|
122 |
+
print(f" Key length: {'β
' if validation['length_ok'] else 'β'}")
|
123 |
+
print(f" Key charset: {'β
' if validation['charset_ok'] else 'β'}")
|
124 |
+
|
125 |
try:
|
126 |
client = OpenAI(
|
127 |
base_url=NVIDIA_BASE_URL,
|
|
|
135 |
print(f"β Failed to initialize NVIDIA API client: {e}")
|
136 |
return None
|
137 |
|
138 |
+
def validate_api_key(api_key: str) -> dict:
|
139 |
+
"""Validate the NVIDIA API key format and return detailed analysis"""
|
140 |
+
analysis = {
|
141 |
+
'valid': True,
|
142 |
+
'issues': [],
|
143 |
+
'format_ok': False,
|
144 |
+
'length_ok': False,
|
145 |
+
'charset_ok': False
|
146 |
+
}
|
147 |
+
|
148 |
+
if not api_key:
|
149 |
+
analysis['valid'] = False
|
150 |
+
analysis['issues'].append("API key is empty or None")
|
151 |
+
return analysis
|
152 |
+
|
153 |
+
# Check length (NVIDIA API keys are typically 40-50 characters)
|
154 |
+
if len(api_key) < 30 or len(api_key) > 60:
|
155 |
+
analysis['length_ok'] = False
|
156 |
+
analysis['issues'].append(f"Key length ({len(api_key)}) seems unusual (expected 30-60 chars)")
|
157 |
+
else:
|
158 |
+
analysis['length_ok'] = True
|
159 |
+
|
160 |
+
# Check format (should start with nvapi- or nv-)
|
161 |
+
if not (api_key.startswith('nvapi-') or api_key.startswith('nv-')):
|
162 |
+
analysis['format_ok'] = False
|
163 |
+
analysis['issues'].append("Key should start with 'nvapi-' or 'nv-'")
|
164 |
+
else:
|
165 |
+
analysis['format_ok'] = True
|
166 |
+
|
167 |
+
# Check for whitespace or special characters
|
168 |
+
if any(c.isspace() for c in api_key):
|
169 |
+
analysis['charset_ok'] = False
|
170 |
+
analysis['issues'].append("Key contains whitespace characters")
|
171 |
+
elif not all(c.isalnum() or c in '-_' for c in api_key):
|
172 |
+
analysis['charset_ok'] = False
|
173 |
+
analysis['issues'].append("Key contains invalid characters")
|
174 |
+
else:
|
175 |
+
analysis['charset_ok'] = True
|
176 |
+
|
177 |
+
# Overall validity
|
178 |
+
analysis['valid'] = analysis['format_ok'] and analysis['length_ok'] and analysis['charset_ok']
|
179 |
+
|
180 |
+
return analysis
|
181 |
+
|
182 |
# Initialize the client
|
183 |
client = initialize_nvidia_client()
|
184 |
|
|
|
232 |
# Debug information
|
233 |
api_key = os.getenv("NVIDIA_API_KEY")
|
234 |
if api_key:
|
235 |
+
print(f"π API Key available: {api_key[:8]}...{api_key[-4:]}")
|
236 |
+
print(f" Key length: {len(api_key)} characters")
|
237 |
+
|
238 |
+
# Validate the key
|
239 |
+
validation = validate_api_key(api_key)
|
240 |
+
if validation['valid']:
|
241 |
+
print(" β
Key format validation passed")
|
242 |
+
else:
|
243 |
+
print(" β Key format validation failed:")
|
244 |
+
for issue in validation['issues']:
|
245 |
+
print(f" - {issue}")
|
246 |
else:
|
247 |
print("β No API key found in environment")
|
248 |
|