saherPervaiz commited on
Commit
e92236a
Β·
verified Β·
1 Parent(s): d2ef00f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -72
app.py CHANGED
@@ -1,13 +1,9 @@
1
- # app.py
2
-
3
- import os
4
  import gradio as gr
5
  from text_extractor import extract_text_from_file
6
  from embedder import get_embeddings
7
  from vector_store import create_faiss_index, search_similar_cvs
8
  from groq_api import summarize_match
9
 
10
- # Global state
11
  cv_texts = []
12
  cv_names = []
13
  cv_vectors = []
@@ -15,86 +11,63 @@ faiss_index = None
15
 
16
  def upload_cvs(files):
17
  global cv_texts, cv_names, cv_vectors, faiss_index
 
18
  try:
19
- cv_texts = [extract_text_from_file(f.name) for f in files]
20
  cv_names = [f.name for f in files]
21
  cv_vectors = get_embeddings(cv_texts)
22
 
23
- import numpy as np
24
- if cv_vectors is None or np.array(cv_vectors).size == 0:
25
- return "❌ No valid CVs extracted or embedded."
26
 
27
  faiss_index = create_faiss_index(cv_vectors)
28
  return f"βœ… Uploaded and indexed {len(files)} CVs."
 
29
  except Exception as e:
30
  return f"❌ Error during upload: {e}"
31
 
32
  def match_jd(jd_text):
33
- global faiss_index
34
- try:
35
- if not faiss_index:
36
- return "❌ Please upload CVs first."
37
- if not jd_text.strip():
38
- return "❌ Job description is empty."
39
-
40
- jd_vector = get_embeddings([jd_text])[0]
41
- top_k_indices = search_similar_cvs(jd_vector, faiss_index, k=3)
42
-
43
- import os
44
- matched_names = [os.path.basename(cv_names[i]) for i in top_k_indices]
45
- matched_texts = [
46
- cv_texts[i][:500] if cv_texts[i].strip() else "[No CV content]"
47
- for i in top_k_indices
48
- ]
49
-
50
- summary = summarize_match(jd_text, matched_names, matched_texts)
51
- return f"""
52
- βœ… <span style='color:#16a34a; font-weight:bold;'>Top Matches:</span><br>{matched_names}<br><br>
53
- πŸ“ <span style='color:#3b82f6; font-weight:bold;'>Summary:</span><br>{summary}
54
- """
55
- except Exception as e:
56
- return f"<span style='color:red;'>❌ Error during matching: {e}</span>"
57
 
58
  def clear_data():
59
  global cv_texts, cv_names, cv_vectors, faiss_index
60
  cv_texts, cv_names, cv_vectors, faiss_index = [], [], [], None
61
- return "🧹 All data cleared. You can now start fresh."
62
-
63
- # ======================
64
- # TABBED PROFESSIONAL UI
65
- # ======================
66
- with gr.Blocks(css="""
67
- .gr-button { background-color: #2563eb; color: white; font-weight: bold; }
68
- .gr-button:hover { background-color: #1d4ed8; }
69
- textarea, input[type='file'] { border: 2px solid #3b82f6 !important; }
70
- .gr-textbox label { color: #111827; font-weight: 600; }
71
- """) as upload_tab:
72
- gr.Markdown("## πŸ“€ Upload CVs")
73
- gr.Markdown("Upload candidate CVs in PDF or DOCX format.")
74
- cv_upload = gr.File(label="Upload CVs", file_types=[".pdf", ".docx"], file_count="multiple")
75
- upload_button = gr.Button("πŸ“ Upload & Index CVs")
76
- upload_status = gr.Textbox(label="Status", interactive=False)
77
- upload_button.click(upload_cvs, inputs=[cv_upload], outputs=[upload_status])
78
-
79
- with gr.Blocks() as match_tab:
80
- gr.Markdown("## πŸ“‹ Match Job Description to CVs")
81
- jd_input = gr.Textbox(label="Paste Job Description", lines=8, placeholder="e.g. Looking for a Python Data Analyst...")
82
- match_button = gr.Button("πŸ” Match CVs")
83
- match_result = gr.HTML()
84
- match_button.click(match_jd, inputs=[jd_input], outputs=[match_result])
85
-
86
- with gr.Blocks() as reset_tab:
87
- gr.Markdown("## 🧹 Clear All Data")
88
- gr.Markdown("Click below to reset the app and upload new CVs.")
89
- clear_button = gr.Button("Reset App")
90
- clear_output = gr.Textbox(label="Reset Status", interactive=False)
91
- clear_button.click(clear_data, inputs=[], outputs=[clear_output])
92
-
93
- # Menu Bar Style Tabs
94
- app = gr.TabbedInterface(
95
- interface_list=[upload_tab, match_tab, reset_tab],
96
- tab_names=["πŸ“€ Upload CVs", "πŸ“‹ Match JD", "🧹 Reset"]
97
- )
98
-
99
- if __name__ == "__main__":
100
- app.launch()
 
 
 
 
1
  import gradio as gr
2
  from text_extractor import extract_text_from_file
3
  from embedder import get_embeddings
4
  from vector_store import create_faiss_index, search_similar_cvs
5
  from groq_api import summarize_match
6
 
 
7
  cv_texts = []
8
  cv_names = []
9
  cv_vectors = []
 
11
 
12
  def upload_cvs(files):
13
  global cv_texts, cv_names, cv_vectors, faiss_index
14
+
15
  try:
16
+ cv_texts = [extract_text_from_file(f) for f in files]
17
  cv_names = [f.name for f in files]
18
  cv_vectors = get_embeddings(cv_texts)
19
 
20
+ if not cv_vectors:
21
+ return "❌ No valid CVs."
 
22
 
23
  faiss_index = create_faiss_index(cv_vectors)
24
  return f"βœ… Uploaded and indexed {len(files)} CVs."
25
+
26
  except Exception as e:
27
  return f"❌ Error during upload: {e}"
28
 
29
  def match_jd(jd_text):
30
+ if not faiss_index:
31
+ return "❌ Please upload CVs first."
32
+ if not jd_text.strip():
33
+ return "⚠️ Job description is empty."
34
+
35
+ jd_vector = get_embeddings([jd_text])[0]
36
+ indices = search_similar_cvs(jd_vector, faiss_index, k=3)
37
+
38
+ matched = [cv_names[i] for i in indices]
39
+ texts = [cv_texts[i][:400] for i in indices]
40
+
41
+ summary = summarize_match(jd_text, matched, texts)
42
+
43
+ return f"βœ… Top Matches:\n{matched}\n\nπŸ“ Summary:\n{summary}"
 
 
 
 
 
 
 
 
 
 
44
 
45
  def clear_data():
46
  global cv_texts, cv_names, cv_vectors, faiss_index
47
  cv_texts, cv_names, cv_vectors, faiss_index = [], [], [], None
48
+ return "🧹 Cleared."
49
+
50
+ with gr.Blocks(css=".gr-box {max-width: none !important;}") as app:
51
+ gr.Markdown("# πŸ“„ CV Matcher with Groq API")
52
+
53
+ with gr.Box():
54
+ gr.Markdown("## πŸ“€ Upload CVs")
55
+ file_input = gr.File(file_types=[".pdf", ".docx"], file_count="multiple", label="Select CVs")
56
+ upload_btn = gr.Button("πŸ“ Upload & Index")
57
+ upload_output = gr.Textbox(label="Status")
58
+
59
+ with gr.Box():
60
+ gr.Markdown("## πŸ“‹ Match Job Description")
61
+ jd_input = gr.Textbox(label="Paste Job Description", lines=8, max_lines=12)
62
+ match_btn = gr.Button("πŸ” Match CVs")
63
+ result_output = gr.Textbox(label="Results", lines=12, interactive=False)
64
+
65
+ with gr.Row():
66
+ clear_btn = gr.Button("🧹 Clear All")
67
+ clear_output = gr.Textbox(label="Status")
68
+
69
+ upload_btn.click(upload_cvs, inputs=[file_input], outputs=[upload_output])
70
+ match_btn.click(match_jd, inputs=[jd_input], outputs=[result_output])
71
+ clear_btn.click(clear_data, inputs=[], outputs=[clear_output])
72
+
73
+ app.launch()