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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
from openpyxl import load_workbook
|
| 6 |
+
from IPython.display import Markdown
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Initialize empty DataFrame and TF-IDF matrix
|
| 10 |
+
data = pd.DataFrame()
|
| 11 |
+
tfidf_vectorizer = None
|
| 12 |
+
tfidf_matrix = None
|
| 13 |
+
|
| 14 |
+
# Helper: Load Data from File (Excel or CSV)
|
| 15 |
+
def load_file(file):
|
| 16 |
+
global data, tfidf_vectorizer, tfidf_matrix
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# Read file based on its extension
|
| 20 |
+
if file.endswith(".xlsx"):
|
| 21 |
+
# Read Excel file
|
| 22 |
+
workbook = load_workbook(filename=file, data_only=True)
|
| 23 |
+
sheet = workbook.active
|
| 24 |
+
headers = [cell.value if cell.value is not None else f"Unnamed Column {i}" for i, cell in enumerate(sheet[1])]
|
| 25 |
+
rows = sheet.iter_rows(min_row=2, values_only=True)
|
| 26 |
+
data = pd.DataFrame(rows, columns=headers)
|
| 27 |
+
|
| 28 |
+
# Extract hyperlinks from "Information" column if present
|
| 29 |
+
if "Information" in data.columns:
|
| 30 |
+
for i, row in enumerate(sheet.iter_rows(min_row=2)):
|
| 31 |
+
cell = row[data.columns.get_loc("Information")]
|
| 32 |
+
if cell.hyperlink:
|
| 33 |
+
data.at[i, "Information"] = cell.hyperlink.target
|
| 34 |
+
elif file.endswith(".csv"):
|
| 35 |
+
# Read CSV file
|
| 36 |
+
data = pd.read_csv(file)
|
| 37 |
+
else:
|
| 38 |
+
return "Unsupported file format. Please upload a .xlsx or .csv file."
|
| 39 |
+
|
| 40 |
+
# Initialize TF-IDF for employee search
|
| 41 |
+
tfidf_vectorizer = TfidfVectorizer(analyzer="char_wb", ngram_range=(2, 4))
|
| 42 |
+
tfidf_matrix = tfidf_vectorizer.fit_transform(data["Employee Name"].astype(str))
|
| 43 |
+
|
| 44 |
+
return "File loaded successfully!"
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error loading file: {e}"
|
| 47 |
+
|
| 48 |
+
# Helper: Generate Report Dynamically
|
| 49 |
+
import os
|
| 50 |
+
|
| 51 |
+
def generate_dynamic_report(query):
|
| 52 |
+
if data.empty or tfidf_vectorizer is None:
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
query_vec = tfidf_vectorizer.transform([query])
|
| 56 |
+
similarities = cosine_similarity(query_vec, tfidf_matrix).flatten()
|
| 57 |
+
best_match_idx = similarities.argmax()
|
| 58 |
+
|
| 59 |
+
if similarities[best_match_idx] > 0:
|
| 60 |
+
employee = data.iloc[best_match_idx]
|
| 61 |
+
employee_name = employee["Employee Name"]
|
| 62 |
+
report_file = f"{employee_name.replace(' ', '_')}_report.html"
|
| 63 |
+
|
| 64 |
+
# Generate HTML content dynamically
|
| 65 |
+
with open(report_file, "w") as file:
|
| 66 |
+
file.write(f"<html><head><title>{employee_name} Report</title></head><body>")
|
| 67 |
+
file.write(f"<h1>Employee Report for {employee_name}</h1>")
|
| 68 |
+
file.write("<hr>")
|
| 69 |
+
|
| 70 |
+
# Employee Details
|
| 71 |
+
file.write("<h2>Employee Details:</h2><ul>")
|
| 72 |
+
for column, value in employee.items():
|
| 73 |
+
if column == "Information" and isinstance(value, str):
|
| 74 |
+
if value.startswith("http") or value.startswith("file://"):
|
| 75 |
+
# Detect file type from hyperlink
|
| 76 |
+
file_type = os.path.splitext(value)[-1].lower()
|
| 77 |
+
if file_type == ".pdf":
|
| 78 |
+
label = "View PDF Document"
|
| 79 |
+
elif file_type in [".ppt", ".pptx"]:
|
| 80 |
+
label = "View PowerPoint Presentation"
|
| 81 |
+
elif file_type in [".doc", ".docx"]:
|
| 82 |
+
label = "View Word Document"
|
| 83 |
+
else:
|
| 84 |
+
label = "Download/View File"
|
| 85 |
+
value = f'<a href="{value}" target="_blank" style="color:blue;text-decoration:underline;">{label}</a>'
|
| 86 |
+
else:
|
| 87 |
+
# Add a clickable link for local paths
|
| 88 |
+
value = f'<a href="file:///{value}" target="_blank" style="color:blue;text-decoration:underline;">Download/View File</a>'
|
| 89 |
+
file.write(f"<li><strong>{column}:</strong> {value}</li>")
|
| 90 |
+
file.write("</ul>")
|
| 91 |
+
|
| 92 |
+
# Customer Insights
|
| 93 |
+
file.write("<h2>Customer Insights:</h2><ul>")
|
| 94 |
+
if "Experience" in data.columns:
|
| 95 |
+
experience = employee.get("Experience", "No experience details available.")
|
| 96 |
+
file.write(f"<li>Experience in the field: {experience}</li>")
|
| 97 |
+
if "Skills" in data.columns:
|
| 98 |
+
skills = employee.get("Skills", "No skills information available.")
|
| 99 |
+
file.write(f"<li>Key skills: {skills}</li>")
|
| 100 |
+
if "Projects" in data.columns:
|
| 101 |
+
projects = employee.get("Projects", "No projects listed.")
|
| 102 |
+
file.write(f"<li>Notable projects: {projects}</li>")
|
| 103 |
+
file.write("</ul>")
|
| 104 |
+
|
| 105 |
+
# Summary
|
| 106 |
+
file.write("<h2>Summary:</h2>")
|
| 107 |
+
file.write(f"<p>{employee_name} has shown notable contributions in the domain. Refer to the linked documents for more details.</p>")
|
| 108 |
+
file.write("<p>Thank you for using our Employee Dashboard!</p>")
|
| 109 |
+
file.write("</body></html>")
|
| 110 |
+
|
| 111 |
+
return report_file
|
| 112 |
+
else:
|
| 113 |
+
return None
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# Helper: Search Employee
|
| 118 |
+
def search_employee(query):
|
| 119 |
+
if data.empty or tfidf_vectorizer is None:
|
| 120 |
+
return pd.DataFrame([{"Error": "No data available. Please upload a file first."}])
|
| 121 |
+
|
| 122 |
+
query_vec = tfidf_vectorizer.transform([query])
|
| 123 |
+
similarities = cosine_similarity(query_vec, tfidf_matrix).flatten()
|
| 124 |
+
best_match_idx = similarities.argmax()
|
| 125 |
+
|
| 126 |
+
if similarities[best_match_idx] > 0:
|
| 127 |
+
# Ensure output is a valid DataFrame with one row
|
| 128 |
+
employee = data.iloc[best_match_idx].to_frame().T # Convert Series to DataFrame
|
| 129 |
+
return employee
|
| 130 |
+
else:
|
| 131 |
+
return pd.DataFrame([{"Error": "No matching employee found."}])
|
| 132 |
+
|
| 133 |
+
# Gradio Interface
|
| 134 |
+
with gr.Blocks() as interface:
|
| 135 |
+
gr.Markdown("""
|
| 136 |
+
<h1 style="text-align: center;">Employee Dashboard</h1>
|
| 137 |
+
<p style="text-align: center;">Upload your Excel or CSV file to get started. Search employees, view metrics, and generate dynamic reports.</p>
|
| 138 |
+
""")
|
| 139 |
+
|
| 140 |
+
with gr.Row():
|
| 141 |
+
file_upload = gr.File(label="Upload Excel or CSV File", type="filepath")
|
| 142 |
+
upload_status = gr.Textbox(label="Upload Status", interactive=False)
|
| 143 |
+
upload_button = gr.Button("Upload")
|
| 144 |
+
upload_button.click(load_file, inputs=[file_upload], outputs=[upload_status])
|
| 145 |
+
|
| 146 |
+
with gr.Row():
|
| 147 |
+
search_query = gr.Textbox(label="Search Employee", placeholder="Type partial name (e.g., 'Aar')")
|
| 148 |
+
employee_details = gr.Dataframe(label="Employee Details", interactive=True)
|
| 149 |
+
report_output = gr.File(label="Download Report")
|
| 150 |
+
|
| 151 |
+
search_button = gr.Button("Search")
|
| 152 |
+
search_button.click(
|
| 153 |
+
search_employee,
|
| 154 |
+
inputs=[search_query],
|
| 155 |
+
outputs=[employee_details],
|
| 156 |
+
)
|
| 157 |
+
search_button.click(
|
| 158 |
+
generate_dynamic_report,
|
| 159 |
+
inputs=[search_query],
|
| 160 |
+
outputs=[report_output],
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
+
interface.launch(share=True)
|