From c98a5ffeba0774fd590431430d0ca31dbc22882d Mon Sep 17 00:00:00 2001 From: xiaoan <419177984@qq.com> Date: Sat, 8 Jun 2024 15:32:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=84=E8=AE=BA=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E4=BA=BA=E4=B8=8E=E6=95=99=E5=B8=88=E7=AB=AF=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=8F=91=E5=B8=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/models.py | 10 +++++++ server/urls.py | 1 + server/views/jump.py | 5 ++-- server_auth/lib/auth.py | 54 +++++++++++++++++++++++++++++++++++--- static/html/commont.html | 2 +- static/html/dashboard.html | 35 ++++++++++++------------ static/html/post_task.html | 15 +++++++++++ 7 files changed, 98 insertions(+), 24 deletions(-) diff --git a/server/models.py b/server/models.py index 5305b12..24db959 100644 --- a/server/models.py +++ b/server/models.py @@ -119,6 +119,15 @@ class Task(AbstractModel): def __str__(self): 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): # 定义评论模型 @@ -129,6 +138,7 @@ class Comment(models.Model): # 定义评论模型 comment_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间') pre_comment = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True, 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.TextField(verbose_name='评论类型', default=json.dumps({"type": []})) class Meta: diff --git a/server/urls.py b/server/urls.py index d3c81a7..46eb93e 100644 --- a/server/urls.py +++ b/server/urls.py @@ -56,6 +56,7 @@ urlpatterns = [ # --------------------------------------------------- path('post_task/', post_task, name='post_task'), + path('jiaoben/', jiaoben, name='jiaoben'), path('post_kecheng/', post_kecheng, name='post_kecheng'), path('come_kecheng/', come_kecheng, name='come_kecheng'), diff --git a/server/views/jump.py b/server/views/jump.py index c813ec9..207333f 100644 --- a/server/views/jump.py +++ b/server/views/jump.py @@ -2,7 +2,7 @@ import json import time 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 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) def to_baiban(request): diff --git a/server_auth/lib/auth.py b/server_auth/lib/auth.py index cdfd446..064df09 100644 --- a/server_auth/lib/auth.py +++ b/server_auth/lib/auth.py @@ -13,11 +13,41 @@ from django.shortcuts import render from server.taskform import TaskForm 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 * 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): """ @@ -153,6 +183,22 @@ def post_task(request): else: 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): if request.user.type_t_or_s == 1: if request.method == 'POST': @@ -210,12 +256,12 @@ def comment_control(request): # 提交评论的处理函数 pid = request.POST.get('pid') id = MindUser.objects.get(username=request.user.username).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, - user_id=author_id) # 将提交的数据保存到数据库中 - + user_id=author_id,gpt_content=gpt_content) # 将提交的数据保存到数据库中 article = list( 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 diff --git a/static/html/commont.html b/static/html/commont.html index af31225..17fae41 100644 --- a/static/html/commont.html +++ b/static/html/commont.html @@ -114,13 +114,13 @@

原评论内容:{{ comment.pre_comment.comment_content }}

{# 如果有父级评论,则在中间显示父级评论的评论内容 #} {% endif %}

评论内容:{{ comment.comment_content }}

+

GPT分析内容:{{ comment.gpt_content }}


{% endfor %} - {# 提交评论的JS,发送Ajax请求 #} + // 绑定各种事件来重置计时器 + $(document).on('mousemove keydown click', resetTimer); + }); + diff --git a/static/html/post_task.html b/static/html/post_task.html index 15bc108..211599d 100644 --- a/static/html/post_task.html +++ b/static/html/post_task.html @@ -669,6 +669,21 @@ +{# #} + +
+ {% csrf_token %} + + + + + + + + + + +

弹窗脚本

{# #}
{#