mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 17:23:03 +02:00
AI API fully working
This commit is contained in:
parent
56ad8d937a
commit
c6322cb4be
@ -4,8 +4,8 @@ import pandas
|
|||||||
import json
|
import json
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from flask_caching import Cache
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
||||||
@ -57,12 +57,11 @@ def add_user(oauth_ID, username):
|
|||||||
#roboczy endpoint służący do wyciąganiu rekomendacji
|
#roboczy endpoint służący do wyciąganiu rekomendacji
|
||||||
@app.route("/api/v3/ai/<string:oauth_ID>", methods=["GET"])
|
@app.route("/api/v3/ai/<string:oauth_ID>", methods=["GET"])
|
||||||
def get_recommendations(oauth_ID):
|
def get_recommendations(oauth_ID):
|
||||||
#request od frontu na rekomendacje
|
cursor = conn.cursor()
|
||||||
#wysyłanie requestu do AI API o rekomendacje dla usera
|
cursor.execute("select movie_ID from ratings where oauth_ID='{}'", oauth_ID)
|
||||||
#przesłanie danych do
|
res = cursor.fetchall()
|
||||||
|
movies = [int(i) for i in res[0]]
|
||||||
movies = [49026, 155, 312113] # mock values to be received from backend
|
url = 'http://localhost:4200/api/v3/AI_recommendations'
|
||||||
url = 'http://localhost:8080/api/v3/AI_recommendations' # nie wiem, jakie powinno być url
|
|
||||||
response = requests.post(url,
|
response = requests.post(url,
|
||||||
json=movies,
|
json=movies,
|
||||||
headers={'Content-Type': 'application/json'})
|
headers={'Content-Type': 'application/json'})
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
[postgres]
|
[postgres]
|
||||||
host=db
|
host=localhost
|
||||||
database=test_db
|
database=test_db
|
||||||
user=root
|
user=root
|
||||||
password=root
|
password=root
|
||||||
|
|||||||
@ -3,7 +3,11 @@ GET http://127.0.0.1:5000/boop
|
|||||||
|
|
||||||
###
|
###
|
||||||
|
|
||||||
POST http://127.0.0.1:5000/api/v3/AI_recommendations
|
GET http://127.0.0.1:8090/api/v3/ai/1111
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
POST http://127.0.0.1:4200/api/v3/AI_recommendations
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
[
|
[
|
||||||
|
|||||||
@ -1,26 +1,20 @@
|
|||||||
import hashlib
|
|
||||||
import json
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
from sklearn.feature_extraction.text import CountVectorizer
|
from sklearn.feature_extraction.text import CountVectorizer
|
||||||
from sklearn.metrics.pairwise import cosine_similarity
|
from sklearn.metrics.pairwise import cosine_similarity
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
from configparser import ConfigParser
|
||||||
|
import psycopg2
|
||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
from flask_caching import Cache
|
from flask_caching import Cache
|
||||||
|
|
||||||
|
|
||||||
config = {
|
|
||||||
"DEBUG": True, # some Flask specific configs
|
|
||||||
"CACHE_TYPE": "SimpleCache",
|
|
||||||
"CACHE_DEFAULT_TIMEOUT": 300
|
|
||||||
}
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
||||||
app.config.from_mapping(config)
|
db_connector = None
|
||||||
cache = Cache(app)
|
conn = None
|
||||||
|
|
||||||
|
|
||||||
def get_director(x):
|
def get_director(x):
|
||||||
@ -130,8 +124,8 @@ class MovieRecommender:
|
|||||||
|
|
||||||
|
|
||||||
recommender = MovieRecommender()
|
recommender = MovieRecommender()
|
||||||
recommender.fit('movie_recommendations/datasets/tmdb_5000_credits.csv',
|
recommender.fit('datasets/tmdb_5000_credits.csv',
|
||||||
'movie_recommendations/datasets/tmdb_5000_movies.csv')
|
'datasets/tmdb_5000_movies.csv')
|
||||||
|
|
||||||
|
|
||||||
def make_cache_key():
|
def make_cache_key():
|
||||||
@ -147,13 +141,30 @@ def make_cache_key():
|
|||||||
def AI_recommendations():
|
def AI_recommendations():
|
||||||
ids = request.get_json()
|
ids = request.get_json()
|
||||||
recommendations = recommender.get_recommendations(ids)
|
recommendations = recommender.get_recommendations(ids)
|
||||||
recommendations[0] = datetime.now()
|
|
||||||
return jsonify(recommendations)
|
return jsonify(recommendations)
|
||||||
|
|
||||||
|
|
||||||
# Przykładowe użycie:
|
if __name__ == "__main__":
|
||||||
# if __name__ == "__main__":
|
config = ConfigParser()
|
||||||
# recommender = MovieRecommender()
|
config.read("../connector/Include/init_scripts/constants.ini")
|
||||||
# recommender.fit('datasets/tmdb_5000_credits.csv', 'datasets/tmdb_5000_movies.csv')
|
|
||||||
# recommendations = recommender.get_recommendations([49026, 155, 312113])
|
while True:
|
||||||
# print(recommendations)
|
try:
|
||||||
|
conn = psycopg2.connect(
|
||||||
|
host=config["postgres"]["host"],
|
||||||
|
database=config["postgres"]["database"],
|
||||||
|
user=config["postgres"]["user"],
|
||||||
|
password=config["postgres"]["password"],
|
||||||
|
port=int(config["postgres"]["port"])
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
print("Trying to connect with database")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
cache.init_app(app)
|
||||||
|
app.run(host="0.0.0.0", port=4200, debug=False)
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user