Swapnil-101 commited on
Commit
7c0f013
·
verified ·
1 Parent(s): db71d9f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +18 -11
main.py CHANGED
@@ -220,24 +220,31 @@ def get_streams():
220
  do_sample=True,
221
  seed=42,
222
  )
223
-
224
- prompt = """You need to act as a recommendation engine.
225
- List all 40+ streams/branches in computer science, chemical engineering, aerospace engineering, etc.
226
- Note: The output should be a valid JSON list in the format:
 
227
  ["Artificial Intelligence", "Computer Science", "Data Science", "Cyber Security", ...]
228
- Return only the list, no additional text, and ensure it is properly formatted as JSON."""
229
-
 
230
  formatted_prompt = format_prompt(prompt)
231
 
232
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
233
 
234
- # Ensure the response is correctly formatted as a JSON list
 
 
 
235
  try:
236
- stream_list = json.loads(stream) # Convert string to list
237
- except json.JSONDecodeError:
 
 
 
238
  return jsonify({"error": "Invalid response format"}), 500
239
-
240
- return jsonify({"ans": stream_list})
241
 
242
 
243
 
 
220
  do_sample=True,
221
  seed=42,
222
  )
223
+
224
+ # Updated prompt to enforce strict JSON output
225
+ prompt = """You are a recommendation engine.
226
+ List 40+ academic streams like "Artificial Intelligence", "Chemical Engineering", etc.
227
+ Output **must be** a valid JSON array **without extra text**:
228
  ["Artificial Intelligence", "Computer Science", "Data Science", "Cyber Security", ...]
229
+ No additional formatting, no extra text, just the JSON array.
230
+ """
231
+
232
  formatted_prompt = format_prompt(prompt)
233
 
234
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
235
 
236
+ # Debugging: Print raw API response
237
+ print("Raw API Response:", stream)
238
+
239
+ # Clean and validate response
240
  try:
241
+ stream_cleaned = stream.strip().replace("\n", "").replace("`", "").replace("json", "")
242
+ stream_list = json.loads(stream_cleaned) # Convert string to Python list
243
+ return jsonify({"ans": stream_list})
244
+ except json.JSONDecodeError as e:
245
+ print("JSON Decode Error:", e)
246
  return jsonify({"error": "Invalid response format"}), 500
247
+
 
248
 
249
 
250