Spaces:
Sleeping
Sleeping
| """ | |
| Converts raw JSON data from each knowledge-base file type into a | |
| human-readable plain-text block that gets fed to the vector store. | |
| """ | |
| import json | |
| # ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _join(items: list, key: str | None = None) -> str: | |
| if key: | |
| return ", ".join(str(i.get(key, "")) for i in items) | |
| return ", ".join(str(i) for i in items) | |
| def _bullet_list(items: list) -> str: | |
| return "\n".join(f"- {item}" for item in items) | |
| # ββ Formatters βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def fmt_personal(d: dict) -> str: | |
| langs = ", ".join(f"{l['language']} ({l['level']})" for l in d.get("languages", [])) | |
| projects = d.get("projects", []) | |
| projects_text = "" | |
| for p in projects: | |
| link = p.get("demoLink") or p.get("githubLink") or "N/A" | |
| cats = ", ".join(p.get("category", [])) | |
| featured = "yes" if p.get("isFeatured") else "no" | |
| projects_text += f"\n- {p.get('title')} | Category: {cats} | Featured: {featured} | Link: {link}" | |
| return f"""PERSONAL INFORMATION | |
| Name: {d.get('name')} | Full name: {d.get('full_name')} | |
| Title: {d.get('title')} | |
| Summary: {d.get('summary')} | |
| Email: {d.get('email')} | |
| Phone: {d.get('phone')} | |
| Location: {d.get('location')} | |
| Website: {d.get('website')} | |
| LinkedIn: {d.get('linkedin')} | |
| GitHub: {d.get('github')} | |
| Instagram: {d.get('instagram')} | |
| YouTube: {d.get('youtube')} | |
| Languages: {langs} | |
| Open to: {d.get('open_to')} | |
| COMPLETE PROJECT LIST (use this for all project listing questions): | |
| {projects_text}""" | |
| def fmt_education(d: dict) -> str: | |
| lines = ["EDUCATION"] | |
| for edu in d.get("education", []): | |
| lines.append(f""" | |
| Degree: {edu.get('degree')} | |
| Institution: {edu.get('institution')} | Location: {edu.get('location')} | |
| Period: {edu.get('period')} | |
| GPA: {edu.get('gpa')} | |
| Achievement: {edu.get('achievement')} | |
| Link: {edu.get('link', 'N/A')}""") | |
| return "\n".join(lines) | |
| def fmt_skills(d: dict) -> str: | |
| s = d.get("skills", {}) | |
| return f"""SKILLS | |
| Programming Languages: {_join(s.get('languages', []))} | |
| Frameworks: {_join(s.get('frameworks', []))} | |
| AI & ML: {_join(s.get('ai_ml', []))} | |
| Databases: {_join(s.get('databases', []))} | |
| Tools: {_join(s.get('tools', []))}""" | |
| def fmt_experience(d: dict) -> str: | |
| lines = ["WORK EXPERIENCE"] | |
| for exp in d.get("experience", []): | |
| links = ", ".join(exp.get("links", [])) or "N/A" | |
| lines.append(f""" | |
| Title: {exp.get('title')} | Type: {exp.get('type')} | |
| Company: {exp.get('company', 'Freelance')} | |
| Period: {exp.get('period')} | |
| Technologies: {_join(exp.get('technologies', []))} | |
| Links: {links} | |
| Highlights: | |
| {_bullet_list(exp.get('highlights', []))}""") | |
| lines.append("\nINTERNSHIPS") | |
| for intern in d.get("internships", []): | |
| lines.append(f""" | |
| Title: {intern.get('title')} | Company: {intern.get('company')} | |
| Location: {intern.get('location', '')} | |
| Period: {intern.get('period')} | |
| Technologies: {_join(intern.get('technologies', []))} | |
| Highlights: | |
| {_bullet_list(intern.get('highlights', []))}""") | |
| return "\n".join(lines) | |
| def fmt_volunteering(d: dict) -> str: | |
| lines = ["VOLUNTEERING"] | |
| for v in d.get("volunteering", []): | |
| lines.append(f""" | |
| Title: {v.get('title')} | |
| Organization: {v.get('organization')} | |
| Period: {v.get('period')} | |
| Description: {v.get('description')} | |
| Document: {v.get('document_image', 'N/A')}""") | |
| return "\n".join(lines) | |
| def fmt_certificates(d: dict) -> str: | |
| lines = ["CERTIFICATES"] | |
| for c in d.get("certificates", []): | |
| lines.append( | |
| f"- {c.get('title')} | {c.get('issuer')} | {c.get('year')} | Category: {c.get('category')}" | |
| ) | |
| return "\n".join(lines) | |
| def fmt_references(d: dict) -> str: | |
| lines = ["REFERENCES"] | |
| for r in d.get("references", []): | |
| lines.append(f""" | |
| Name: {r.get('name')} | |
| Title: {r.get('title')} | |
| Institution: {r.get('institution')} | |
| Email: {r.get('email')} | |
| Profile: {r.get('profile_url', 'N/A')}""") | |
| return "\n".join(lines) | |
| def _content_blocks(data: dict) -> str: | |
| return "".join( | |
| f"\n### {b.get('heading', '')}\n{b.get('content', '')}\n" | |
| for b in data.get("contentBlocks", []) | |
| if b.get("type") == 0 | |
| ) | |
| def fmt_project(d: dict) -> str: | |
| results = "\n".join( | |
| f"- {r.get('metric')}: {r.get('value')} β {r.get('description')}" | |
| for r in d.get("results", []) | |
| ) | |
| return f"""PROJECT: {d.get('title')} | |
| Subtitle: {d.get('subtitle')} | |
| Description: {d.get('description')} | |
| Category: {_join(d.get('category', []))} | |
| Tags: {_join(d.get('tags', []))} | |
| Technologies: {_join(d.get('technologies', []), key='name')} | |
| Date: {d.get('date')} | |
| Duration: {d.get('duration', 'N/A')} | |
| Team Size: {d.get('teamSize')} | |
| Role: {d.get('role')} | |
| Demo Link: {d.get('demoLink', 'N/A')} | |
| GitHub: {d.get('githubLink', 'N/A')} | |
| Featured: {d.get('isFeatured')} | |
| Long Description: | |
| {d.get('longDescription', '')} | |
| {_content_blocks(d)} | |
| Challenges: | |
| {_bullet_list(d.get('challenges', []))} | |
| Solutions: | |
| {_bullet_list(d.get('solutions', []))} | |
| Results: | |
| {results}""" | |
| def fmt_article(d: dict) -> str: | |
| return f"""ARTICLE: {d.get('title')} | |
| Subtitle: {d.get('subtitle', '')} | |
| Description: {d.get('description', '')} | |
| Category: {d.get('category', '')} | |
| Tags: {_join(d.get('tags', []))} | |
| Date: {d.get('date', '')} | |
| Link: {d.get('link', d.get('demoLink', 'N/A'))} | |
| {_content_blocks(d)}""" | |
| # ββ Dispatch βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| _FORMATTERS = { | |
| "personal": fmt_personal, | |
| "education": fmt_education, | |
| "skills": fmt_skills, | |
| "experience": fmt_experience, | |
| "volunteering": fmt_volunteering, | |
| "certificates": fmt_certificates, | |
| "references": fmt_references, | |
| "project": fmt_project, | |
| "article": fmt_article, | |
| } | |
| def json_to_text(data: dict, source_type: str) -> str: | |
| formatter = _FORMATTERS.get(source_type) | |
| if formatter: | |
| return formatter(data) | |
| return json.dumps(data, ensure_ascii=False, indent=2) | |