Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
@@ -79,26 +79,38 @@ def main():
|
|
79 |
|
80 |
# Reinitialize NVIDIA client after environment setup
|
81 |
print("π Reinitializing NVIDIA API client...")
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
print("π Attempting to reinitialize client with environment variables...")
|
85 |
-
# Reinitialize the client
|
86 |
-
import demo
|
87 |
demo.client = initialize_nvidia_client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
# Test API connection
|
90 |
-
if os.getenv("NVIDIA_API_KEY"):
|
91 |
print("π§ͺ Testing NVIDIA API connection...")
|
92 |
test_nvidia_api_connection()
|
93 |
|
94 |
# Create the Gradio interface
|
95 |
print("π― Creating RoutePilot interface...")
|
96 |
-
|
97 |
|
98 |
# Launch the application
|
99 |
# For Hugging Face Spaces, we don't need to specify server_name and port
|
100 |
# The platform handles this automatically
|
101 |
-
|
102 |
show_error=True,
|
103 |
debug=False, # Set to False for production
|
104 |
show_api=False
|
|
|
79 |
|
80 |
# Reinitialize NVIDIA client after environment setup
|
81 |
print("π Reinitializing NVIDIA API client...")
|
82 |
+
|
83 |
+
# Import demo module and reinitialize client
|
84 |
+
import demo
|
85 |
+
print(f"π Initial client status: {'β
Available' if demo.client is not None else 'β None'}")
|
86 |
+
print(f"π API key in environment: {'β
Present' if os.getenv('NVIDIA_API_KEY') else 'β Missing'}")
|
87 |
+
|
88 |
+
if demo.client is None and os.getenv("NVIDIA_API_KEY"):
|
89 |
print("π Attempting to reinitialize client with environment variables...")
|
90 |
+
# Reinitialize the client and update the module variable
|
|
|
91 |
demo.client = initialize_nvidia_client()
|
92 |
+
print(f"π Client reinitialized: {'β
Success' if demo.client is not None else 'β Failed'}")
|
93 |
+
elif demo.client is not None:
|
94 |
+
print("β
Client already initialized")
|
95 |
+
else:
|
96 |
+
print("β No API key available for client initialization")
|
97 |
+
|
98 |
+
# Final client status check
|
99 |
+
print(f"π Final client status: {'β
Available' if demo.client is not None else 'β None'}")
|
100 |
|
101 |
# Test API connection
|
102 |
+
if os.getenv("NVIDIA_API_KEY") and demo.client is not None:
|
103 |
print("π§ͺ Testing NVIDIA API connection...")
|
104 |
test_nvidia_api_connection()
|
105 |
|
106 |
# Create the Gradio interface
|
107 |
print("π― Creating RoutePilot interface...")
|
108 |
+
demo_interface = create_interface()
|
109 |
|
110 |
# Launch the application
|
111 |
# For Hugging Face Spaces, we don't need to specify server_name and port
|
112 |
# The platform handles this automatically
|
113 |
+
demo_interface.launch(
|
114 |
show_error=True,
|
115 |
debug=False, # Set to False for production
|
116 |
show_api=False
|
demo.py
CHANGED
@@ -184,14 +184,15 @@ client = initialize_nvidia_client()
|
|
184 |
|
185 |
def test_nvidia_api_connection():
|
186 |
"""Test the NVIDIA API connection to verify authentication"""
|
187 |
-
|
|
|
188 |
print("β Cannot test API connection - client not initialized")
|
189 |
return False
|
190 |
|
191 |
try:
|
192 |
print("π§ͺ Testing NVIDIA API connection...")
|
193 |
# Make a simple test call
|
194 |
-
test_response =
|
195 |
model="meta/llama-3.1-8b-instruct",
|
196 |
messages=[{"role": "user", "content": "Hello"}],
|
197 |
max_tokens=10,
|
@@ -204,6 +205,28 @@ def test_nvidia_api_connection():
|
|
204 |
print(f"β NVIDIA API connection test failed: {e}")
|
205 |
return False
|
206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
def model_prompting(
|
208 |
llm_model: str,
|
209 |
prompt: str,
|
@@ -226,7 +249,9 @@ def model_prompting(
|
|
226 |
Returns:
|
227 |
Generated text response
|
228 |
"""
|
229 |
-
|
|
|
|
|
230 |
raise Exception("NVIDIA API client not initialized. Please check your .env file contains NVIDIA_API_KEY")
|
231 |
|
232 |
# Debug information
|
@@ -248,7 +273,7 @@ def model_prompting(
|
|
248 |
|
249 |
try:
|
250 |
print(f"π Making API call to model: {llm_model}")
|
251 |
-
completion =
|
252 |
model=llm_model,
|
253 |
messages=[{"role": "user", "content": prompt}],
|
254 |
max_tokens=max_token_num,
|
@@ -1038,6 +1063,13 @@ def process_query(query):
|
|
1038 |
try:
|
1039 |
print(f"Processing query: {query[:50]}...")
|
1040 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1041 |
# Use Graph Router to select the best LLM
|
1042 |
routed_llm_name, task_description, selection_info = get_routed_llm(query)
|
1043 |
print(f"Graph router selected: {routed_llm_name}")
|
|
|
184 |
|
185 |
def test_nvidia_api_connection():
|
186 |
"""Test the NVIDIA API connection to verify authentication"""
|
187 |
+
current_client = ensure_client_available()
|
188 |
+
if current_client is None:
|
189 |
print("β Cannot test API connection - client not initialized")
|
190 |
return False
|
191 |
|
192 |
try:
|
193 |
print("π§ͺ Testing NVIDIA API connection...")
|
194 |
# Make a simple test call
|
195 |
+
test_response = current_client.chat.completions.create(
|
196 |
model="meta/llama-3.1-8b-instruct",
|
197 |
messages=[{"role": "user", "content": "Hello"}],
|
198 |
max_tokens=10,
|
|
|
205 |
print(f"β NVIDIA API connection test failed: {e}")
|
206 |
return False
|
207 |
|
208 |
+
def ensure_client_available():
|
209 |
+
"""Ensure the NVIDIA API client is available, reinitialize if needed"""
|
210 |
+
global client
|
211 |
+
|
212 |
+
if client is not None:
|
213 |
+
return client
|
214 |
+
|
215 |
+
# Try to reinitialize the client
|
216 |
+
print("π Client not available, attempting to reinitialize...")
|
217 |
+
api_key = os.getenv("NVIDIA_API_KEY")
|
218 |
+
if api_key:
|
219 |
+
client = initialize_nvidia_client()
|
220 |
+
if client is not None:
|
221 |
+
print("β
Client successfully reinitialized")
|
222 |
+
return client
|
223 |
+
else:
|
224 |
+
print("β Failed to reinitialize client")
|
225 |
+
return None
|
226 |
+
else:
|
227 |
+
print("β No API key available for client initialization")
|
228 |
+
return None
|
229 |
+
|
230 |
def model_prompting(
|
231 |
llm_model: str,
|
232 |
prompt: str,
|
|
|
249 |
Returns:
|
250 |
Generated text response
|
251 |
"""
|
252 |
+
# Ensure client is available
|
253 |
+
current_client = ensure_client_available()
|
254 |
+
if current_client is None:
|
255 |
raise Exception("NVIDIA API client not initialized. Please check your .env file contains NVIDIA_API_KEY")
|
256 |
|
257 |
# Debug information
|
|
|
273 |
|
274 |
try:
|
275 |
print(f"π Making API call to model: {llm_model}")
|
276 |
+
completion = current_client.chat.completions.create(
|
277 |
model=llm_model,
|
278 |
messages=[{"role": "user", "content": prompt}],
|
279 |
max_tokens=max_token_num,
|
|
|
1063 |
try:
|
1064 |
print(f"Processing query: {query[:50]}...")
|
1065 |
|
1066 |
+
# Check client availability before processing
|
1067 |
+
print("π Checking client availability...")
|
1068 |
+
current_client = ensure_client_available()
|
1069 |
+
if current_client is None:
|
1070 |
+
raise Exception("NVIDIA API client not available. Please check your API key configuration.")
|
1071 |
+
print("β
Client available for processing")
|
1072 |
+
|
1073 |
# Use Graph Router to select the best LLM
|
1074 |
routed_llm_name, task_description, selection_info = get_routed_llm(query)
|
1075 |
print(f"Graph router selected: {routed_llm_name}")
|