mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 16:03:11 +02:00
AI API fully working
This commit is contained in:
parent
56ad8d937a
commit
c6322cb4be
@ -4,8 +4,8 @@ import pandas
|
||||
import json
|
||||
from configparser import ConfigParser
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
from flask_caching import Cache
|
||||
|
||||
app = Flask(__name__)
|
||||
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
|
||||
@app.route("/api/v3/ai/<string:oauth_ID>", methods=["GET"])
|
||||
def get_recommendations(oauth_ID):
|
||||
#request od frontu na rekomendacje
|
||||
#wysyłanie requestu do AI API o rekomendacje dla usera
|
||||
#przesłanie danych do
|
||||
|
||||
movies = [49026, 155, 312113] # mock values to be received from backend
|
||||
url = 'http://localhost:8080/api/v3/AI_recommendations' # nie wiem, jakie powinno być url
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select movie_ID from ratings where oauth_ID='{}'", oauth_ID)
|
||||
res = cursor.fetchall()
|
||||
movies = [int(i) for i in res[0]]
|
||||
url = 'http://localhost:4200/api/v3/AI_recommendations'
|
||||
response = requests.post(url,
|
||||
json=movies,
|
||||
headers={'Content-Type': 'application/json'})
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
[postgres]
|
||||
host=db
|
||||
host=localhost
|
||||
database=test_db
|
||||
user=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
|
||||
|
||||
[
|
||||
|
||||
@ -1,26 +1,20 @@
|
||||
import hashlib
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from ast import literal_eval
|
||||
from sklearn.feature_extraction.text import CountVectorizer
|
||||
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_caching import Cache
|
||||
|
||||
|
||||
config = {
|
||||
"DEBUG": True, # some Flask specific configs
|
||||
"CACHE_TYPE": "SimpleCache",
|
||||
"CACHE_DEFAULT_TIMEOUT": 300
|
||||
}
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
app.config.from_mapping(config)
|
||||
cache = Cache(app)
|
||||
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
||||
db_connector = None
|
||||
conn = None
|
||||
|
||||
|
||||
def get_director(x):
|
||||
@ -130,8 +124,8 @@ class MovieRecommender:
|
||||
|
||||
|
||||
recommender = MovieRecommender()
|
||||
recommender.fit('movie_recommendations/datasets/tmdb_5000_credits.csv',
|
||||
'movie_recommendations/datasets/tmdb_5000_movies.csv')
|
||||
recommender.fit('datasets/tmdb_5000_credits.csv',
|
||||
'datasets/tmdb_5000_movies.csv')
|
||||
|
||||
|
||||
def make_cache_key():
|
||||
@ -147,13 +141,30 @@ def make_cache_key():
|
||||
def AI_recommendations():
|
||||
ids = request.get_json()
|
||||
recommendations = recommender.get_recommendations(ids)
|
||||
recommendations[0] = datetime.now()
|
||||
return jsonify(recommendations)
|
||||
|
||||
|
||||
# Przykładowe użycie:
|
||||
# if __name__ == "__main__":
|
||||
# recommender = MovieRecommender()
|
||||
# recommender.fit('datasets/tmdb_5000_credits.csv', 'datasets/tmdb_5000_movies.csv')
|
||||
# recommendations = recommender.get_recommendations([49026, 155, 312113])
|
||||
# print(recommendations)
|
||||
if __name__ == "__main__":
|
||||
config = ConfigParser()
|
||||
config.read("../connector/Include/init_scripts/constants.ini")
|
||||
|
||||
while True:
|
||||
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