Shreyas094 commited on
Commit
550c8e2
·
verified ·
1 Parent(s): d3d8817

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -131,15 +131,38 @@ def generate_cloudflare_response(prompt, max_tokens, temperature):
131
  "temperature": temperature
132
  }
133
  )
 
 
 
 
134
  result = response.json()
135
- if 'result' in result and 'response' in result['result']:
136
- return result['result']['response']
137
- else:
138
- print(f"Unexpected response format: {result}")
139
- return "Error: Unexpected response format from Cloudflare API"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  except Exception as e:
141
- print(f"Error in generating Cloudflare response: {str(e)}")
142
- return f"Error: {str(e)}"
 
143
 
144
 
145
  def duckduckgo_search(query):
 
131
  "temperature": temperature
132
  }
133
  )
134
+
135
+ # Check if the request was successful
136
+ response.raise_for_status()
137
+
138
  result = response.json()
139
+ if not result:
140
+ raise ValueError("Empty response from Cloudflare API")
141
+
142
+ if 'result' not in result:
143
+ raise ValueError(f"Unexpected response format. 'result' key missing. Response: {result}")
144
+
145
+ if 'response' not in result['result']:
146
+ raise ValueError(f"Unexpected response format. 'response' key missing. Result: {result['result']}")
147
+
148
+ return result['result']['response']
149
+
150
+ except requests.exceptions.RequestException as e:
151
+ error_message = f"Network error when calling Cloudflare API: {str(e)}"
152
+ print(error_message)
153
+ return f"Error: {error_message}"
154
+ except json.JSONDecodeError as e:
155
+ error_message = f"Error decoding JSON response from Cloudflare API: {str(e)}"
156
+ print(error_message)
157
+ return f"Error: {error_message}"
158
+ except ValueError as e:
159
+ error_message = str(e)
160
+ print(error_message)
161
+ return f"Error: {error_message}"
162
  except Exception as e:
163
+ error_message = f"Unexpected error in generate_cloudflare_response: {str(e)}"
164
+ print(error_message)
165
+ return f"Error: {error_message}"
166
 
167
 
168
  def duckduckgo_search(query):