Spaces:
Sleeping
Sleeping
File size: 3,062 Bytes
ca06dd0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
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)
|