Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +13 -5
src/streamlit_app.py
CHANGED
|
@@ -15,21 +15,28 @@ JSON:"""
|
|
| 15 |
def extract(text: str):
|
| 16 |
out = gen(prompt.format(text=text))
|
| 17 |
raw = out[0].get("generated_text") or out[0].get("text") or str(out[0])
|
| 18 |
-
m = re.search(r"\{[\s\S]*\}", raw)
|
| 19 |
data = {}
|
| 20 |
if m:
|
| 21 |
-
blob = m.group(0)
|
| 22 |
for parser in (json.loads, ast.literal_eval):
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if not isinstance(data, dict):
|
| 27 |
return {
|
| 28 |
"SKILL": ["(Error: Invalid/Corrupted Model Output)"],
|
| 29 |
"KNOWLEDGE": [],
|
| 30 |
"DEBUG_RAW_OUTPUT": raw
|
| 31 |
}
|
| 32 |
-
|
| 33 |
return {
|
| 34 |
"SKILL": data.get("SKILL", []),
|
| 35 |
"KNOWLEDGE": data.get("KNOWLEDGE", [])
|
|
@@ -37,5 +44,6 @@ def extract(text: str):
|
|
| 37 |
|
| 38 |
st.title("Skill/Knowledge Extractor")
|
| 39 |
text = st.text_area("Paste text")
|
|
|
|
| 40 |
if st.button("Extract") and text.strip():
|
| 41 |
st.json(extract(text))
|
|
|
|
| 15 |
def extract(text: str):
|
| 16 |
out = gen(prompt.format(text=text))
|
| 17 |
raw = out[0].get("generated_text") or out[0].get("text") or str(out[0])
|
| 18 |
+
m = re.search(r"(\{[\s\S]*\})", raw)
|
| 19 |
data = {}
|
| 20 |
if m:
|
| 21 |
+
blob = m.group(0).strip()
|
| 22 |
for parser in (json.loads, ast.literal_eval):
|
| 23 |
try:
|
| 24 |
+
parsed_data = parser(blob)
|
| 25 |
+
if isinstance(parsed_data, list) and parsed_data:
|
| 26 |
+
data = parsed_data[0]
|
| 27 |
+
elif isinstance(parsed_data, dict):
|
| 28 |
+
data = parsed_data
|
| 29 |
+
break
|
| 30 |
+
except Exception:
|
| 31 |
+
continue
|
| 32 |
+
|
| 33 |
if not isinstance(data, dict):
|
| 34 |
return {
|
| 35 |
"SKILL": ["(Error: Invalid/Corrupted Model Output)"],
|
| 36 |
"KNOWLEDGE": [],
|
| 37 |
"DEBUG_RAW_OUTPUT": raw
|
| 38 |
}
|
| 39 |
+
|
| 40 |
return {
|
| 41 |
"SKILL": data.get("SKILL", []),
|
| 42 |
"KNOWLEDGE": data.get("KNOWLEDGE", [])
|
|
|
|
| 44 |
|
| 45 |
st.title("Skill/Knowledge Extractor")
|
| 46 |
text = st.text_area("Paste text")
|
| 47 |
+
|
| 48 |
if st.button("Extract") and text.strip():
|
| 49 |
st.json(extract(text))
|