forked from ligi2009/pick_hbdb_prompt_exampls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
317 lines (273 loc) · 13.7 KB
/
app.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from flask import Flask, render_template, request, jsonify, send_file
from flask_bootstrap import Bootstrap5
from io import BytesIO
import os
import json
import mysql.connector
from pathlib import Path
from dotenv import load_dotenv
import zipfile
from datetime import datetime
# Load environment variables
load_dotenv()
app = Flask(__name__)
bootstrap = Bootstrap5(app)
def process_compound_data(compound_id):
connection = mysql.connector.connect(
host=os.getenv('MYSQL_HOST'),
port=os.getenv('MYSQL_PORT'),
user=os.getenv('MYSQL_USER'),
password=os.getenv('MYSQL_ROOT_PASSWORD')
)
cursor = connection.cursor()
cursor.execute("USE `hbdb2`;")
# Get compound name
cursor.execute(f"SELECT `name` FROM `compounds` WHERE `id`={compound_id};")
result = cursor.fetchone()
if not result:
return None
compound_name = result[0]
# Define tables as in original sql.py
tables_dir_name = ['concept_abnormality_metadata', 'concept_chemical_metadata',
'concept_molecular function_metadata', 'concept_gene_metadata',
'concept_location_metadata', 'concept_animal model_metadata']
tables = ['concept_abnorm_metadata', 'concept_chemical_metadata',
'concept_function_metadata', 'concept_gene_metadata',
'concept_location_metadata', 'concept_model_metadata']
results = []
for idx, table in enumerate(tables):
cursor.execute(f"SELECT * FROM {table} WHERE `compound_id`={compound_id} AND `is_related`=1;")
ret = cursor.fetchall()
for i in ret:
concept_id = i[2]
if table == 'concept_abnorm_metadata':
reference_id = i[7]
paragraph = i[5]
sentence = i[6]
else:
reference_id = i[3]
paragraph = i[6]
sentence = i[7]
cursor.execute(f"SELECT `concept_name` FROM `concepts` WHERE `id`={concept_id};")
term_b = cursor.fetchall()[0][0]
# Get context sentences
# Escape single quotes in the sentence
escaped_sentence = sentence.replace("'", "''")
cursor.execute(f"""SELECT *
FROM `sentences`
WHERE `content`
LIKE %s AND `reference_id`=%s;""", (escaped_sentence, reference_id))
k = cursor.fetchall()
if k:
start_idx = k[0][0] - 3
end_idx = k[0][0] + 3
cursor.execute(f"SELECT `content` FROM `sentences` WHERE `id` BETWEEN %s AND %s;",
(start_idx, end_idx))
kk = cursor.fetchall()
context = " ".join(j[0] for j in kk)
# Store result
result_data = {
"term_A": compound_name,
"term_B": term_b,
"context": context,
"category": tables_dir_name[idx],
"score": None, # Default score
"verified": False, # Default verification status
"table": table,
"compound_id": compound_id,
"concept_id": concept_id,
"reference_id": reference_id,
"paragraph": paragraph
}
# Save to file
save_path = f"{compound_name}/{tables_dir_name[idx]}/{reference_id}"
os.makedirs(save_path, exist_ok=True)
file_path = f"{save_path}/{compound_name}_{term_b}_{paragraph}.json"
with open(file_path, "w") as file:
json.dump(result_data, file, indent=4)
results.append(result_data)
cursor.close()
connection.close()
return {
'compound_name': compound_name,
'results': results
}
@app.route('/', methods=['GET', 'POST'])
def index():
data = None
error = None
if request.method == 'POST':
try:
compound_id = int(request.form.get('compound_id'))
data = process_compound_data(compound_id)
if not data:
error = f"No compound found with ID {compound_id}"
except ValueError:
error = "Please enter a valid compound ID (number)"
except Exception as e:
error = f"An error occurred: {str(e)}"
return render_template('index.html', data=data, error=error)
@app.route('/update_score', methods=['POST'])
def update_score():
try:
data = request.json
compound_name = data['compound_name']
term_b = data['term_b']
category = data['category']
reference_id = data['reference_id']
paragraph = data['paragraph']
score = data['score']
verified = data['verified']
# Construct the file path
file_path = f"{compound_name}/{category}/{reference_id}/{compound_name}_{term_b}_{paragraph}.json"
# Read existing JSON
with open(file_path, 'r') as file:
json_data = json.load(file)
# Update score and verification
json_data['score'] = score
json_data['verified'] = verified
# Save updated JSON
with open(file_path, 'w') as file:
json.dump(json_data, file, indent=4)
return jsonify({"success": True})
except Exception as e:
return jsonify({"success": False, "error": str(e)})
@app.route('/list_files')
def list_files():
try:
compounds = [d for d in os.listdir('.') if os.path.isdir(d) and not d.startswith('.')]
all_files = []
sections = set()
terms_a = set()
verified_count = 0
scored_count = 0
score_counts = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
for compound in compounds:
for category in os.listdir(compound):
category_path = os.path.join(compound, category)
if os.path.isdir(category_path):
for ref_id in os.listdir(category_path):
ref_path = os.path.join(category_path, ref_id)
if os.path.isdir(ref_path):
for file in os.listdir(ref_path):
if file.endswith('.json'):
file_path = os.path.join(ref_path, file)
with open(file_path, 'r') as f:
data = json.load(f)
score = data.get('score')
if score:
try:
score = int(score)
scored_count += 1
score_counts[score] += 1
except (ValueError, TypeError):
print(f"Invalid score in file {file_path}: {score}")
# Check verified status explicitly as boolean
if data.get('verified') is True:
verified_count += 1
paragraph_name = file.split('_')[-1].replace('.json', '')
sections.add(paragraph_name)
terms_a.add(data.get('term_A', ''))
file_info = {
'path': file_path,
'term_A': data.get('term_A', ''),
'term_B': data.get('term_B', ''),
'context': data.get('context', ''),
'score': score if score else None,
'verified': data.get('verified') is True,
'category': category,
'reference_id': ref_id,
'paragraph': paragraph_name
}
all_files.append(file_info)
# Print debug information
print(f"Total files: {len(all_files)}")
print(f"Scored files: {scored_count}")
print(f"Verified files: {verified_count}")
print("Score distribution:", score_counts)
return render_template('list_files.html',
files=all_files,
sections=sorted(sections),
terms_a=sorted(terms_a),
verified_count=verified_count,
scored_count=scored_count,
total_count=len(all_files),
score_counts=score_counts)
except Exception as e:
print(f"Error in list_files: {str(e)}")
return jsonify({"error": str(e)}), 500
@app.route('/needs_verification', methods=['GET'])
def needs_verification():
try:
# Get all compound directories
compounds = [d for d in os.listdir('.') if os.path.isdir(d) and not d.startswith('.')]
files_to_verify = []
for compound in compounds:
for category in os.listdir(compound):
category_path = os.path.join(compound, category)
if os.path.isdir(category_path):
for ref_id in os.listdir(category_path):
ref_path = os.path.join(category_path, ref_id)
if os.path.isdir(ref_path):
for file in os.listdir(ref_path):
if file.endswith('.json'):
file_path = os.path.join(ref_path, file)
with open(file_path, 'r') as f:
data = json.load(f)
# Check if file has score but isn't verified
if data.get('score') and not data.get('verified', False):
paragraph_name = file.split('_')[-1].replace('.json', '')
file_info = {
'path': file_path,
'compound': compound,
'category': category,
'reference_id': ref_id,
'term_A': data.get('term_A', ''),
'term_B': data.get('term_B', ''),
'context': data.get('context', ''),
'score': int(data.get('score')) if data.get('score') else None,
'verified': data.get('verified', False),
'paragraph': paragraph_name
}
files_to_verify.append(file_info)
return render_template('needs_verification.html', files=files_to_verify)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/download_verified', methods=['GET'])
def download_verified():
try:
# Create a BytesIO object to store the zip file
memory_file = BytesIO()
# Create a ZipFile object
with zipfile.ZipFile(memory_file, 'w', zipfile.ZIP_DEFLATED) as zf:
compounds = [d for d in os.listdir('.') if os.path.isdir(d) and not d.startswith('.')]
for compound in compounds:
for category in os.listdir(compound):
category_path = os.path.join(compound, category)
if os.path.isdir(category_path):
for ref_id in os.listdir(category_path):
ref_path = os.path.join(category_path, ref_id)
if os.path.isdir(ref_path):
for file in os.listdir(ref_path):
if file.endswith('.json'):
file_path = os.path.join(ref_path, file)
with open(file_path, 'r') as f:
data = json.load(f)
# Check if file has score and is verified
if data.get('score') and data.get('verified', False):
# Add file to zip with its relative path
zf.write(file_path, os.path.join(compound, category, ref_id, file))
# Reset the file pointer
memory_file.seek(0)
# Generate timestamp for filename
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
return send_file(
memory_file,
mimetype='application/zip',
as_attachment=True,
download_name=f'verified_files_{timestamp}.zip'
)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5020, debug=True)