Update app.py
Browse files
app.py
CHANGED
|
@@ -15,9 +15,11 @@ async def generate_solution_python(user_query):
|
|
| 15 |
|
| 16 |
Args:
|
| 17 |
user_query (str): The query provided by the user.
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
if not user_query:
|
| 20 |
-
return "
|
| 21 |
|
| 22 |
print(f"Processing query: {user_query}")
|
| 23 |
response_text = ""
|
|
@@ -32,6 +34,8 @@ async def generate_solution_python(user_query):
|
|
| 32 |
|
| 33 |
# In a real scenario, you'd make an API call like this (example with a hypothetical search API):
|
| 34 |
# search_api_key = os.environ.get("YOUR_SEARCH_API_KEY")
|
|
|
|
|
|
|
| 35 |
# search_api_url = "https://api.example.com/search"
|
| 36 |
# search_response = requests.get(search_api_url, params={"q": user_query, "api_key": search_api_key})
|
| 37 |
# search_response.raise_for_status()
|
|
@@ -85,17 +89,17 @@ Please provide a detailed and helpful solution, incorporating the provided infor
|
|
| 85 |
response_text = "No solution could be generated. Please try a different query."
|
| 86 |
|
| 87 |
except requests.exceptions.RequestException as e:
|
| 88 |
-
error_message = f"Network or API error: {e}"
|
| 89 |
print(f"Error: {error_message}")
|
| 90 |
-
response_text = f"An error occurred: {error_message}. Please check the logs
|
| 91 |
except ValueError as e:
|
| 92 |
-
error_message = f"Configuration error: {e}"
|
| 93 |
print(f"Error: {error_message}")
|
| 94 |
-
response_text = f"
|
| 95 |
except Exception as e:
|
| 96 |
-
error_message = f"An unexpected error occurred: {e}"
|
| 97 |
print(f"Error: {error_message}")
|
| 98 |
-
response_text = f"An error occurred: {error_message}. Please check the logs
|
| 99 |
|
| 100 |
return response_text
|
| 101 |
|
|
@@ -109,14 +113,24 @@ def index():
|
|
| 109 |
@app.route('/generate', methods=['POST'])
|
| 110 |
async def generate():
|
| 111 |
"""Handles the AI generation request."""
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
if __name__ == '__main__':
|
| 122 |
# Hugging Face Spaces typically expect the app to run on port 7860
|
|
|
|
| 15 |
|
| 16 |
Args:
|
| 17 |
user_query (str): The query provided by the user.
|
| 18 |
+
Returns:
|
| 19 |
+
str: The generated solution text or an error message.
|
| 20 |
"""
|
| 21 |
if not user_query:
|
| 22 |
+
return "Error: Query is required."
|
| 23 |
|
| 24 |
print(f"Processing query: {user_query}")
|
| 25 |
response_text = ""
|
|
|
|
| 34 |
|
| 35 |
# In a real scenario, you'd make an API call like this (example with a hypothetical search API):
|
| 36 |
# search_api_key = os.environ.get("YOUR_SEARCH_API_KEY")
|
| 37 |
+
# if not search_api_key:
|
| 38 |
+
# raise ValueError("YOUR_SEARCH_API_KEY environment variable not set.")
|
| 39 |
# search_api_url = "https://api.example.com/search"
|
| 40 |
# search_response = requests.get(search_api_url, params={"q": user_query, "api_key": search_api_key})
|
| 41 |
# search_response.raise_for_status()
|
|
|
|
| 89 |
response_text = "No solution could be generated. Please try a different query."
|
| 90 |
|
| 91 |
except requests.exceptions.RequestException as e:
|
| 92 |
+
error_message = f"Network or API error during LLM call: {e}"
|
| 93 |
print(f"Error: {error_message}")
|
| 94 |
+
response_text = f"An API error occurred: {error_message}. Please check the logs."
|
| 95 |
except ValueError as e:
|
| 96 |
+
error_message = f"Configuration error (e.g., missing API key): {e}"
|
| 97 |
print(f"Error: {error_message}")
|
| 98 |
+
response_text = f"A configuration error occurred: {error_message}. Please check your Space secrets."
|
| 99 |
except Exception as e:
|
| 100 |
+
error_message = f"An unexpected error occurred in generate_solution_python: {e}"
|
| 101 |
print(f"Error: {error_message}")
|
| 102 |
+
response_text = f"An unexpected error occurred: {error_message}. Please check the logs."
|
| 103 |
|
| 104 |
return response_text
|
| 105 |
|
|
|
|
| 113 |
@app.route('/generate', methods=['POST'])
|
| 114 |
async def generate():
|
| 115 |
"""Handles the AI generation request."""
|
| 116 |
+
try:
|
| 117 |
+
# Try to parse JSON from the request body
|
| 118 |
+
data = request.get_json()
|
| 119 |
+
if not data:
|
| 120 |
+
return jsonify({"error": "Request body must be JSON"}), 400
|
| 121 |
+
|
| 122 |
+
user_query = data.get('query')
|
| 123 |
+
if not user_query:
|
| 124 |
+
return jsonify({"error": "Query is required in the request body"}), 400
|
| 125 |
|
| 126 |
+
# Run the async function
|
| 127 |
+
solution = await generate_solution_python(user_query)
|
| 128 |
+
return jsonify({"solution": solution})
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
# Catch any unexpected errors during request processing or function call
|
| 132 |
+
print(f"Error in /generate endpoint: {e}")
|
| 133 |
+
return jsonify({"error": f"Internal server error: {e}"}), 500
|
| 134 |
|
| 135 |
if __name__ == '__main__':
|
| 136 |
# Hugging Face Spaces typically expect the app to run on port 7860
|