Anwar11234 commited on
Commit
d823dd2
1 Parent(s): 1a06b7c

first commit

Browse files
Files changed (4) hide show
  1. Dockerfile +14 -0
  2. preprocessed_data.pkl +3 -0
  3. recommender.py +35 -0
  4. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.10.9 image
2
+ FROM python:3.10.9
3
+
4
+ # Copy the current directory contents into the container at .
5
+ COPY . .
6
+
7
+ # Set the working directory to /
8
+ WORKDIR /
9
+
10
+ # Install requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r /requirements.txt
12
+
13
+ # Start the FastAPI app on port 7860, the default port expected by Spaces
14
+ CMD ["uvicorn", "recommender:app", "--host", "0.0.0.0", "--port", "7860"]
preprocessed_data.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b741a71fb861485395ecea86b14ff7b5e190bed8da5b06fc9feb841bcf3ef355
3
+ size 67792
recommender.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Body
2
+ import pickle
3
+
4
+ with open('preprocessed_data.pkl', 'rb') as f:
5
+ tfidf_matrix, cosine_sim_tfidf, df, indices = pickle.load(f)
6
+
7
+ app = FastAPI()
8
+
9
+ @app.post("/recommendations")
10
+ def recommend(course_data: dict = Body(...)):
11
+ idx = indices.get(course_data["title"])
12
+
13
+ # Handle cases where the course title is not found
14
+ if idx is None:
15
+ return {"message": "Course not found."}
16
+
17
+ # Get the pairwise similarity scores of all courses with that course
18
+ sim_scores = list(enumerate(cosine_sim_tfidf[idx]))
19
+
20
+ # Sort the courses based on the similarity scores
21
+ sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
22
+
23
+ # Get the scores of the 10 most similar courses
24
+ sim_scores = sim_scores[1:11]
25
+
26
+ # Get the course indices
27
+ course_indices = [i[0] for i in sim_scores]
28
+
29
+ recommendations = df.iloc[course_indices][['CourseID', 'Title']].to_dict(orient='records')
30
+
31
+ return recommendations
32
+
33
+ if __name__ == "__main__":
34
+ import uvicorn
35
+ uvicorn.run("recommender:app", host="0.0.0.0", port=3000)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ scipy
4
+ pandas