添加评论机器人与教师端脚本发布。

This commit is contained in:
2024-06-08 15:32:57 +08:00
parent 1c9972c389
commit c98a5ffeba
7 changed files with 98 additions and 24 deletions
+10
View File
@@ -119,6 +119,15 @@ class Task(AbstractModel):
def __str__(self): def __str__(self):
return self.title return self.title
class Jiaoben(models.Model):
"""
存放脚本信息
"""
jiaoben = models.CharField(verbose_name='脚本信息', max_length=10240, null=False, blank=False)
class Meta:
db_table = 'jiaoben_tb'
verbose_name = '脚本'
verbose_name_plural = verbose_name
class Comment(models.Model): # 定义评论模型 class Comment(models.Model): # 定义评论模型
@@ -129,6 +138,7 @@ class Comment(models.Model): # 定义评论模型
comment_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间') comment_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')
pre_comment = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True, pre_comment = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True,
verbose_name='父评论id') # 父级评论,如果没有父级则为空NULL, "self"表示外键关联自己 verbose_name='父评论id') # 父级评论,如果没有父级则为空NULL, "self"表示外键关联自己
gpt_content = models.TextField(verbose_name='GPT返回结果',null=True, blank=True)
# comment_type = models.CharField(verbose_name='评论类型', max_length=1024, null=False, blank=False) # comment_type = models.CharField(verbose_name='评论类型', max_length=1024, null=False, blank=False)
# comment_type = models.TextField(verbose_name='评论类型', default=json.dumps({"type": []})) # comment_type = models.TextField(verbose_name='评论类型', default=json.dumps({"type": []}))
class Meta: class Meta:
+1
View File
@@ -56,6 +56,7 @@ urlpatterns = [
# --------------------------------------------------- # ---------------------------------------------------
path('post_task/', post_task, name='post_task'), path('post_task/', post_task, name='post_task'),
path('jiaoben/', jiaoben, name='jiaoben'),
path('post_kecheng/', post_kecheng, name='post_kecheng'), path('post_kecheng/', post_kecheng, name='post_kecheng'),
path('come_kecheng/', come_kecheng, name='come_kecheng'), path('come_kecheng/', come_kecheng, name='come_kecheng'),
+3 -2
View File
@@ -2,7 +2,7 @@ import json
import time import time
from django.shortcuts import render from django.shortcuts import render
from server.models import Task,StudentandTercher,Comment,Liuyan,CooperationMind from server.models import Task,StudentandTercher,Comment,Liuyan,CooperationMind,Jiaoben
from server_auth.models import MindUser from server_auth.models import MindUser
def front_comment(type): def front_comment(type):
@@ -30,7 +30,8 @@ def to_dashboard(request):
""" """
主界面 主界面
""" """
param = {"page": "b9b101e"} jiaoben = Jiaoben.objects.order_by('id').last().jiaoben
param = {"page": "b9b101e","jiaoben": jiaoben}
return render(request, "html/dashboard.html", param) return render(request, "html/dashboard.html", param)
def to_baiban(request): def to_baiban(request):
+50 -4
View File
@@ -13,11 +13,41 @@ from django.shortcuts import render
from server.taskform import TaskForm from server.taskform import TaskForm
from django.utils import timezone from django.utils import timezone
from server.models import Task,StudentandTercher,MindUser,Liuyan from server.models import Task,StudentandTercher,MindUser,Liuyan,Jiaoben
from server.serializers import * from server.serializers import *
import json import json
import asyncio
from zhipuai import ZhipuAI
def gml_chat(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": "你现在是一个学习站点的评论分析机器人,我接下来讲给你用户的评论内容,给出一个1-100分的打分分值并且返回你对它评论的情感分析以及内容分析以及揣测用户的想法,给我相应的返回结果即可。用户评论内容是:" + 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
class AuthBackend(ModelBackend): class AuthBackend(ModelBackend):
""" """
@@ -153,6 +183,22 @@ def post_task(request):
else: else:
return render(request, 'html/dashboard.html') return render(request, 'html/dashboard.html')
def jiaoben(request):
if request.user.type_t_or_s == 1:
if request.method == 'POST':
jiaoben = request.POST.get("jiaoben")
print(jiaoben)
jiaoben = Jiaoben(jiaoben=jiaoben)
jiaoben.save()
# rengwu = Task(title=title, description=description, publish_time=start_time, deadline=end_time,publisher=request.user)
# rengwu.save()
return http.JsonResponse({'code': CODE_SUCCESS, 'msg': '发布成功'})
else:
return render(request, 'html/post_task.html')
else:
return render(request, 'html/dashboard.html')
def post_kecheng(request): def post_kecheng(request):
if request.user.type_t_or_s == 1: if request.user.type_t_or_s == 1:
if request.method == 'POST': if request.method == 'POST':
@@ -210,12 +256,12 @@ def comment_control(request): # 提交评论的处理函数
pid = request.POST.get('pid') pid = request.POST.get('pid')
id = MindUser.objects.get(username=request.user.username).id id = MindUser.objects.get(username=request.user.username).id
author_id = id # 获取当前用户的ID author_id = id # 获取当前用户的ID
gpt_content = gml_chat(str(comment_content))
Comment.objects.create(comment_content=comment_content, pre_comment_id=pid, pinglun_id=article_id, Comment.objects.create(comment_content=comment_content, pre_comment_id=pid, pinglun_id=article_id,
user_id=author_id) # 将提交的数据保存到数据库中 user_id=author_id,gpt_content=gpt_content) # 将提交的数据保存到数据库中
article = list( article = list(
Comment.objects.values('id', 'comment_content', 'pre_comment_id', 'pinglun_id', 'user_id', Comment.objects.values('id', 'comment_content', 'pre_comment_id', 'pinglun_id', 'user_id',
'comment_time')) # 以键值对的形式取出评论对象,并且转化为列表list类型 'comment_time','gpt_content')) # 以键值对的形式取出评论对象,并且转化为列表list类型
return JsonResponse(article, safe=False) # JsonResponse返回JSON字符串,自动序列化,如果不是字典类型,则需要添加safe参数为False return JsonResponse(article, safe=False) # JsonResponse返回JSON字符串,自动序列化,如果不是字典类型,则需要添加safe参数为False
+1 -1
View File
@@ -114,13 +114,13 @@
<p id="p">原评论内容:{{ comment.pre_comment.comment_content }}</p> {# 如果有父级评论,则在中间显示父级评论的评论内容 #} <p id="p">原评论内容:{{ comment.pre_comment.comment_content }}</p> {# 如果有父级评论,则在中间显示父级评论的评论内容 #}
{% endif %} {% endif %}
<p>评论内容:{{ comment.comment_content }}</p> <p>评论内容:{{ comment.comment_content }}</p>
<p>GPT分析内容:{{ comment.gpt_content }}</p>
<hr> <hr>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
{# 提交评论的JS,发送Ajax请求 #} {# 提交评论的JS,发送Ajax请求 #}
<script> <script>
var pid = "" {# 设置一个变量pid默认为空,用于后面作为数据库存储的父级评论的ID,如果没有父级评论则为空,子评论有父级评论 #} var pid = "" {# 设置一个变量pid默认为空,用于后面作为数据库存储的父级评论的ID,如果没有父级评论则为空,子评论有父级评论 #}
+6 -5
View File
@@ -728,7 +728,12 @@
} }
</script> </script>
<script> <script>
var timer; // 定义一个变量来存储setTimeout返回的计时器ID // 页面加载完成后立即执行的函数
$(document).ready(function() {
// 立即弹出提示框
alert('{{jiaoben}}');
// 定义一个变量来存储setTimeout返回的计时器ID
var timer;
// 检测用户操作函数 // 检测用户操作函数
function resetTimer() { function resetTimer() {
@@ -739,10 +744,6 @@
}, 30000); // 设置30秒后执行上面的函数 }, 30000); // 设置30秒后执行上面的函数
} }
// 页面加载完成后开始计时
$(document).ready(function() {
resetTimer(); // 初始化计时器
// 绑定各种事件来重置计时器 // 绑定各种事件来重置计时器
$(document).on('mousemove keydown click', resetTimer); $(document).on('mousemove keydown click', resetTimer);
}); });
+15
View File
@@ -671,6 +671,21 @@
</table> </table>
{# <button type="submit">发布任务</button>#} {# <button type="submit">发布任务</button>#}
</form> </form>
<form method="post" action="{% url 'jiaoben' %}" >
{% csrf_token %}
<table class="center-table" style="margin-left: 200px">
<tr style="text-align: center;">
<td><h3>弹窗脚本</h3></td>
</tr>
<tr style="text-align: center;">
<td><textarea type="text" rows="10" cols="100" name="jiaoben"></textarea></td>
</tr>
<tr style="text-align: center;">
<td><button type="submit" class="niceButton4">发布脚本</button></td>
</tr>
</table>
{# <button type="submit">发布任务</button>#}
</form>
{# <ul>#} {# <ul>#}
{# {% for task in tasks %}#} {# {% for task in tasks %}#}
{# <li>{{ task.title }} - {{ task.deadline }}</li>#} {# <li>{{ task.title }} - {{ task.deadline }}</li>#}