RoTesla commited on
Commit
3ea381c
1 Parent(s): 679f224
Files changed (2) hide show
  1. main.py +144 -0
  2. requirements.txt +7 -0
main.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Import libraries
2
+ from fastapi import FastAPI, Request, Form
3
+ import pandas as pd
4
+ import numpy as np
5
+ from flask import Flask, request
6
+ from fastapi.responses import HTMLResponse
7
+ from fastapi.staticfiles import StaticFiles
8
+ from fastapi.templating import Jinja2Templates
9
+ from fastapi.responses import JSONResponse
10
+ from fastapi.exceptions import HTTPException
11
+ from typing import Optional
12
+ import pickle
13
+
14
+ app = FastAPI()
15
+
16
+
17
+ df= pd.read_csv('platforms_and_score.csv', sep=",")
18
+
19
+ # Configure a sample rate
20
+ app.mount("/static", StaticFiles(directory="static"), name="static")
21
+
22
+ templates = Jinja2Templates(directory="templates")
23
+
24
+ @app.get("/", response_class=HTMLResponse)
25
+ async def index(request: Request):
26
+ return templates.TemplateResponse("index.html", {"request": request})
27
+
28
+ # Loading information for the API
29
+ @app.get('/about')
30
+ async def about():
31
+ return 'API created with FastAPI and Uvicorn'
32
+
33
+ #API 1)
34
+ @app.get("/get_max_duration")
35
+ def get_max_duration(year: Optional[int] = None, platform: Optional[str] = None, duration_type: Optional[str] = None):
36
+ # Convert year to integer if provided
37
+ if year is not None and year != "":
38
+ year = int(year)
39
+ # Filter the DataFrame
40
+ if duration_type is not None:
41
+ filtered_df = df[df['duration_type'] == duration_type]
42
+ else:
43
+ filtered_df = df.copy()
44
+ if year is not None:
45
+ filtered_df = filtered_df[filtered_df['year'] == year]
46
+ if platform is not None:
47
+ filtered_df = filtered_df[filtered_df['platform'] == platform]
48
+ # Sort the DataFrame and get the row with the maximum duration
49
+ sorted_df = filtered_df.sort_values(by='duration_int', ascending=False)
50
+ max_duration = sorted_df.iloc[0][['title']]
51
+
52
+ return max_duration
53
+ """
54
+ This function calculates the Movie with the longest duration with optional filters of YEAR, PLATFORM and TYPE OF DURATION.
55
+ (the function should be called get_max_duration(year, platform, duration_type))
56
+ """
57
+
58
+ #API 2)
59
+ @app.get("/get_score_count/")
60
+ def get_score_count(platform: str, scored: int, year:int):
61
+ # Filter data based on conditions
62
+ df_filtered = df[(df["platform"] == platform) & (df["scored"] >= scored) & (df["year"] == year)]
63
+ # Count the number of rows that meet the conditions
64
+ cantidad = len(df_filtered)
65
+ # Return the number of times the condition is met
66
+ return cantidad
67
+ """
68
+ This function calculates the number of movies by platform with a score greater than XX in a given year.
69
+ (the function must be called get_score_count(platform, scored, year))
70
+ """
71
+
72
+ #API 3)
73
+ @app.get('/get_count_platform')
74
+ def get_count_platform(platform=None):
75
+ # if a platform is provided, filter the dataframe by that platform
76
+ if platform:
77
+ df_filtered = df[df['platform'] == platform]
78
+ else:
79
+ df_filtered = df
80
+
81
+ # count the number of movies per platform
82
+ count_by_platform = df_filtered['platform'].value_counts()
83
+
84
+ # if the platform exists in the dataframe, return the amount as an integer
85
+ if platform in count_by_platform.index:
86
+ return int(count_by_platform[platform])
87
+
88
+ # if no frame was provided, return the sum of all movies in the dataframe
89
+ elif platform is None:
90
+ return int(count_by_platform.sum())
91
+
92
+ # if the platform does not exist in the dataframe, return 0
93
+ else:
94
+ return 0
95
+ """
96
+ The above function calculates the number of movies per platform with the PLATFORM filter.
97
+ (The function should be called get_count_platform(platform))
98
+ """
99
+ #API 4)
100
+ @app.get('/get_actor')
101
+ def get_actor(platform:str, year:int):
102
+ global df
103
+ # Filter by year and platform
104
+ filtered_df = df[(df['platform'] == platform) & (df['year'] == year)]
105
+
106
+ # Count the number of times each actor appears in the 'cast' column
107
+ actor_count = filtered_df['cast'].str.split(',').explode().str.strip().value_counts()
108
+
109
+ # Return the actor that repeats the most
110
+ if actor_count.empty:
111
+ return None
112
+ else:
113
+ return actor_count.index[0]
114
+ """
115
+ The previous function identifies the Actor that is repeated the most according to platform and year.
116
+ (The function should be called get_actor(platform, year))
117
+ """
118
+
119
+ def cargar_modelo():
120
+ with open('C:/Users/rocio/OneDrive/Escritorio/FastAPI//modelo_recomendacion.pkl', 'rb') as archivo_modelo:
121
+ model = pickle.load('modelo_recomendacion.pkl')
122
+ return model
123
+
124
+ #API 4)
125
+ @app.get("/get_predictions")
126
+ async def get_predictions(userId: str, id: str):
127
+
128
+ # Cargar el modelo desde un archivo
129
+ with open('modelo_recomendacion.pkl', 'rb') as archivo:
130
+ model = pickle.load(archivo)
131
+
132
+ predictions = model.predict(userId, id)
133
+
134
+ if predictions.est >= 4:
135
+ mensaje = "Muy recomendada"
136
+ elif predictions.est >=3 and predictions.est < 4:
137
+ mensaje = "Recomendada"
138
+ else:
139
+ mensaje = "No te la recomiendo", predictions.est
140
+ return {"mensaje": mensaje}
141
+
142
+ """
143
+ LOREM
144
+ """
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ pandas
3
+ numpy
4
+ uvicorn
5
+ deta
6
+ Jinja2
7
+ flask