Files
2024-11-18 14:54:36 +08:00

372 lines
13 KiB
Python

import json
import time
from wordcloud import WordCloud
# python的可视化库,也是二级考试推荐的可视化库
import matplotlib.pyplot as plt
from django.shortcuts import render
from server.models import Task,StudentandTercher,Comment,Liuyan,CooperationMind,Jiaoben,Chat_info
from server_auth.models import MindUser
from zhipuai import ZhipuAI
def gml_chat(name,messages):
print("--------------------")
print(messages)
client = ZhipuAI(api_key="92a0e4868cda21d22fe6fd0f4c0bf6fb.oZ4IE9PPDOhsLKPp")
response = client.chat.completions.create(
model="glm-3-turbo",
# messages=[
# {
# "role": "user",
# "content": prompt
# }
# ],
messages =[{
"role": "user",
"content": "你现在是一名教育领域专家,请你从清晰性、完整性、布鲁姆认知层级三个方面对学小组作品内容进行分析,给我相应的返回结果即可。小组作品标题是:" + name + "小组作品内容是:" + messages
}],
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
return content
def front_comment(type):
front_datas = []
if len(type) == 0:
return front_datas
for i in range(len(type)):
front_data = f"""
<div class="rating">
<h1>{type[i]}</h1>
<input type="radio" name="rating{i}" id="5{i}" value="5" />
<label for="5{i}"><i class="icon ion-md-star-outline"></i></label>
<input type="radio" name="ratin{i}" id="4{i}" value="4" />
<label for="4{i}"><i class="icon ion-md-star-outline"></i></label>
<input type="radio" name="rating{i}" id="3{i}" value="3" />
<label for="3{i}"><i class="icon ion-md-star-outline"></i></label>
<input type="radio" name="rating{i}" id="2{i}" value="2" />
<label for="2{i}"><i class="icon ion-md-star-outline"></i></label>
<input type="radio" name="rating{i}" id="1{i}" value="1" />
<label for="1{i}"><i class="icon ion-md-star-outline"></i></label>
</div>"""
front_datas.append(front_data)
return front_datas
def to_dashboard(request):
"""
主界面
"""
jiaoben = Jiaoben.objects.order_by('id').last().jiaoben
param = {"page": "b9b101e","jiaoben": jiaoben}
return render(request, "html/dashboard.html", param)
def to_baiban(request):
"""
协作白板
"""
test_date = request.GET.get('test')
param = {"page": "share",
"path": test_date}
return render(request, "html/baiban.html", param)
def to_pinglun(request):
user = request.user
test_date = request.GET.get('id')
comment_list = Comment.objects.filter(pinglun_id=test_date) # 从数据库找出该文章的评论数据对象
comment_type = CooperationMind.objects.get(id=test_date).comment_type # 找出对应的comment_type字段
front_code = front_comment((json.loads(comment_type))['type'])
param = {"path": test_date,
"comment_list": comment_list,
"front_code": front_code}
print(test_date)
return render(request, "html/commont.html", param)
def to_admin(request):
username = request.user
# supper = MindUser.objects.filter(username=username).type_t_or_s
try:
if username.type_t_or_s == 3:
datas = MindUser.objects.filter(type_t_or_s=2)
return render(request, "html/admin.html",{"datas":datas})
else:
return render(request, "html/dashboard.html")
except:
return render(request, "html/dashboard.html")
def to_share(request):
"""
分享页面
"""
param = {"page": "share"}
return render(request, "html/dashboard.html", param)
def to_echarts(request):
"""
图表页面
"""
param = {"page": "share"}
return render(request, "html/echats.html", param)
def to_chat_room(request):
"""
聊天室页面
"""
test_date = request.GET.get('id')
name = CooperationMind.objects.get(id=test_date).coop_mind_name
room_id = int(test_date) + 10000
param = {"page": "chat_room",
"username":request.user.username,
"name":name,
"room_id":room_id}
return render(request, "html/wechat_2.html", param)
def to_rengwu(request):
"""
任务列表
"""
tasks = Task.objects.all() # 查询所有的任务
return render(request, "html/rengwu.html", {'tasks': tasks})
def to_kecheng(request):
"""
课程列表
"""
user = request.user
kechengs = StudentandTercher.objects.all() # 查询所有的任务
xiaozu = MindUser.objects.get(username=user).select_xiaozu
kz = MindUser.objects.get(username=user).select_class
xiaozu = json.loads(xiaozu)['xiaozu']
kc = json.loads(kz)['class']
test = zip(kc,xiaozu)
it3 = []
for it1,it2 in test:
it3.append(str(it1) + "课程" + str(it2) + "小组")
return render(request, "html/kecheng.html", {'kechengs': kechengs,"select":it3})
def to_write(request):
"""
协同编辑页面
"""
param = {"page": "write"}
return render(request, "html/write_2.html", param)
def to_star(request):
"""
收藏页面
"""
param = {"page": "star"}
return render(request, "html/dashboard.html", param)
def to_recycle(request):
"""
回收站
"""
param = {"page": "recycle"}
return render(request, "html/dashboard.html", param)
def to_detail(request):
"""
详情页面
"""
param = {'id': request.GET.get('id')}
return render(request, "html/detail.html", param)
def to_qunti(request):
"""
详情页面
"""
import re
import random
def remove_digits(s):
return re.sub(r'\d+', '', s)
param = {'id': request.GET.get('id')}
name = CooperationMind.objects.get(id=request.GET.get('id')).coop_mind_name
kecheng = json.loads(CooperationMind.objects.get(id=request.GET.get('id')).belong_to)
# 获取Student的所有记录
student = []
data = []
data_time = []
xyz = []
xyz2 = []
all_student = MindUser.objects.all()
chat_content = []
sui1 = []
sui2 = []
sui3 = []
sui4 = []
sui5 = []
sui6 = []
sui7 = []
sui8 = []
sui9 = []
# 遍历所有记录
import requests
def download_text_file(url,id):
# 发送GET请求
response = requests.get(url)
# 确保请求成功
response.raise_for_status()
# 读取内容
content = response.text
# 将内容写入本地文件
with open(f'{id}.txt', 'w', encoding='utf-8') as file:
file.write(content)
print("文件下载并保存成功。")
id_down = request.GET.get('id')
# 使用函数下载文件
download_url = f'http://192.168.1.13:9001/p/{id_down}/export/txt' # 替换成你的文件URL
download_text_file(download_url,id_down)
# 打开文件
with open(f'{id_down}.txt', 'r', encoding='utf-8') as file:
# 读取整个文件内容
content = file.read()
# 打印内容
print(content)
chat_content.append(content)
for stu in all_student:
class_name = json.loads(stu.select_class)['class']
xiaozu = json.loads(stu.select_xiaozu)['xiaozu']
test = zip(class_name, xiaozu)
# xiaozu = MindUser.objects.get(username=username).select_xiaozu
# kz = MindUser.objects.get(username=username).select_class
# xiaozu = json.loads(xiaozu)['xiaozu']
# kc = json.loads(kz)['class']
# test = zip(kc, xiaozu)
# it3 = []
# for it1, it2 in test:
# it3.append(str(it1) + "课程" + str(it2) + "小组")
it3 = []
for it1, it2 in test:
it3.append(str(it1) + "第" + str(it2) + "小组")
if name in it3:
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(user_id=id).values('comment_content')
content = CooperationMind.objects.get(id=request.GET.get('id')).snapshot_data
# 定义要删除的键
# def extract_topics(data):
# topics = []
# if 'topic' in data:
# topics.append(data['topic'])
# if 'children' in data:
# for child in data['children']:
# topics.extend(extract_topics(child))
# return topics
# print(extract_topics(content))
chat_content.append(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),random.uniform(120,150)])
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))
xyz2.append(random.uniform(200,600))
print("-----------------------------------------------")
print(chat_content)
for i in range(len(student)):
sui3.append(random.uniform(300, 700))
sui4.append(random.uniform(200, 600))
sui5.append(random.uniform(500, 900))
sui6.append(random.uniform(700, 600))
sui7.append(random.uniform(200, 300))
sui8.append(random.uniform(600, 900))
sui9.append(random.uniform(100, 200))
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)
datas2 = zip(student,data)
datas3 = zip(student,xyz,xyz2)
datas4 = zip(student,data_time)
student2 = student[:]
student3 = student[:]
random.shuffle(student2)
random.shuffle(student3)
student4 = zip(student,student3)
return render(request, "html/echats.html", {"param":param,"name":name,"student":student,"datas":datas,"datas2":datas2,"datas3":datas3,"student4"
:student4,"sui1":sui1,"sui2":sui2,"sui3":sui3,"sui4":sui4,"sui5":sui5,"sui6":sui6,"sui7":sui7,"sui8":sui8,"sui9":sui9,"datas4":datas4})
def to_gpt(request):
name = CooperationMind.objects.get(id=request.GET.get('id')).coop_mind_name
content = CooperationMind.objects.get(id=request.GET.get('id')).snapshot_data
gpt_content = gml_chat(name,content)
print(gpt_content)
return render(request, "html/gpt.html", {"gpt_content":gpt_content})
def share_to_detail(request):
"""
详情页面
"""
param = {'h': request.GET.get('h'), 'u': request.GET.get('u')}
return render(request, "html/detail.html", param)
def teacher_login(request):
"""
详情页面
"""
param = {'id': request.GET.get('id')}
return render(request, "html/login.html", param)
def to_information(request):
"""
个人信息页面
"""
param = {"page": "recycle"}
username = request.user
datas = MindUser.objects.filter(username=username)
xiaozu = MindUser.objects.get(username=username).select_xiaozu
kz = MindUser.objects.get(username=username).select_class
xiaozu = json.loads(xiaozu)['xiaozu']
kc = json.loads(kz)['class']
test = zip(kc, xiaozu)
it3 = []
for it1, it2 in test:
it3.append(str(it1) + "课程" + str(it2) + "小组")
return render(request, "html/informations.html", {"datas":datas,"it3":it3})
def to_liuyan(request):
"""
留言板页面
"""
# param = {"page": "recycle"}
datas = MindUser.objects.all()
liuyans = Liuyan.objects.filter(liuyan_id=request.user.id)
return render(request, "html/liuyan.html", {"datas":datas,"liuyans":liuyans})