Spaces:
Sleeping
Sleeping
def text_to_json(text):
Browse files
app.py
CHANGED
|
@@ -62,41 +62,22 @@ def process_image(image_url):
|
|
| 62 |
print("======image_to_text=====")
|
| 63 |
print(text)
|
| 64 |
print("========================")
|
| 65 |
-
|
| 66 |
-
question_json = json.loads(text_refine)
|
| 67 |
return text, question_json
|
| 68 |
|
| 69 |
def image_to_text(url):
|
| 70 |
user_prompt = """
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
rule: only show the json text format of the response
|
| 79 |
-
|
| 80 |
-
restrictions:
|
| 81 |
-
- dont show the word:'json' in response
|
| 82 |
-
- dont show the word:'```' in response
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
EXAMPLE:
|
| 86 |
-
{{
|
| 87 |
-
"q_id" : 1,
|
| 88 |
-
"question": .......,
|
| 89 |
-
"choice_1": ....,
|
| 90 |
-
"choice_2": .... ,
|
| 91 |
-
"choice_3": ....,
|
| 92 |
-
"choice_4": ....
|
| 93 |
-
}}
|
| 94 |
"""
|
| 95 |
|
| 96 |
-
response_format = { "type": "json_object" }
|
| 97 |
response = OPEN_AI_CLIENT.chat.completions.create(
|
| 98 |
-
|
| 99 |
-
|
| 100 |
{
|
| 101 |
"role": "user",
|
| 102 |
"content": [
|
|
@@ -111,45 +92,42 @@ def image_to_text(url):
|
|
| 111 |
},
|
| 112 |
},
|
| 113 |
],
|
| 114 |
-
"response_format": response_format
|
| 115 |
}
|
| 116 |
],
|
| 117 |
-
max_tokens=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
)
|
| 119 |
-
text = response.choices[0].message.content
|
| 120 |
-
return text
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# 你是專業的轉譯器,看得懂題目,並保留 LATEX 語法($$)
|
| 126 |
-
# """
|
| 127 |
-
# user_prompt = """
|
| 128 |
-
# 將以內容轉成 json,並保留 latex 語法($$),請一定要用 LATEX markdown 語法(前後用 $ 包起來的形式),包含 question 跟 choice 1~4
|
| 129 |
-
# {
|
| 130 |
-
# "q_id" : 1,
|
| 131 |
-
# "question": .......,
|
| 132 |
-
# "choice_1": ....,
|
| 133 |
-
# "choice_2": .... ,
|
| 134 |
-
# "choice_3": ....,
|
| 135 |
-
# "choice_4": ....
|
| 136 |
-
# }
|
| 137 |
-
# ---
|
| 138 |
-
# """
|
| 139 |
-
# user_prompt += text
|
| 140 |
-
|
| 141 |
-
# response_to_json = OPEN_AI_CLIENT.chat.completions.create(
|
| 142 |
-
# model="gpt-4o",
|
| 143 |
-
# response_format={ "type": "json_object" },
|
| 144 |
-
# messages=[
|
| 145 |
-
# {"role": "system", "content": system_prompt},
|
| 146 |
-
# {"role": "user", "content": user_prompt}
|
| 147 |
-
# ],
|
| 148 |
-
# max_tokens=1000,
|
| 149 |
-
# )
|
| 150 |
-
|
| 151 |
-
# result = response_to_json.choices[0].message.content
|
| 152 |
-
# return result
|
| 153 |
|
| 154 |
def build_perseus_json(question_json):
|
| 155 |
question = question_json['question']
|
|
|
|
| 62 |
print("======image_to_text=====")
|
| 63 |
print(text)
|
| 64 |
print("========================")
|
| 65 |
+
question_json = json.loads(text_to_json(text))
|
|
|
|
| 66 |
return text, question_json
|
| 67 |
|
| 68 |
def image_to_text(url):
|
| 69 |
user_prompt = """
|
| 70 |
+
請解讀數學題目圖片,圖片請用 zh-TW 解讀
|
| 71 |
+
[數學用語、題目內的數字、選項上的數字、 數學符號、英文單字] 請一定要用 LATEX markdown 語法(前後用 $ 包起來),LATEX 這很重要
|
| 72 |
+
輸出為
|
| 73 |
+
1. 題號:
|
| 74 |
+
2. 題目:
|
| 75 |
+
3. 選項:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
"""
|
| 77 |
|
|
|
|
| 78 |
response = OPEN_AI_CLIENT.chat.completions.create(
|
| 79 |
+
model="gpt-4o",
|
| 80 |
+
messages=[
|
| 81 |
{
|
| 82 |
"role": "user",
|
| 83 |
"content": [
|
|
|
|
| 92 |
},
|
| 93 |
},
|
| 94 |
],
|
|
|
|
| 95 |
}
|
| 96 |
],
|
| 97 |
+
max_tokens=300,
|
| 98 |
+
)
|
| 99 |
+
return response.choices[0].message.content
|
| 100 |
+
|
| 101 |
+
def text_to_json(text):
|
| 102 |
+
system_prompt = """
|
| 103 |
+
你是專業的轉譯器,看得懂題目,並保留 LATEX 語法($$)
|
| 104 |
+
"""
|
| 105 |
+
user_prompt = """
|
| 106 |
+
將以內容轉成 json,並保留 latex 語法($$),請一定要用 LATEX markdown 語法(前後用 $ 包起來的形式),包含 question 跟 choice 1~4
|
| 107 |
+
{
|
| 108 |
+
"q_id" : 1,
|
| 109 |
+
"question": .......,
|
| 110 |
+
"choice_1": ....,
|
| 111 |
+
"choice_2": .... ,
|
| 112 |
+
"choice_3": ....,
|
| 113 |
+
"choice_4": ....
|
| 114 |
+
}
|
| 115 |
+
---
|
| 116 |
+
"""
|
| 117 |
+
user_prompt += text
|
| 118 |
+
|
| 119 |
+
response_to_json = OPEN_AI_CLIENT.chat.completions.create(
|
| 120 |
+
model="gpt-4o",
|
| 121 |
+
response_format={ "type": "json_object" },
|
| 122 |
+
messages=[
|
| 123 |
+
{"role": "system", "content": system_prompt},
|
| 124 |
+
{"role": "user", "content": user_prompt}
|
| 125 |
+
],
|
| 126 |
+
max_tokens=1000,
|
| 127 |
)
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
result = response_to_json.choices[0].message.content
|
| 130 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
def build_perseus_json(question_json):
|
| 133 |
question = question_json['question']
|