-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor-a.py
More file actions
86 lines (76 loc) · 3.27 KB
/
Copy pathmonitor-a.py
File metadata and controls
86 lines (76 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import json
import shelve
from flask import Flask, render_template, request, jsonify
from openai import OpenAI
app = Flask(__name__)
# Configuration
INPUT_FILE = "001-process-document-for-candidates-output.json"
SHELF_FILE = "processing_progress.shelf"
# Initialize OpenAI client
client = OpenAI(base_url="http://localhost:1234/v1", api_key="not-needed")
def get_ai_response(prompt, line_content):
try:
completion = client.chat.completions.create(
model="local-model",
messages=prompt + [{"role": "user", "content": line_content}],
temperature=0.1,
max_tokens=(len(line_content) + 20),
top_p=0.95,
)
return completion.choices[0].message.content
except Exception as e:
print(f"Error getting AI response: {e}")
return None
@app.route('/')
def index():
with open(INPUT_FILE, 'r') as f:
data = json.load(f)
with shelve.open(SHELF_FILE) as shelf:
processed = [str(item['line_number']) in shelf for item in data]
return render_template('index.html', items=zip(data, processed))
@app.route('/item/<int:line_number>')
def item_detail(line_number):
with open(INPUT_FILE, 'r') as f:
data = json.load(f)
original_item = next((item for item in data if item['line_number'] == line_number), None)
with shelve.open(SHELF_FILE) as shelf:
processed_item = shelf.get(str(line_number))
return render_template('item_detail.html', original=original_item, processed=processed_item)
@app.route('/update', methods=['POST'])
def update_item():
line_number = request.form['line_number']
corrected_content = request.form['corrected_content']
with shelve.open(SHELF_FILE, writeback=True) as shelf:
item = shelf[line_number]
item['corrected_content'] = corrected_content
shelf[line_number] = item
return jsonify(success=True)
@app.route('/delete', methods=['POST'])
def delete_item():
line_number = request.form['line_number']
with shelve.open(SHELF_FILE, writeback=True) as shelf:
if line_number in shelf:
del shelf[line_number]
return jsonify(success=True)
@app.route('/process', methods=['POST'])
def process_item():
line_number = request.form['line_number']
with open(INPUT_FILE, 'r') as f:
data = json.load(f)
item = next((item for item in data if str(item['line_number']) == line_number), None)
if item:
prompt = [
{
"role": "system",
"content": "You are a professional proofreader specializing in correcting text where words may be incorrectly joined together. Your task is to insert spaces and/or punctuation where needed to correct the text. Please provide only the corrected text in your response.",
}
]
corrected_content = get_ai_response(prompt, item['content'])
if corrected_content:
with shelve.open(SHELF_FILE, writeback=True) as shelf:
item['corrected_content'] = corrected_content
shelf[line_number] = item
return jsonify(success=True, corrected_content=corrected_content)
return jsonify(success=False)
if __name__ == '__main__':
app.run(debug=True)