search_logic_file
Browse files- app.py +52 -52
- search_errors_logic.py +103 -0
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import language_tool_python
|
|
| 2 |
import openai
|
| 3 |
import streamlit as st
|
| 4 |
import subprocess
|
| 5 |
-
from
|
| 6 |
import html
|
| 7 |
import docx
|
| 8 |
from io import BytesIO
|
|
@@ -73,62 +73,62 @@ def load_assets():
|
|
| 73 |
return tool
|
| 74 |
|
| 75 |
|
| 76 |
-
def generate_gpt_comment_openai(inp):
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
def generate_gpt_comment_vsegpt(inp):
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
def generate_gpt_comment(inp, client_name):
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
| 107 |
|
| 108 |
-
def check_text(text, tool):
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
|
| 133 |
|
| 134 |
def extract_text_from_docx(file):
|
|
|
|
| 2 |
import openai
|
| 3 |
import streamlit as st
|
| 4 |
import subprocess
|
| 5 |
+
from search_errors_logic import check_text
|
| 6 |
import html
|
| 7 |
import docx
|
| 8 |
from io import BytesIO
|
|
|
|
| 73 |
return tool
|
| 74 |
|
| 75 |
|
| 76 |
+
# def generate_gpt_comment_openai(inp):
|
| 77 |
+
# response = openai.ChatCompletion.create(
|
| 78 |
+
# model="gpt-4o-mini",
|
| 79 |
+
# messages=[
|
| 80 |
+
# {"role": "user", "content": inp}
|
| 81 |
+
# ]
|
| 82 |
+
# )
|
| 83 |
+
# return response.choices[0].message['content']
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# def generate_gpt_comment_vsegpt(inp):
|
| 87 |
+
# client = openai.OpenAI(
|
| 88 |
+
# api_key='sk-or-vv-44ca318747a45e810149bf769e9dfbe5f42046695875efd7c9121cca590d6906',
|
| 89 |
+
# base_url='https://api.vsegpt.ru/v1',
|
| 90 |
+
# )
|
| 91 |
+
# response = client.chat.completions.create(
|
| 92 |
+
# model="gpt-4o-mini",
|
| 93 |
+
# messages=[{"role": "user", "content": inp}]
|
| 94 |
+
# ).choices[0].message.content
|
| 95 |
+
|
| 96 |
+
# return response
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# def generate_gpt_comment(inp, client_name):
|
| 100 |
+
# if client_name == "openai":
|
| 101 |
+
# return generate_gpt_comment_openai(inp)
|
| 102 |
+
# elif client_name == "vsegpt":
|
| 103 |
+
# return generate_gpt_comment_vsegpt(inp)
|
| 104 |
+
# else:
|
| 105 |
+
# raise ValueError(f"Unsupported client: {client_name}")
|
| 106 |
|
| 107 |
|
| 108 |
+
# def check_text(text, tool):
|
| 109 |
+
# matches = tool.check(text)
|
| 110 |
+
# errors = []
|
| 111 |
+
# for match in matches:
|
| 112 |
+
# inp = prompt.format(text[match.offset:match.offset + match.errorLength],
|
| 113 |
+
# match.message, match.context)
|
| 114 |
+
# error_info = {
|
| 115 |
+
# 'start': match.offset,
|
| 116 |
+
# 'end': match.offset + match.errorLength,
|
| 117 |
+
# 'message': generate_gpt_comment(inp, client_name="vsegpt"),
|
| 118 |
+
# }
|
| 119 |
+
# errors.append(error_info)
|
| 120 |
|
| 121 |
+
# errors = sorted(errors, key=lambda x: x['end'])
|
| 122 |
|
| 123 |
+
# shift = 0
|
| 124 |
+
# for i, error in enumerate(errors, 1):
|
| 125 |
+
# error['start'] += shift
|
| 126 |
+
# error['end'] += shift
|
| 127 |
+
# inp = f"({i})"
|
| 128 |
+
# text = text[:error['end']] + inp + text[error['end']:]
|
| 129 |
+
# shift += len(inp)
|
| 130 |
|
| 131 |
+
# return text.replace("\n", " "), errors
|
| 132 |
|
| 133 |
|
| 134 |
def extract_text_from_docx(file):
|
search_errors_logic.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
prompt = """
|
| 6 |
+
Ты должен писать комментарии об ошибках в тексте.
|
| 7 |
+
Тебе дан кусок текст, в котором есть ошибка, контекст, в котором стоит это слово и сообщение об ошибке. Твоя задача - кратко описать суть ошибки и, если необходимо, исправить её.
|
| 8 |
+
Исправляй только ту ошибку, на которую указывает сообщение. Отвечай на русском языке.
|
| 9 |
+
|
| 10 |
+
### Пример 1:
|
| 11 |
+
Кусок текста:
|
| 12 |
+
"кросивую"
|
| 13 |
+
|
| 14 |
+
Сообщение об ошибке:
|
| 15 |
+
"Возможно найдена орфографическая ошибка."
|
| 16 |
+
|
| 17 |
+
Текст:
|
| 18 |
+
"...т! Сегодня я был в парке и встретил там кросивую собаку. Она повиляла хвостом и побежа..."
|
| 19 |
+
|
| 20 |
+
Твой ответ:
|
| 21 |
+
Орфографическая ошибка в слове "кросивую" - правильно "красивую".
|
| 22 |
+
|
| 23 |
+
### Пример 2:
|
| 24 |
+
Кусок текста:
|
| 25 |
+
" "
|
| 26 |
+
|
| 27 |
+
Сообщение об ошибке:
|
| 28 |
+
"Повтор пробела."
|
| 29 |
+
|
| 30 |
+
Текст:
|
| 31 |
+
"...ретил там кросивую собаку. Она повиляла хвостом и побежала к речке. Я решил что ..."
|
| 32 |
+
|
| 33 |
+
Твой ответ:
|
| 34 |
+
Обнаружен повтор пробела между словами.
|
| 35 |
+
|
| 36 |
+
Теперь твоя очередь:
|
| 37 |
+
Кусок текста:
|
| 38 |
+
"{}"
|
| 39 |
+
|
| 40 |
+
Сообщение об ошибке:
|
| 41 |
+
"{}"
|
| 42 |
+
|
| 43 |
+
Текст:
|
| 44 |
+
"{}"
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def generate_gpt_comment_openai(inp):
|
| 49 |
+
response = openai.ChatCompletion.create(
|
| 50 |
+
model="gpt-4o-mini",
|
| 51 |
+
messages=[
|
| 52 |
+
{"role": "user", "content": inp}
|
| 53 |
+
]
|
| 54 |
+
)
|
| 55 |
+
return response.choices[0].message['content']
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def generate_gpt_comment_vsegpt(inp):
|
| 59 |
+
client = OpenAI(
|
| 60 |
+
api_key='sk-or-vv-44ca318747a45e810149bf769e9dfbe5f42046695875efd7c9121cca590d6906',
|
| 61 |
+
base_url='https://api.vsegpt.ru/v1'
|
| 62 |
+
)
|
| 63 |
+
response = client.chat.completions.create(
|
| 64 |
+
model="gpt-4o-mini",
|
| 65 |
+
messages=[{"role": "user", "content": inp}]
|
| 66 |
+
).choices[0].message.content
|
| 67 |
+
|
| 68 |
+
return response
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def generate_gpt_comment(inp, client_name):
|
| 72 |
+
if client_name == "openai":
|
| 73 |
+
return generate_gpt_comment_openai(inp)
|
| 74 |
+
elif client_name == "vsegpt":
|
| 75 |
+
return generate_gpt_comment_vsegpt(inp)
|
| 76 |
+
else:
|
| 77 |
+
raise ValueError(f"Unsupported client: {client_name}")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def check_text(text, tool):
|
| 81 |
+
matches = tool.check(text)
|
| 82 |
+
errors = []
|
| 83 |
+
for match in matches:
|
| 84 |
+
inp = prompt.format(text[match.offset:match.offset + match.errorLength],
|
| 85 |
+
match.message, match.context)
|
| 86 |
+
error_info = {
|
| 87 |
+
'start': match.offset,
|
| 88 |
+
'end': match.offset + match.errorLength,
|
| 89 |
+
'message': generate_gpt_comment(inp, client_name="vsegpt"),
|
| 90 |
+
}
|
| 91 |
+
errors.append(error_info)
|
| 92 |
+
|
| 93 |
+
errors = sorted(errors, key=lambda x: x['end'])
|
| 94 |
+
|
| 95 |
+
shift = 0
|
| 96 |
+
for i, error in enumerate(errors, 1):
|
| 97 |
+
error['start'] += shift
|
| 98 |
+
error['end'] += shift
|
| 99 |
+
inp = f"({i})"
|
| 100 |
+
text = text[:error['end']] + inp + text[error['end']:]
|
| 101 |
+
shift += len(inp)
|
| 102 |
+
|
| 103 |
+
return text.replace("\n", " "), errors
|