Last commit not found
raw
history blame
3.06 kB
import gradio as gr
import pickle
import pandas as pd
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
# Load model and data
with open("course_emb.pkl", "rb") as f:
course_emb = pickle.load(f)
df = pd.read_excel("analytics_vidhya_courses_Final.xlsx")
model = SentenceTransformer('all-MiniLM-L6-v2')
def search_courses(query, top_n=5):
if not query.strip():
return "Please enter a search query."
query_embedding = model.encode([query])
similarities = cosine_similarity(query_embedding, course_emb)
top_n_idx = similarities[0].argsort()[-top_n:][::-1]
results = []
for idx in top_n_idx:
course = df.iloc[idx]
results.append({
"title": course["Course Title"],
"description": course["Course Description"],
"similarity": float(similarities[0][idx])
})
return results
def gradio_interface(query):
results = search_courses(query)
if isinstance(results, str):
return results
# Format results as HTML for better presentation
html_output = "<div style='font-family: Arial, sans-serif;'>"
for i, course in enumerate(results, 1):
relevance = int(course['similarity'] * 100)
html_output += f"""
<div style='background: white; padding: 15px; margin: 10px 0; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);'>
<h3 style='color: #2c3e50; margin: 0 0 10px 0;'>#{i}. {course['title']}</h3>
<div style='color: #7f8c8d; font-size: 0.9em; margin-bottom: 8px;'>Relevance: {relevance}%</div>
<p style='color: #34495e; margin: 0; line-height: 1.5;'>{course['description']}</p>
</div>
"""
html_output += "</div>"
return html_output
# Create Gradio interface with improved styling
css = """
.gradio-container {
font-family: 'Arial', sans-serif;
}
"""
with gr.Blocks(css=css) as iface:
gr.Markdown(
"""
# πŸŽ“ EduPath Explorer
Discover your ideal learning path! Simply describe what you want to learn, and let our AI find the perfect courses for you.
"""
)
with gr.Row():
query_input = gr.Textbox(
label="Describe Your Learning Goals",
placeholder="e.g., 'data visualization fundamentals' or 'deep learning with practical projects'",
scale=4
)
with gr.Row():
search_button = gr.Button("🎯 Find My Courses", variant="primary")
with gr.Row():
output = gr.HTML(label="Recommended Courses")
search_button.click(
fn=gradio_interface,
inputs=query_input,
outputs=output,
)
gr.Markdown(
"""
### πŸ’‘ Search Tips:
- Mention your current expertise level
- Include specific technologies or tools
- Describe your end goals
- Add preferred learning style (hands-on, theoretical, etc.)
"""
)
# Launch the interface
iface.launch(share=True)