Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as stc
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity,linear_kernel
|
| 6 |
+
|
| 7 |
+
import re
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import numpy as np
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import neattext.functions as nfx
|
| 12 |
+
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
|
| 13 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 14 |
+
import warnings
|
| 15 |
+
st.set_page_config(layout="wide", initial_sidebar_state="expanded")
|
| 16 |
+
|
| 17 |
+
warnings.filterwarnings("ignore")
|
| 18 |
+
data= pd.read_csv("udemy_courses.csv")
|
| 19 |
+
data["titre_OK"]= data.course_title.apply(nfx.remove_stopwords)
|
| 20 |
+
data["titre_OK"]= data.course_title.apply(nfx.remove_special_characters)
|
| 21 |
+
countVec= CountVectorizer()
|
| 22 |
+
cv= countVec.fit_transform(data.titre_OK)
|
| 23 |
+
df=data
|
| 24 |
+
matrice_cosine= cosine_similarity(cv)
|
| 25 |
+
|
| 26 |
+
def recommend_course2(title, numrec=10):
|
| 27 |
+
try:
|
| 28 |
+
pattern = re.compile(re.escape(title), re.IGNORECASE)
|
| 29 |
+
matching_courses = df['course_title'].apply(lambda x: bool(pattern.search(x)))
|
| 30 |
+
index = df[matching_courses].index[0]
|
| 31 |
+
scores = list(enumerate(matrice_cosine[index]))
|
| 32 |
+
sorted_scores = sorted(scores, key=lambda x: x[1], reverse=True)
|
| 33 |
+
selected_course_index = [i[0] for i in sorted_scores[1:]]
|
| 34 |
+
selected_course_score = [i[1] for i in sorted_scores[1:]]
|
| 35 |
+
rec_df = df.iloc[selected_course_index]
|
| 36 |
+
rec_df['Similarity_Score'] = selected_course_score
|
| 37 |
+
|
| 38 |
+
final_recommended_courses = rec_df[["course_title","level", "subject","Similarity_Score"]]
|
| 39 |
+
except:
|
| 40 |
+
final_recommended_courses= pd.DataFrame({"data": "Aucune Recommendaion disponible!"},index=[0])
|
| 41 |
+
|
| 42 |
+
return final_recommended_courses.head(numrec)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def main():
|
| 47 |
+
|
| 48 |
+
st.title("Système de Recommandation de Cours")
|
| 49 |
+
|
| 50 |
+
menu = ["Accueil","Recommendations","A propos"]
|
| 51 |
+
choice = st.sidebar.selectbox("Menu",menu)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
if choice == "Accueil":
|
| 55 |
+
st.subheader("Accueil")
|
| 56 |
+
st.dataframe(df.head(10))
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
elif choice == "Recommendations":
|
| 60 |
+
st.subheader("Recommendations de formation")
|
| 61 |
+
search_term = st.text_input("cours")
|
| 62 |
+
num_of_rec = st.sidebar.number_input("Nombre de cours",4,30,7)
|
| 63 |
+
if st.button("Recommendations"):
|
| 64 |
+
if search_term:
|
| 65 |
+
st.write(recommend_course2(search_term, num_of_rec))
|
| 66 |
+
else:
|
| 67 |
+
st.subheader("A propos")
|
| 68 |
+
st.text("Keyce @2024")
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
main()
|
| 73 |
+
|
| 74 |
+
|