diagnose-backend/diagnose/openai_gen.py

116 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
from openai import OpenAI
import markdown2
import pdfkit
from django.conf import settings
from celery import shared_task
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from .models import Papers
# ============== PYdoc ===================
# from docx import Document
# import pypandoc
# pypandoc.download_pandoc()
# ================ weasyprint ===========
# 库用不了
# from weasyprint import HTML
# HTML(string=html).write_pdf("output.pdf")
def generate_html_paper(md_result, file_path_name):
# 写到md中
md_file = open(f'{file_path_name}.md', 'w', encoding='utf-8')
md_file.write(md_result)
md_file.close()
# 从md中读出来再写到html中
md_read_file = open(f'{file_path_name}.md', 'r', encoding='utf-8')
md_text = md_read_file.read()
md_read_file.close()
html = markdown2.markdown(md_text)
html_write_file = open(f'{file_path_name}.html', 'w', encoding='utf-8')
html_write_file.write(
"""<head>
<meta charset="utf-8">
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
</head>"""+html)
html_write_file.close()
def convert_to_pdf(file_path_name):
# =============== pdfkit ================
# 从html中读出来再写到pdf中
f = open(f'{file_path_name}.html', 'r', encoding='utf-8')
content = f.read()
f.close()
config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)
pdfkit.from_string(content, f'{file_path_name}.pdf', configuration=config)
@shared_task
def paper_gen_async_wraper(file_path_name, uuid_str, subject, textbook_version, start_grade, \
start_semester, start_chapter, end_grade, end_semester, end_chapter, student):
# client = OpenAI(
# api_key = settings.OPAI_API_KEY,
# base_url = settings.OPAI_BASE_URL,
# )
# completion = client.chat.completions.create(
# model = "deepseek-r1-250120", # your model endpoint ID
# messages = [
# {"role": "system", "content": "你是教学助手"},
# # {"role": "user",
# # "content": f"请根据以下信息生成一份检测卷:学科为{subject},教材版本是{textbook_version},\
# # 检测范围从{start_grade}年级{start_semester}学期{start_chapter}章\
# # 到{end_grade}年级{end_semester}学期{end_chapter}章,学生年级为{student.get('grade')},\
# # 年龄为{student.get('age')},所在学校是{student.get('school')}。\
# # 根据答题所需时间一个半小时设置题量。题目有梯度。\
# # 试卷题目为{subject}{textbook_version}检测卷。只生成题目,不输出答案及说明等其他内容。"
# # },
# {"role": "user", "content": f"""请严格按照以下要求生成一份{subject}检测卷:
# 【试卷信息】
# 1. 标题:**{subject}{textbook_version}综合检测卷**
# 2. 检测范围:{start_grade}年级{start_semester}学期第{start_chapter}章 至
# {end_grade}年级{end_semester}学期第{end_chapter}章
# 3. 适用对象:{student.get('school')} {student.get('grade')}年级学生(年龄{student.get('age')}
# 【命题要求】
# 1. 题型结构:
# - 按难度梯度分为基础题60%、中档题30%、提高题10%
# - 根据学科特点设计合理的题型(如数学可设置选择题/填空题/计算题/应用题,语文可设置阅读理解/文言文/写作等)
# - 同一知识点不重复考查
# 2. 题量控制:
# - 总题数控制在18-22题以优秀学生完成时间约75分钟为标准
# - 选择题不超过5题填空题不超过6题
# - 需包含至少1道综合应用题理科或材料分析题文科
# 3. 内容要求:
# - 严格按照指定章节范围命题
# - 题目表述清晰无歧义,数字单位标注完整
# - 禁止出现需图片辅助的题目
# - 数学类题目需给出必要公式位置(如"提示可用公式____"
# 【输出格式】
# 仅输出以下内容:
# 1. 试卷标题(加粗居中)
# 2. 考试说明含总分值100分、考试时长、姓名班级填写处
# 3. 分模块的题目内容(标注题型、分值和题号)
# 4. "——以下为题目区域——" 分隔线
# 禁止包含:
# - 答案及解析
# - 评分标准
# - 额外说明文字
# - markdown格式及特殊符号"""}
# ],
# )
# generate_html_paper(completion.choices[0].message.content, file_path_name)
import time
time.sleep(30)
generate_html_paper(""" # Test md""", file_path_name)
try:
obj = Papers.objects.get(uuid=uuid_str) # 只获取一个对象
obj.status = 'D'
obj.updated_at = timezone.now()
obj.save()
except ObjectDoesNotExist:
print("warning: record not found")