new functions
This commit is contained in:
@@ -158,3 +158,14 @@ class Liuyan(models.Model): # 定义留言板模型
|
|||||||
verbose_name = '留言'
|
verbose_name = '留言'
|
||||||
verbose_name_plural = verbose_name
|
verbose_name_plural = verbose_name
|
||||||
|
|
||||||
|
class Chat_info(models.Model): # 定义聊天板模型
|
||||||
|
# article = models.ForeignKey(to=Article, on_delete=models.DO_NOTHING, verbose_name='评论文章')
|
||||||
|
chat_content = models.TextField(verbose_name='留言内容')
|
||||||
|
user_chat_name = models.CharField(verbose_name='聊天人姓名', max_length=128, null=False, blank=False) #留言人id
|
||||||
|
chat_time = models.DateTimeField(auto_now_add=True, verbose_name='留言时间')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
db_table = 'chat_tb'
|
||||||
|
verbose_name = '聊天'
|
||||||
|
verbose_name_plural = verbose_name
|
||||||
|
|
||||||
|
|||||||
+47
-12
@@ -1,8 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
from wordcloud import WordCloud
|
||||||
|
# python的可视化库,也是二级考试推荐的可视化库
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from server.models import Task,StudentandTercher,Comment,Liuyan,CooperationMind,Jiaoben
|
from server.models import Task,StudentandTercher,Comment,Liuyan,CooperationMind,Jiaoben,Chat_info
|
||||||
from server_auth.models import MindUser
|
from server_auth.models import MindUser
|
||||||
|
|
||||||
def front_comment(type):
|
def front_comment(type):
|
||||||
@@ -162,6 +165,16 @@ def to_qunti(request):
|
|||||||
xyz = []
|
xyz = []
|
||||||
xyz2 = []
|
xyz2 = []
|
||||||
all_student = MindUser.objects.all()
|
all_student = MindUser.objects.all()
|
||||||
|
chat_content = []
|
||||||
|
sui1 = []
|
||||||
|
sui2 = []
|
||||||
|
sui3 = []
|
||||||
|
sui4 = []
|
||||||
|
sui5 = []
|
||||||
|
sui6 = []
|
||||||
|
sui7 = []
|
||||||
|
sui8 = []
|
||||||
|
sui9 = []
|
||||||
# 遍历所有记录
|
# 遍历所有记录
|
||||||
for stu in all_student:
|
for stu in all_student:
|
||||||
class_name = json.loads(stu.select_class)['class']
|
class_name = json.loads(stu.select_class)['class']
|
||||||
@@ -180,22 +193,31 @@ def to_qunti(request):
|
|||||||
it3.append(str(it1) + "第" + str(it2) + "小组")
|
it3.append(str(it1) + "第" + str(it2) + "小组")
|
||||||
if name in it3:
|
if name in it3:
|
||||||
student.append(stu.username)
|
student.append(stu.username)
|
||||||
|
print("-----------------------------------------------")
|
||||||
|
print(stu.username)
|
||||||
|
chat_info = Chat_info.objects.filter(user_chat_name=stu.username).values('chat_content')
|
||||||
|
id = MindUser.objects.get(username=stu.username).id
|
||||||
|
pinglun_info = Comment.objects.filter(pinglun_id=id).values('comment_content')
|
||||||
|
print("-----------------------------------------------")
|
||||||
|
print(len(chat_info))
|
||||||
|
sui2.append(len(chat_info))
|
||||||
|
sui1.append(len(pinglun_info))
|
||||||
|
if len(chat_info) != 0:
|
||||||
|
for i in range(len(chat_info)):
|
||||||
|
chat_content.append(chat_info[i]['chat_content'])
|
||||||
|
if len(pinglun_info) != 0:
|
||||||
|
for i in range(len(pinglun_info)):
|
||||||
|
chat_content.append(pinglun_info[i]['comment_content'])
|
||||||
|
print("-----------------------------------------------")
|
||||||
|
print(pinglun_info)
|
||||||
|
# chat_content.append()
|
||||||
data.append([random.uniform(110,150),random.uniform(100,150),random.uniform(100,150)])
|
data.append([random.uniform(110,150),random.uniform(100,150),random.uniform(100,150)])
|
||||||
data_time.append([random.uniform(10,20),random.uniform(20,40),random.uniform(40,60),random.uniform(60,80),random.uniform(80,100)])
|
data_time.append([random.uniform(10,20),random.uniform(20,40),random.uniform(40,60),random.uniform(60,80),random.uniform(80,100)])
|
||||||
xyz.append(random.uniform(300,800))
|
xyz.append(random.uniform(300,800))
|
||||||
xyz2.append(random.uniform(200,600))
|
xyz2.append(random.uniform(200,600))
|
||||||
sui1 = []
|
print("-----------------------------------------------")
|
||||||
sui2 = []
|
print(chat_content)
|
||||||
sui3 = []
|
|
||||||
sui4 = []
|
|
||||||
sui5 = []
|
|
||||||
sui6 = []
|
|
||||||
sui7 = []
|
|
||||||
sui8 = []
|
|
||||||
sui9 = []
|
|
||||||
for i in range(len(student)):
|
for i in range(len(student)):
|
||||||
sui1.append(random.uniform(200, 600))
|
|
||||||
sui2.append(random.uniform(100, 400))
|
|
||||||
sui3.append(random.uniform(300, 700))
|
sui3.append(random.uniform(300, 700))
|
||||||
sui4.append(random.uniform(200, 600))
|
sui4.append(random.uniform(200, 600))
|
||||||
sui5.append(random.uniform(500, 900))
|
sui5.append(random.uniform(500, 900))
|
||||||
@@ -206,6 +228,19 @@ def to_qunti(request):
|
|||||||
|
|
||||||
|
|
||||||
print(remove_digits(kecheng['belong'][0]))
|
print(remove_digits(kecheng['belong'][0]))
|
||||||
|
text_cut = '/'.join(chat_content)
|
||||||
|
|
||||||
|
# 看一下连接后的样子
|
||||||
|
# 关键点 text_cut 是词云要处理的内容
|
||||||
|
print(text_cut)
|
||||||
|
wordcloud = WordCloud(background_color='white', font_path='msyh.ttc', width=1000, height=860, margin=2).generate(
|
||||||
|
text_cut)
|
||||||
|
# 显示图片
|
||||||
|
plt.imshow(wordcloud)
|
||||||
|
plt.axis('off')
|
||||||
|
plt.savefig('./static/pic/wordcloud.png') # 保存为PNG格式,你可以根据需要更改文件名和格式
|
||||||
|
# plt.show()
|
||||||
|
|
||||||
datas = zip(student,data)
|
datas = zip(student,data)
|
||||||
datas2 = zip(student,data)
|
datas2 = zip(student,data)
|
||||||
datas3 = zip(student,xyz,xyz2)
|
datas3 = zip(student,xyz,xyz2)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ def gml_chat(messages):
|
|||||||
# ],
|
# ],
|
||||||
messages =[{
|
messages =[{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
"content": "你现在是一个学习站点的评论分析机器人,我接下来讲给你用户的评论内容,给出一个1-100分的打分分值并且返回你对它评论的情感分析以及内容分析以及揣测用户的想法,给我相应的返回结果即可。用户评论内容是:" + messages
|
"content": "你现在是一名教育领域专家,请你从清晰性、完整性、布鲁姆认知层级三个方面对学生评价内容进行分析,并给出相应的分数,总分为10分,给我相应的返回结果即可。用户评论内容是:" + messages
|
||||||
|
|
||||||
}],
|
}],
|
||||||
top_p=0.7,
|
top_p=0.7,
|
||||||
|
|||||||
@@ -610,6 +610,9 @@
|
|||||||
<div id="app2" style="width: 1000px;height:500px;"></div>
|
<div id="app2" style="width: 1000px;height:500px;"></div>
|
||||||
<div id="app3" style="width: 1300px;height:500px;"></div>
|
<div id="app3" style="width: 1300px;height:500px;"></div>
|
||||||
<div id="app4" style="width: 1300px;height:500px;"></div>
|
<div id="app4" style="width: 1300px;height:500px;"></div>
|
||||||
|
<p>
|
||||||
|
<img src="/static/pic/wordcloud.png" alt="germany" width="1000px" height="800px">
|
||||||
|
</p>
|
||||||
</main>
|
</main>
|
||||||
<!-- 右侧内容 start-->
|
<!-- 右侧内容 start-->
|
||||||
|
|
||||||
@@ -768,7 +771,7 @@ option = {
|
|||||||
],
|
],
|
||||||
series: [
|
series: [
|
||||||
{
|
{
|
||||||
name: '写作字数',
|
name: '评论条数',
|
||||||
type: 'bar',
|
type: 'bar',
|
||||||
emphasis: {
|
emphasis: {
|
||||||
focus: 'series'
|
focus: 'series'
|
||||||
|
|||||||
@@ -0,0 +1,172 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# 后端
|
||||||
|
# WS server example that synchronizes state across clients
|
||||||
|
import pymysql
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import websockets
|
||||||
|
import re
|
||||||
|
import datetime
|
||||||
|
from zhipuai import ZhipuAI
|
||||||
|
|
||||||
|
conversation_history = []
|
||||||
|
async def gml_chat(messages,conversation_history):
|
||||||
|
if len(conversation_history) >= 4:
|
||||||
|
conversation_history.pop(0)
|
||||||
|
print("-==========================---")
|
||||||
|
print(conversation_history)
|
||||||
|
client = ZhipuAI(api_key="92a0e4868cda21d22fe6fd0f4c0bf6fb.oZ4IE9PPDOhsLKPp")
|
||||||
|
conversation_history.append({"role": "user", "content": messages})
|
||||||
|
# conversation_history.extend(messages)
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model="glm-3-turbo",
|
||||||
|
# messages=[
|
||||||
|
# {
|
||||||
|
# "role": "user",
|
||||||
|
# "content": prompt
|
||||||
|
# }
|
||||||
|
# ],
|
||||||
|
messages = conversation_history,
|
||||||
|
top_p=0.7,
|
||||||
|
temperature=0.95,
|
||||||
|
max_tokens=1024,
|
||||||
|
stream=True,
|
||||||
|
)
|
||||||
|
content = ""
|
||||||
|
for trunk in response:
|
||||||
|
# print(trunk.choices[0].delta)
|
||||||
|
content = content + trunk.choices[0].delta.content
|
||||||
|
conversation_history.append({"role": "assistant", "content": content})
|
||||||
|
robot_message = json.dumps(
|
||||||
|
{"type": "user", "content": content, "from": "机器人"})
|
||||||
|
await asyncio.wait([user.send(robot_message) for user in USERS.values()])
|
||||||
|
return content
|
||||||
|
# print(gml_chat("你好"))
|
||||||
|
|
||||||
|
# 名字:websockets
|
||||||
|
USERS = {}
|
||||||
|
async def chat(websocket, path):
|
||||||
|
# 握手
|
||||||
|
await websocket.send(json.dumps({"type": "handshake"}))
|
||||||
|
async for message in websocket:
|
||||||
|
data = json.loads(message)
|
||||||
|
print("---------------------")
|
||||||
|
print(data)
|
||||||
|
print(data['content'])
|
||||||
|
message = ''
|
||||||
|
|
||||||
|
# 用户发信息
|
||||||
|
if data["type"] == 'send':
|
||||||
|
name = '404'
|
||||||
|
for k, v in USERS.items():
|
||||||
|
if v == websocket:
|
||||||
|
name = k
|
||||||
|
pattern = r"@机器人"
|
||||||
|
matches = re.findall(pattern, data['content'])
|
||||||
|
# 建立数据库连接
|
||||||
|
conn = pymysql.connect(
|
||||||
|
host='localhost', # 主机名(或IP地址)
|
||||||
|
port=3306, # 端口号,默认为3306
|
||||||
|
user='root', # 用户名
|
||||||
|
password='guoan123', # 密码
|
||||||
|
charset='utf8mb4' # 设置字符编码
|
||||||
|
)
|
||||||
|
# 创建数据库游标对象
|
||||||
|
cursor = conn.cursor()
|
||||||
|
# 选择数据表
|
||||||
|
conn.select_db("jira3")
|
||||||
|
sql = "INSERT INTO chat_tb (chat_content,user_chat_name,chat_time) VALUES ('%s','%s','%s')" %(data['content'], name,datetime.datetime.now())
|
||||||
|
cursor.execute(sql)
|
||||||
|
conn.commit()
|
||||||
|
# 关闭游标和连接
|
||||||
|
cursor.close()
|
||||||
|
conn.close()
|
||||||
|
if len(USERS) != 0:
|
||||||
|
if len(matches):
|
||||||
|
question_rebot = re.sub(pattern, "", data['content'])
|
||||||
|
# robot_reply = await gml_chat(question_rebot) # 获取机器人的回复
|
||||||
|
message = json.dumps(
|
||||||
|
{"type": "user", "content": data["content"], "from": name})
|
||||||
|
await asyncio.wait([user.send(message) for user in USERS.values()])
|
||||||
|
robot_reply = await gml_chat(question_rebot,conversation_history) # 获取机器人的回复
|
||||||
|
# await asyncio.wait([user.send(message) for user in USERS.values()])
|
||||||
|
# robot_message = json.dumps(
|
||||||
|
# {"type": "user", "content": robot_reply, "from": "机器人"})
|
||||||
|
# # await asyncio.wait([user.send(robot_message) for user in USERS.values()])
|
||||||
|
else:
|
||||||
|
message = json.dumps(
|
||||||
|
{"type": "user", "content": data["content"], "from": name})
|
||||||
|
await asyncio.wait([user.send(message) for user in USERS.values()])
|
||||||
|
|
||||||
|
# if len(USERS) != 0: # asyncio.wait doesn't accept an empty list
|
||||||
|
# message = json.dumps(
|
||||||
|
# {"type": "user", "content": data["content"], "from": name})
|
||||||
|
|
||||||
|
#用户登录
|
||||||
|
elif data["type"] == 'login':
|
||||||
|
USERS[data["content"]] = websocket
|
||||||
|
if len(USERS) != 0: # asyncio.wait doesn't accept an empty list
|
||||||
|
message = json.dumps(
|
||||||
|
{"type": "login", "content": data["content"], "user_list": list(USERS.keys())})
|
||||||
|
await asyncio.wait([user.send(message) for user in USERS.values()])
|
||||||
|
#用户退出
|
||||||
|
elif data["type"] == 'logout':
|
||||||
|
del USERS[data["content"]]
|
||||||
|
if len(USERS) != 0: # asyncio.wait doesn't accept an empty list
|
||||||
|
message = json.dumps(
|
||||||
|
{"type": "logout", "content": data["content"], "user_list": list(USERS.keys())})
|
||||||
|
await asyncio.wait([user.send(message) for user in USERS.values()])
|
||||||
|
|
||||||
|
# 群发
|
||||||
|
# await asyncio.wait([user.send(message) for user in USERS.values()])
|
||||||
|
start_server1 = websockets.serve(chat, "0.0.0.0", 10001)
|
||||||
|
start_server2 = websockets.serve(chat, "0.0.0.0", 10002)
|
||||||
|
start_server3 = websockets.serve(chat, "0.0.0.0", 10003)
|
||||||
|
start_server4 = websockets.serve(chat, "0.0.0.0", 10004)
|
||||||
|
|
||||||
|
start_server5 = websockets.serve(chat, "0.0.0.0", 10005)
|
||||||
|
start_server6 = websockets.serve(chat, "0.0.0.0", 10006)
|
||||||
|
|
||||||
|
start_server7 = websockets.serve(chat, "0.0.0.0", 10007)
|
||||||
|
start_server8 = websockets.serve(chat, "0.0.0.0", 10008)
|
||||||
|
start_server9 = websockets.serve(chat, "0.0.0.0", 10009)
|
||||||
|
start_server10 = websockets.serve(chat, "0.0.0.0", 10010)
|
||||||
|
start_server11 = websockets.serve(chat, "0.0.0.0", 10011)
|
||||||
|
start_server12 = websockets.serve(chat, "0.0.0.0", 10012)
|
||||||
|
start_server13= websockets.serve(chat, "0.0.0.0", 10013)
|
||||||
|
start_server14 = websockets.serve(chat, "0.0.0.0", 10014)
|
||||||
|
start_server15 = websockets.serve(chat, "0.0.0.0", 10015)
|
||||||
|
start_server16 = websockets.serve(chat, "0.0.0.0", 10016)
|
||||||
|
|
||||||
|
start_server17 = websockets.serve(chat, "0.0.0.0", 10017)
|
||||||
|
start_server18 = websockets.serve(chat, "0.0.0.0", 10018)
|
||||||
|
start_server19 = websockets.serve(chat, "0.0.0.0", 10019)
|
||||||
|
start_server20 = websockets.serve(chat, "0.0.0.0", 10020)
|
||||||
|
start_server21 = websockets.serve(chat, "0.0.0.0", 10021)
|
||||||
|
start_server22 = websockets.serve(chat, "0.0.0.0", 10022)
|
||||||
|
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server1)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server2)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server3)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server4)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server5)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server6)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server7)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server8)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server9)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server10)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server11)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server12)
|
||||||
|
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server13)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server14)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server15)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server16)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server17)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server18)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server19)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server20)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server21)
|
||||||
|
asyncio.get_event_loop().run_until_complete(start_server22)
|
||||||
|
|
||||||
|
asyncio.get_event_loop().run_forever()
|
||||||
Reference in New Issue
Block a user