Spaces:
Running
Running
feat: add json extraction to access to even cheaper models (google/gemma)
Browse files- app/taskAI.py +5 -2
- app/utilAI.py +21 -0
app/taskAI.py
CHANGED
|
@@ -6,6 +6,7 @@ from llama_index.core import ChatPromptTemplate
|
|
| 6 |
|
| 7 |
from util import mylogger
|
| 8 |
from util import checkAPI
|
|
|
|
| 9 |
|
| 10 |
from icecream import ic
|
| 11 |
|
|
@@ -138,8 +139,10 @@ class TaskAI(OpenAILike):
|
|
| 138 |
).message.content
|
| 139 |
# yield meta_JD+'\n'+meta_CV
|
| 140 |
try:
|
| 141 |
-
meta_JD = json.loads(meta_JD.strip())
|
| 142 |
-
meta_CV = json.loads(meta_CV.strip())
|
|
|
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
ic(e)
|
| 145 |
raise ValueError(
|
|
|
|
| 6 |
|
| 7 |
from util import mylogger
|
| 8 |
from util import checkAPI
|
| 9 |
+
from utilAI import extract_json_from_text
|
| 10 |
|
| 11 |
from icecream import ic
|
| 12 |
|
|
|
|
| 139 |
).message.content
|
| 140 |
# yield meta_JD+'\n'+meta_CV
|
| 141 |
try:
|
| 142 |
+
# meta_JD = json.loads(meta_JD.strip())
|
| 143 |
+
# meta_CV = json.loads(meta_CV.strip())
|
| 144 |
+
meta_JD = extract_json_from_text(meta_JD.strip())
|
| 145 |
+
meta_CV = extract_json_from_text(meta_CV.strip())
|
| 146 |
except Exception as e:
|
| 147 |
ic(e)
|
| 148 |
raise ValueError(
|
app/utilAI.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
def extract_json_from_text(text):
|
| 4 |
+
try:
|
| 5 |
+
# Find the indices of the first and last curly braces
|
| 6 |
+
start_index = text.index('{')
|
| 7 |
+
end_index = text.rindex('}') + 1 # +1 to include the closing brace
|
| 8 |
+
|
| 9 |
+
# Extract the potential JSON string
|
| 10 |
+
json_string = text[start_index:end_index]
|
| 11 |
+
|
| 12 |
+
# Attempt to parse the extracted string as JSON
|
| 13 |
+
json_object = json.loads(json_string)
|
| 14 |
+
|
| 15 |
+
return json_object
|
| 16 |
+
except ValueError as e:
|
| 17 |
+
print(f"Error: Unable to extract valid JSON. {str(e)}")
|
| 18 |
+
return None
|
| 19 |
+
except json.JSONDecodeError as e:
|
| 20 |
+
print(f"Error: Invalid JSON format. {str(e)}")
|
| 21 |
+
return None
|