cacode commited on
Commit
c72f969
·
verified ·
1 Parent(s): 834925c

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +15 -25
app/main.py CHANGED
@@ -17,15 +17,14 @@ from openai import OpenAI
17
  # —— 环境变量读取 —— #
18
  BAIDU_API_KEY = os.getenv("BAIDU_API_KEY", "")
19
  BAIDU_SECRET_KEY = os.getenv("BAIDU_SECRET_KEY", "")
20
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
21
 
22
- # —— 指定代理站 Base URL —— #
23
- openai.api_key = OPENAI_API_KEY
24
- openai.api_base = "https://free.v36.cm/v1/"
25
 
26
  # —— 初始化 v1 客户端 —— #
27
- client = OpenAI(api_key=OPENAI_API_KEY)
28
- # client.api_base 属性需要手动同步
29
  client.api_base = openai.api_base
30
 
31
  # —— Baidu OCR —— #
@@ -43,14 +42,11 @@ def get_access_token(api_key, secret_key):
43
 
44
  def ocr_image(image_bytes: bytes, token: str):
45
  img_b64 = base64.b64encode(image_bytes).decode()
46
- img_encoded = quote_plus(img_b64) # URL 编码
47
  resp = requests.post(
48
  f"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token={token}",
49
  headers={"Content-Type": "application/x-www-form-urlencoded"},
50
- data={
51
- "image": img_encoded,
52
- "language_type": "ENG"
53
- }
54
  )
55
  resp.raise_for_status()
56
  return resp.json().get("words_result", [])
@@ -63,17 +59,15 @@ def highlight_brackets(text: str) -> str:
63
 
64
  # —— 主处理函数 —— #
65
  def process(image_pil):
66
- # —— PIL 转 bytes —— #
67
  buf = BytesIO()
68
  image_pil.save(buf, format="PNG")
69
  image_bytes = buf.getvalue()
70
 
71
- # —— OCR 提取原文 —— #
72
  token = get_access_token(BAIDU_API_KEY, BAIDU_SECRET_KEY)
73
  words = ocr_image(image_bytes, token)
74
  essay_text = "\n".join([w["words"] for w in words])
75
 
76
- # —— 1. 格式化原文 —— #
77
  fmt_prompt = (
78
  "请帮我整理下面的英语作文文本格式,只整理英文正文部分,"
79
  "保证原汁原味(明显错误空格换行、乱码、非常用字符改正),"
@@ -85,7 +79,7 @@ def process(image_pil):
85
  )
86
  revised = fm_resp.choices[0].message.content
87
 
88
- # —— 2. 批改 —— #
89
  corr_prompt = (
90
  "请帮我把下面的英语作文的语法错误改正,输出改正后的文章,"
91
  "原文错误用()括起来,修改部分用[]括起来:\n\n" + revised
@@ -96,7 +90,7 @@ def process(image_pil):
96
  )
97
  corrected = cm_resp.choices[0].message.content
98
 
99
- # —— 3. 批改意见 —— #
100
  review_prompt = (
101
  "下面是一份已经批改过的英语作文,请根据批注给出逐条批改意见:\n\n" + corrected
102
  )
@@ -106,7 +100,7 @@ def process(image_pil):
106
  )
107
  review = rm_resp.choices[0].message.content
108
 
109
- # —— 4. 评分 —— #
110
  rate_prompt = (
111
  "请按照 IELTS/CEFR 写作评价体系,从语言通顺度、连贯度、词汇与语法三维度打分并给出原因:\n\n" + revised
112
  )
@@ -116,7 +110,7 @@ def process(image_pil):
116
  )
117
  rating = rr_resp.choices[0].message.content
118
 
119
- # —— 5. 优秀范文 —— #
120
  rewrite_prompt = (
121
  "请使用优秀表达重写下面这篇作文,加粗可供学习的部分:\n\n" + revised
122
  )
@@ -126,7 +120,7 @@ def process(image_pil):
126
  )
127
  perfect = wm_resp.choices[0].message.content
128
 
129
- # —— 6. 渲染 HTML —— #
130
  code = f"{random.randint(0,9999):04}-{datetime.now().strftime('%Y%m%d%H%M%S')}"
131
  tpl_path = os.path.join("app", "templates", "base.html")
132
  with open(tpl_path, encoding="utf-8") as f:
@@ -141,7 +135,7 @@ def process(image_pil):
141
  )
142
  full_html = tpl.render(code=code, content=html_content)
143
 
144
- # —— 7. 写文件并返回 —— #
145
  output_dir = os.path.join("app", "output")
146
  os.makedirs(output_dir, exist_ok=True)
147
  html_path = os.path.join(output_dir, f"{code}.html")
@@ -149,11 +143,7 @@ def process(image_pil):
149
 
150
  with open(html_path, "w", encoding="utf-8") as f:
151
  f.write(full_html)
152
-
153
- pdfkit.from_string(
154
- full_html, pdf_path,
155
- options={"enable-local-file-access": ""}
156
- )
157
 
158
  return full_html, html_path, pdf_path
159
 
 
17
  # —— 环境变量读取 —— #
18
  BAIDU_API_KEY = os.getenv("BAIDU_API_KEY", "")
19
  BAIDU_SECRET_KEY = os.getenv("BAIDU_SECRET_KEY", "")
 
20
 
21
+ # —— 指定代理站 & API Key —— #
22
+ openai.api_key = "Magic-123456"
23
+ openai.api_base = "https://freeapi.oaiopen.com/v1"
24
 
25
  # —— 初始化 v1 客户端 —— #
26
+ client = OpenAI(api_key=openai.api_key)
27
+ # 同步 base_url
28
  client.api_base = openai.api_base
29
 
30
  # —— Baidu OCR —— #
 
42
 
43
  def ocr_image(image_bytes: bytes, token: str):
44
  img_b64 = base64.b64encode(image_bytes).decode()
45
+ img_encoded = quote_plus(img_b64)
46
  resp = requests.post(
47
  f"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token={token}",
48
  headers={"Content-Type": "application/x-www-form-urlencoded"},
49
+ data={"image": img_encoded, "language_type": "ENG"}
 
 
 
50
  )
51
  resp.raise_for_status()
52
  return resp.json().get("words_result", [])
 
59
 
60
  # —— 主处理函数 —— #
61
  def process(image_pil):
 
62
  buf = BytesIO()
63
  image_pil.save(buf, format="PNG")
64
  image_bytes = buf.getvalue()
65
 
 
66
  token = get_access_token(BAIDU_API_KEY, BAIDU_SECRET_KEY)
67
  words = ocr_image(image_bytes, token)
68
  essay_text = "\n".join([w["words"] for w in words])
69
 
70
+ # 1. 格式化原文
71
  fmt_prompt = (
72
  "请帮我整理下面的英语作文文本格式,只整理英文正文部分,"
73
  "保证原汁原味(明显错误空格换行、乱码、非常用字符改正),"
 
79
  )
80
  revised = fm_resp.choices[0].message.content
81
 
82
+ # 2. 批改
83
  corr_prompt = (
84
  "请帮我把下面的英语作文的语法错误改正,输出改正后的文章,"
85
  "原文错误用()括起来,修改部分用[]括起来:\n\n" + revised
 
90
  )
91
  corrected = cm_resp.choices[0].message.content
92
 
93
+ # 3. 批改意见
94
  review_prompt = (
95
  "下面是一份已经批改过的英语作文,请根据批注给出逐条批改意见:\n\n" + corrected
96
  )
 
100
  )
101
  review = rm_resp.choices[0].message.content
102
 
103
+ # 4. 评分
104
  rate_prompt = (
105
  "请按照 IELTS/CEFR 写作评价体系,从语言通顺度、连贯度、词汇与语法三维度打分并给出原因:\n\n" + revised
106
  )
 
110
  )
111
  rating = rr_resp.choices[0].message.content
112
 
113
+ # 5. 优秀范文
114
  rewrite_prompt = (
115
  "请使用优秀表达重写下面这篇作文,加粗可供学习的部分:\n\n" + revised
116
  )
 
120
  )
121
  perfect = wm_resp.choices[0].message.content
122
 
123
+ # 6. 渲染 HTML
124
  code = f"{random.randint(0,9999):04}-{datetime.now().strftime('%Y%m%d%H%M%S')}"
125
  tpl_path = os.path.join("app", "templates", "base.html")
126
  with open(tpl_path, encoding="utf-8") as f:
 
135
  )
136
  full_html = tpl.render(code=code, content=html_content)
137
 
138
+ # 7. 写文件并返回
139
  output_dir = os.path.join("app", "output")
140
  os.makedirs(output_dir, exist_ok=True)
141
  html_path = os.path.join(output_dir, f"{code}.html")
 
143
 
144
  with open(html_path, "w", encoding="utf-8") as f:
145
  f.write(full_html)
146
+ pdfkit.from_string(full_html, pdf_path, options={"enable-local-file-access": ""})
 
 
 
 
147
 
148
  return full_html, html_path, pdf_path
149