hi5bro's picture
Update app.py
93f2465 verified
import pandas as pd
import gradio as gr
from sentence_transformers import SentenceTransformer, util
# Load dataset
df_internships = pd.read_csv("internships.csv")
# Load lightweight embedding model
model = SentenceTransformer('all-MiniLM-L6-v2')
def recommend_internships(education, skills, sector, location):
# 1. Candidate profile as a single string
candidate_text = f"Education: {education}. Skills: {', '.join(skills)}. Sector: {sector}. Location: {location}"
# 2. Compute embeddings
candidate_emb = model.encode(candidate_text, convert_to_tensor=True)
internship_texts = df_internships['Skills'] + " " + df_internships['Sector'] + " " + df_internships['Location']
internship_embs = model.encode(list(internship_texts), convert_to_tensor=True)
# 3. Compute cosine similarity
cosine_scores = util.cos_sim(candidate_emb, internship_embs)[0]
# 4. Add score to dataframe
df_internships['Match Score'] = cosine_scores.cpu().numpy()
# 5. Filter by sector/location preference (optional fallback)
filtered = df_internships[
(df_internships['Sector'] == sector) &
(df_internships['Location'] == location)
]
if len(filtered) < 3:
filtered = df_internships # fallback to all internships
# 6. Sort by match score and pick top 5
top = filtered.sort_values(by='Match Score', ascending=False).head(5)
# 7. Prepare card-like recommendations
recommendations = []
for _, row in top.iterrows():
card = (
f"**{row['Title']}**\n"
f"Location: {row['Location']}\n"
f"Sector: {row['Sector']}\n"
f"Skills Required: {row['Skills']}\n"
f"Match Score: {int(row['Match Score']*100)}%"
)
recommendations.append(card)
# 8. Save Excel for download
top[['Title', 'Skills', 'Sector', 'Location', 'Match Score']].to_excel("output.xlsx", index=False)
return recommendations, "output.xlsx"
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## 🌟 Skill-to-Opportunity AI Navigator for PM Internship Scheme")
gr.Markdown("Enter your details and get top 3–5 personalized internship recommendations.")
with gr.Row():
education_input = gr.Dropdown(["High School", "Undergraduate", "Graduate", "Other"], label="Education")
sector_input = gr.Dropdown(["Tech", "Governance", "NGOs"], label="Sector Interest")
location_input = gr.Dropdown(["Delhi", "Bangalore", "Mumbai", "Chennai", "Hyderabad"], label="Preferred Location")
skills_input = gr.CheckboxGroup(
["Python","Excel","SQL","Writing","Editing","Research","Communication","Creativity","Marketing","Organizing","Planning"],
label="Select Your Skills"
)
recommend_btn = gr.Button("Get Recommendations")
recommendations_output = gr.Textbox(label="Top Internship Recommendations", lines=10)
download_output = gr.File(label="Download Excel with Top Recommendations")
recommend_btn.click(
recommend_internships,
inputs=[education_input, skills_input, sector_input, location_input],
outputs=[recommendations_output, download_output]
)
if __name__ == "__main__":
demo.launch()