Spaces:
Sleeping
Sleeping
Commit
·
2529e2a
1
Parent(s):
fb363a1
addling more detailed error logging
Browse files
helper.py
CHANGED
|
@@ -41,6 +41,7 @@ def generate_hypothetical_answer(query):
|
|
| 41 |
# Get API token from environment variable
|
| 42 |
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 43 |
if not api_token:
|
|
|
|
| 44 |
return "Error: HUGGINGFACE_API_TOKEN environment variable not set"
|
| 45 |
|
| 46 |
# Headers for the API request
|
|
@@ -71,17 +72,31 @@ def generate_hypothetical_answer(query):
|
|
| 71 |
try:
|
| 72 |
# Make the API request to Hugging Face
|
| 73 |
print("Sending request to Hugging Face API for hypothetical answer...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
start_time = time.time()
|
| 75 |
|
| 76 |
# Set a longer timeout (5 minutes)
|
| 77 |
response = requests.post(api_url, headers=headers, json=payload, timeout=300)
|
| 78 |
-
response.raise_for_status() # Raise an exception for HTTP errors
|
| 79 |
|
| 80 |
end_time = time.time()
|
| 81 |
print(f"Received hypothetical answer from Hugging Face API in {end_time - start_time:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# Parse the response
|
| 84 |
result = response.json()
|
|
|
|
| 85 |
|
| 86 |
# Extract the generated text
|
| 87 |
if isinstance(result, list) and len(result) > 0:
|
|
@@ -101,6 +116,13 @@ def generate_hypothetical_answer(query):
|
|
| 101 |
|
| 102 |
except requests.exceptions.HTTPError as e:
|
| 103 |
print(f"HTTP error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
if e.response.status_code == 401:
|
| 105 |
return "Authentication error. Please check your Hugging Face API token."
|
| 106 |
elif e.response.status_code == 429:
|
|
@@ -109,6 +131,8 @@ def generate_hypothetical_answer(query):
|
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Error generating hypothetical answer: {e}")
|
|
|
|
|
|
|
| 112 |
return "Failed to generate a hypothetical answer."
|
| 113 |
|
| 114 |
|
|
@@ -146,6 +170,7 @@ def query_llm_with_context(query, context, top_n=3):
|
|
| 146 |
# Get API token from environment variable
|
| 147 |
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 148 |
if not api_token:
|
|
|
|
| 149 |
return "Error: HUGGINGFACE_API_TOKEN environment variable not set"
|
| 150 |
|
| 151 |
# Headers for the API request
|
|
@@ -168,17 +193,31 @@ def query_llm_with_context(query, context, top_n=3):
|
|
| 168 |
try:
|
| 169 |
# Make the API request to Hugging Face
|
| 170 |
print("Sending request to Hugging Face API...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
start_time = time.time()
|
| 172 |
|
| 173 |
# Set a longer timeout (5 minutes)
|
| 174 |
response = requests.post(api_url, headers=headers, json=payload, timeout=300)
|
| 175 |
-
response.raise_for_status() # Raise an exception for HTTP errors
|
| 176 |
|
| 177 |
end_time = time.time()
|
| 178 |
print(f"Received response from Hugging Face API in {end_time - start_time:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
# Parse the response
|
| 181 |
result = response.json()
|
|
|
|
| 182 |
|
| 183 |
# Extract the generated text
|
| 184 |
if isinstance(result, list) and len(result) > 0:
|
|
@@ -198,6 +237,9 @@ def query_llm_with_context(query, context, top_n=3):
|
|
| 198 |
|
| 199 |
except requests.exceptions.HTTPError as e:
|
| 200 |
print(f"HTTP error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
| 201 |
if e.response.status_code == 401:
|
| 202 |
return "Authentication error. Please check your Hugging Face API token."
|
| 203 |
elif e.response.status_code == 429:
|
|
@@ -206,6 +248,8 @@ def query_llm_with_context(query, context, top_n=3):
|
|
| 206 |
|
| 207 |
except Exception as e:
|
| 208 |
print(f"Error querying LLM with context: {e}")
|
|
|
|
|
|
|
| 209 |
return "Failed to generate an answer with the provided context."
|
| 210 |
|
| 211 |
|
|
|
|
| 41 |
# Get API token from environment variable
|
| 42 |
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 43 |
if not api_token:
|
| 44 |
+
print("Error: HUGGINGFACE_API_TOKEN environment variable not set")
|
| 45 |
return "Error: HUGGINGFACE_API_TOKEN environment variable not set"
|
| 46 |
|
| 47 |
# Headers for the API request
|
|
|
|
| 72 |
try:
|
| 73 |
# Make the API request to Hugging Face
|
| 74 |
print("Sending request to Hugging Face API for hypothetical answer...")
|
| 75 |
+
print(f"API URL: {api_url}")
|
| 76 |
+
print(f"Headers: {headers}")
|
| 77 |
+
print(f"Payload: {json.dumps(payload, indent=2)}")
|
| 78 |
+
|
| 79 |
start_time = time.time()
|
| 80 |
|
| 81 |
# Set a longer timeout (5 minutes)
|
| 82 |
response = requests.post(api_url, headers=headers, json=payload, timeout=300)
|
|
|
|
| 83 |
|
| 84 |
end_time = time.time()
|
| 85 |
print(f"Received hypothetical answer from Hugging Face API in {end_time - start_time:.2f} seconds")
|
| 86 |
+
print(f"Response status code: {response.status_code}")
|
| 87 |
+
print(f"Response headers: {response.headers}")
|
| 88 |
+
|
| 89 |
+
# Try to print the response content for debugging
|
| 90 |
+
try:
|
| 91 |
+
print(f"Response content: {response.text[:1000]}...") # Print first 1000 chars
|
| 92 |
+
except:
|
| 93 |
+
print("Could not print response content")
|
| 94 |
+
|
| 95 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 96 |
|
| 97 |
# Parse the response
|
| 98 |
result = response.json()
|
| 99 |
+
print(f"Parsed response: {json.dumps(result, indent=2)[:1000]}...") # Print first 1000 chars
|
| 100 |
|
| 101 |
# Extract the generated text
|
| 102 |
if isinstance(result, list) and len(result) > 0:
|
|
|
|
| 116 |
|
| 117 |
except requests.exceptions.HTTPError as e:
|
| 118 |
print(f"HTTP error occurred: {e}")
|
| 119 |
+
print(f"Response status code: {e.response.status_code}")
|
| 120 |
+
print(f"Response headers: {e.response.headers}")
|
| 121 |
+
try:
|
| 122 |
+
print(f"Response content: {e.response.text}")
|
| 123 |
+
except:
|
| 124 |
+
print("Could not print response content")
|
| 125 |
+
|
| 126 |
if e.response.status_code == 401:
|
| 127 |
return "Authentication error. Please check your Hugging Face API token."
|
| 128 |
elif e.response.status_code == 429:
|
|
|
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
print(f"Error generating hypothetical answer: {e}")
|
| 134 |
+
import traceback
|
| 135 |
+
print(f"Traceback: {traceback.format_exc()}")
|
| 136 |
return "Failed to generate a hypothetical answer."
|
| 137 |
|
| 138 |
|
|
|
|
| 170 |
# Get API token from environment variable
|
| 171 |
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 172 |
if not api_token:
|
| 173 |
+
print("Error: HUGGINGFACE_API_TOKEN environment variable not set")
|
| 174 |
return "Error: HUGGINGFACE_API_TOKEN environment variable not set"
|
| 175 |
|
| 176 |
# Headers for the API request
|
|
|
|
| 193 |
try:
|
| 194 |
# Make the API request to Hugging Face
|
| 195 |
print("Sending request to Hugging Face API...")
|
| 196 |
+
print(f"API URL: {api_url}")
|
| 197 |
+
print(f"Headers: {headers}")
|
| 198 |
+
print(f"Payload: {json.dumps(payload, indent=2)}")
|
| 199 |
+
|
| 200 |
start_time = time.time()
|
| 201 |
|
| 202 |
# Set a longer timeout (5 minutes)
|
| 203 |
response = requests.post(api_url, headers=headers, json=payload, timeout=300)
|
|
|
|
| 204 |
|
| 205 |
end_time = time.time()
|
| 206 |
print(f"Received response from Hugging Face API in {end_time - start_time:.2f} seconds")
|
| 207 |
+
print(f"Response status code: {response.status_code}")
|
| 208 |
+
print(f"Response headers: {response.headers}")
|
| 209 |
+
|
| 210 |
+
# Try to print the response content for debugging
|
| 211 |
+
try:
|
| 212 |
+
print(f"Response content: {response.text[:1000]}...") # Print first 1000 chars
|
| 213 |
+
except:
|
| 214 |
+
print("Could not print response content")
|
| 215 |
+
|
| 216 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 217 |
|
| 218 |
# Parse the response
|
| 219 |
result = response.json()
|
| 220 |
+
print(f"Parsed response: {json.dumps(result, indent=2)[:1000]}...") # Print first 1000 chars
|
| 221 |
|
| 222 |
# Extract the generated text
|
| 223 |
if isinstance(result, list) and len(result) > 0:
|
|
|
|
| 237 |
|
| 238 |
except requests.exceptions.HTTPError as e:
|
| 239 |
print(f"HTTP error occurred: {e}")
|
| 240 |
+
print(f"Response status code: {e.response.status_code}")
|
| 241 |
+
print(f"Response headers: {e.response.headers}")
|
| 242 |
+
|
| 243 |
if e.response.status_code == 401:
|
| 244 |
return "Authentication error. Please check your Hugging Face API token."
|
| 245 |
elif e.response.status_code == 429:
|
|
|
|
| 248 |
|
| 249 |
except Exception as e:
|
| 250 |
print(f"Error querying LLM with context: {e}")
|
| 251 |
+
import traceback
|
| 252 |
+
print(f"Traceback: {traceback.format_exc()}")
|
| 253 |
return "Failed to generate an answer with the provided context."
|
| 254 |
|
| 255 |
|