Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -11,6 +11,7 @@ from sqlalchemy import create_engine, Column, Integer, String, Boolean, ForeignK
|
|
| 11 |
from sqlalchemy.ext.declarative import declarative_base
|
| 12 |
from sqlalchemy import LargeBinary
|
| 13 |
from sqlalchemy.orm import sessionmaker, relationship
|
|
|
|
| 14 |
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
|
|
@@ -222,25 +223,35 @@ def get_streams():
|
|
| 222 |
)
|
| 223 |
|
| 224 |
prompt = """
|
| 225 |
-
You
|
| 226 |
-
List
|
| 227 |
-
|
| 228 |
-
["
|
| 229 |
-
|
| 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 |
try:
|
| 237 |
-
# Ensure
|
| 238 |
cleaned_data = stream.strip()
|
| 239 |
-
if not cleaned_data.startswith("["):
|
| 240 |
-
raise ValueError("Invalid response format")
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
parsed_data = json.loads(cleaned_data)
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
except Exception as e:
|
| 245 |
return jsonify({"error": "Invalid response format", "details": str(e)})
|
| 246 |
|
|
|
|
| 11 |
from sqlalchemy.ext.declarative import declarative_base
|
| 12 |
from sqlalchemy import LargeBinary
|
| 13 |
from sqlalchemy.orm import sessionmaker, relationship
|
| 14 |
+
import json
|
| 15 |
|
| 16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
|
|
|
|
| 223 |
)
|
| 224 |
|
| 225 |
prompt = """
|
| 226 |
+
You are a recommendation engine.
|
| 227 |
+
List at least 40 branches of study (e.g., Computer Science, Chemical Engineering, Aerospace).
|
| 228 |
+
**Output should be a valid JSON array with double quotes, like this:**
|
| 229 |
+
["Computer Science", "Chemical Engineering", "Aerospace", ...]
|
| 230 |
+
Do not add extra text, explanations, or newlines.
|
| 231 |
"""
|
| 232 |
+
|
| 233 |
formatted_prompt = format_prompt(prompt)
|
| 234 |
|
| 235 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
|
| 236 |
|
| 237 |
try:
|
| 238 |
+
# Ensure the model's response is a valid JSON array
|
| 239 |
cleaned_data = stream.strip()
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
# Fix incomplete or malformed JSON
|
| 242 |
+
if not cleaned_data.startswith("[") or not cleaned_data.endswith("]"):
|
| 243 |
+
cleaned_data = cleaned_data.split("[", 1)[-1] # Keep text after first [
|
| 244 |
+
cleaned_data = "[" + cleaned_data # Add missing opening bracket
|
| 245 |
+
cleaned_data = cleaned_data.rsplit("]", 1)[0] # Keep text before last ]
|
| 246 |
+
cleaned_data = cleaned_data + "]" # Add missing closing bracket
|
| 247 |
+
|
| 248 |
+
# Parse JSON safely
|
| 249 |
parsed_data = json.loads(cleaned_data)
|
| 250 |
+
|
| 251 |
+
if not isinstance(parsed_data, list): # Ensure it's a list
|
| 252 |
+
raise ValueError("Response is not a valid list")
|
| 253 |
+
|
| 254 |
+
return jsonify({"ans": parsed_data}) # Return clean JSON list
|
| 255 |
except Exception as e:
|
| 256 |
return jsonify({"error": "Invalid response format", "details": str(e)})
|
| 257 |
|