Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import json | |
| import PyPDF2 | |
| import docx | |
| # ---------------------------- | |
| # Resume Text Extraction | |
| # ---------------------------- | |
| def extract_text(file): | |
| if file is None: | |
| return "" | |
| filename = file.name | |
| text = "" | |
| if filename.endswith(".pdf"): | |
| reader = PyPDF2.PdfReader(file) | |
| for page in reader.pages: | |
| text += page.extract_text() or "" | |
| elif filename.endswith(".docx"): | |
| document = docx.Document(file) | |
| for para in document.paragraphs: | |
| text += para.text + "\n" | |
| elif filename.endswith(".txt"): | |
| text = file.read().decode("utf-8") | |
| return text | |
| # ---------------------------- | |
| # Resume Analyzer | |
| # ---------------------------- | |
| def analyze_resume(resume_text): | |
| if not resume_text.strip(): | |
| return { | |
| "score": 0, | |
| "technical_skills": [], | |
| "soft_skills": [], | |
| "recommendation": "No text detected in resume" | |
| } | |
| text = resume_text.lower() | |
| tech_keywords = [ | |
| "python","java","c++","sql","machine learning", | |
| "data analysis","tensorflow","pandas","numpy", | |
| "git","linux","ai" | |
| ] | |
| soft_keywords = [ | |
| "communication","teamwork","leadership", | |
| "problem solving","adaptability" | |
| ] | |
| tech_found = [k for k in tech_keywords if k in text] | |
| soft_found = [k for k in soft_keywords if k in text] | |
| score = min(100, len(tech_found)*8 + len(soft_found)*5) | |
| recommendation = ( | |
| "Add more technical skills and measurable achievements." | |
| if score < 50 else | |
| "Good resume. Minor improvements recommended." | |
| ) | |
| return { | |
| "score": score, | |
| "technical_skills": tech_found, | |
| "soft_skills": soft_found, | |
| "recommendation": recommendation | |
| } | |
| # ---------------------------- | |
| # Format analysis for UI | |
| # ---------------------------- | |
| def format_analysis(result): | |
| return f""" | |
| ## Resume Score: {result['score']}/100 | |
| ### Technical Skills Found | |
| {', '.join(result['technical_skills']) if result['technical_skills'] else "None"} | |
| ### Soft Skills Found | |
| {', '.join(result['soft_skills']) if result['soft_skills'] else "None"} | |
| ### Recommendation | |
| {result['recommendation']} | |
| """ | |
| # ---------------------------- | |
| # Export Functions | |
| # ---------------------------- | |
| def export_json(data): | |
| file_path = "analysis.json" | |
| with open(file_path,"w") as f: | |
| json.dump(data,f,indent=4) | |
| return file_path | |
| def export_text(data): | |
| file_path = "analysis.txt" | |
| with open(file_path,"w") as f: | |
| f.write(str(data)) | |
| return file_path | |
| # ---------------------------- | |
| # Processing Pipeline | |
| # ---------------------------- | |
| def process_resume(file): | |
| text = extract_text(file) | |
| analysis = analyze_resume(text) | |
| formatted = format_analysis(analysis) | |
| return text, formatted, analysis | |
| # ---------------------------- | |
| # UI | |
| # ---------------------------- | |
| with gr.Blocks(title="Resume Analyzer") as demo: | |
| gr.Markdown("# AI Resume Analyzer") | |
| gr.Markdown("Upload your resume and get instant feedback.") | |
| resume_file = gr.File(label="Upload Resume (PDF / DOCX / TXT)") | |
| analyze_btn = gr.Button("Analyze Resume") | |
| resume_text = gr.Textbox( | |
| label="Extracted Resume Text", | |
| lines=10 | |
| ) | |
| analysis_output = gr.Markdown(label="Analysis Result") | |
| analysis_state = gr.State() | |
| with gr.Row(): | |
| export_json_btn = gr.Button("Export JSON") | |
| export_text_btn = gr.Button("Export Text") | |
| download_file = gr.File(label="Download Analysis") | |
| # ---------------------------- | |
| # Button Actions | |
| # ---------------------------- | |
| analyze_btn.click( | |
| process_resume, | |
| inputs=resume_file, | |
| outputs=[resume_text, analysis_output, analysis_state] | |
| ) | |
| export_json_btn.click( | |
| export_json, | |
| inputs=analysis_state, | |
| outputs=download_file | |
| ) | |
| export_text_btn.click( | |
| export_text, | |
| inputs=analysis_state, | |
| outputs=download_file | |
| ) | |
| # ---------------------------- | |
| # Launch | |
| # ---------------------------- | |
| if __name__ == "__main__": | |
| demo.launch() |