Abdul-Haseeb commited on
Commit
ff4ed29
·
verified ·
1 Parent(s): 633fde0

Update pipelines.py

Browse files
Files changed (1) hide show
  1. pipelines.py +25 -1
pipelines.py CHANGED
@@ -76,6 +76,30 @@ def generate_quiz_pipeline():
76
 
77
  return quiz_generation_pipeline
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  def generate_quiz(url):
80
  pipeline = generate_quiz_pipeline()
81
  try:
@@ -83,7 +107,7 @@ def generate_quiz(url):
83
  if "generator" in results and "replies" in results["generator"]:
84
  #raw_reply = results["generator"]["replies"][0]
85
  reply = results["generator"]["replies"][0]
86
- raw_reply = reply.content if hasattr(reply, "content") else str(reply)
87
 
88
  # Extract the JSON part of the reply
89
  json_start = raw_reply.find("{")
 
76
 
77
  return quiz_generation_pipeline
78
 
79
+ import re
80
+ import json
81
+
82
+ def clean_llm_json(s: str) -> str:
83
+ s = s.strip()
84
+
85
+ # Strip ```json ... ``` fences if present
86
+ if s.startswith("```"):
87
+ # remove leading ```... line
88
+ s = re.sub(r"^```[a-zA-Z0-9]*\s*", "", s)
89
+ # remove trailing ```
90
+ s = re.sub(r"\s*```$", "", s)
91
+
92
+ # If it looks like a Python dict with single quotes only, convert to double quotes
93
+ if s.startswith("{") and "'" in s and '"' not in s:
94
+ s = s.replace("'", '"')
95
+
96
+ # Remove trailing commas before } or ]
97
+ s = re.sub(r",\s*}", "}", s)
98
+ s = re.sub(r",\s*]", "]", s)
99
+
100
+ return s
101
+
102
+
103
  def generate_quiz(url):
104
  pipeline = generate_quiz_pipeline()
105
  try:
 
107
  if "generator" in results and "replies" in results["generator"]:
108
  #raw_reply = results["generator"]["replies"][0]
109
  reply = results["generator"]["replies"][0]
110
+ raw_reply = getattr(reply, "text", str(reply)) # ChatMessage.text is the canonical field
111
 
112
  # Extract the JSON part of the reply
113
  json_start = raw_reply.find("{")