1 Star 0 Fork 8

于少文/ 基于spark大数据的音乐推荐

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SparkFlask.py 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
Huang_Daxian 提交于 2021-07-14 07:44 . 初始化提交所有代码
from flask import render_template
from flask import Flask,request,redirect
# from livereload import Server
from pyspark.ml.recommendation import ALS
from pyspark.ml.recommendation import ALSModel
from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.sql.functions import lit
import json
import time
def makeRecommendations(model,userID,number):
toRecommend = modelnew.itemFactors.selectExpr("id as artist").withColumn("user",lit(userID))
toRecommend2 = toRecommend.withColumn("artist",toRecommend['artist'].cast("Int")).withColumn("user",toRecommend['user'].cast('Int'))
toRecommend2.printSchema()
www = modelnew.transform(toRecommend2).select("artist","prediction").orderBy('prediction',ascending = False).take(number)
return www
def artistPredict(userID):
recommend = makeRecommendations(modelnew,userID,10)
www = recommend
for j in range(len(www)):
i = www[j]
#print(i.artist)
name = artistByID.filter(artistByID['artist'] == str(i.artist)).select('name').collect()[0]
i = i.asDict()
#print(name)
i.update({'name':name.name })
www[j] = i
f = open('/usr/local/spark/test/code/static/data/predict.json', 'w')
f.write(json.dumps(www))
f.close()
return json.dumps(www)
app = Flask(__name__)
@app.route('/')
def index():
#使用 render_template() 方法来渲染模板
return render_template('index.html')
@app.route('/direct',methods=['GET','POST'])
def predict():
if request.method == 'POST':
userID = request.form['userID']
print(userID)
message = userID
mess = artistPredict(userID)
print(mess)
time.sleep(3)
# return reder_template('genre-predict.html')
return render_template('genre-predict.html',message = mess,userid=message)
@app.route('/<filename>')
def req_file(filename):
return render_template(filename)
if __name__ == '__main__':
app.DEBUG=True#代码调试立即生效
app.jinja_env.auto_reload = True#模板调试立即生效
sc = SparkContext('local','test')
sc.setLogLevel("WARN")
spark = SparkSession.builder.getOrCreate()
modelnew = ALSModel.load("/usr/local/spark/Model/modelnew")
artistByID = spark.read.csv("/usr/local/spark/Model/artistByID").toDF("artist","name")
trainData = spark.read.csv("/usr/local/spark/Model/trainData").toDF("user","artist","count")
trainData.cache()
artistByID.cache()
trainData= trainData.withColumn('count',trainData['count'].cast('int'))
trainData.printSchema()
app.run()#用 run() 函数来让应用运行在本地服务器上
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Scala
1
https://gitee.com/yu-shaowen121/bigdata.git
git@gitee.com:yu-shaowen121/bigdata.git
yu-shaowen121
bigdata
基于spark大数据的音乐推荐
master

搜索帮助