Spaces:
Sleeping
Sleeping
File size: 24,489 Bytes
102ae18 |
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
#!/usr/bin/env python3
"""
Flask server for POA Graph Web Interface
"""
import glob
import os
import pickle
import re
import sys
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
# Get the repository root directory (parent of web_interface)
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Add the repository root to the path so we can import the POA graph modules
sys.path.append(REPO_ROOT)
from src.new_text_alignment import TextSeqGraphAlignment
from src.text_poa_graph import TextPOAGraph
try:
from src.generation_methods import decode_consensus
except ImportError:
decode_consensus = None
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# Base paths for different datasets (relative to repo root)
GRAPH_PATHS = {
"bio": os.path.join(REPO_ROOT, "results/graphs/HALoGEN/bio"),
"fp": os.path.join(REPO_ROOT, "results/graphs/HALoGEN/fp"),
"hist": os.path.join(REPO_ROOT, "results/graphs/HALoGEN/hist"),
"refs": os.path.join(REPO_ROOT, "results/graphs/HALoGEN/refs"),
"math": os.path.join(REPO_ROOT, "results/graphs/HALoGEN/MATH"),
"aime": os.path.join(REPO_ROOT, "results/graphs/HALoGEN/AIME"),
}
MODELS = ["qwen72b", "qwen7b", "llama8b", "llama70b", "olmo7b", "olmo32b"]
@app.route("/")
def index():
"""Serve the main HTML file"""
return send_from_directory(".", "index.html")
@app.route("/api/datasets", methods=["GET"])
def get_datasets():
"""Get available datasets"""
datasets = []
for dataset_name, path in GRAPH_PATHS.items():
if os.path.exists(path):
# Count available graphs
pkl_files = glob.glob(os.path.join(path, "*.pkl"))
datasets.append(
{
"name": dataset_name,
"display_name": dataset_name.upper(),
"path": path,
"count": len(pkl_files),
}
)
return jsonify({"datasets": datasets})
@app.route("/api/models", methods=["GET"])
def get_models():
"""Get available models for a specific entity"""
entity = request.args.get("entity")
dataset = request.args.get("dataset")
if not entity:
return jsonify({"error": "Entity parameter required"}), 400
if not dataset or dataset not in GRAPH_PATHS:
return jsonify({"error": "Invalid dataset"}), 400
path = GRAPH_PATHS[dataset]
if not os.path.exists(path):
return jsonify({"error": "Dataset path not found"}), 404
models = []
pkl_files = glob.glob(os.path.join(path, "*.pkl"))
for pkl_file in pkl_files:
filename = os.path.basename(pkl_file)
# Different filename patterns for different datasets
if dataset == "bio":
# Format: bio_graph_{entity}_merged_{model}.pkl
match = re.match(r"bio_graph_(.+?)_merged_(\w+)\.pkl", filename)
if match:
entity_name, model = match.groups()
if entity_name == entity:
models.append({"model": model, "filename": filename, "filepath": pkl_file})
elif dataset == "fp":
# Format: fp_graph_{number}_merged_{model}.pkl
match = re.match(r"fp_graph_(\d+)_merged_(\w+)\.pkl", filename)
if match:
entity_name, model = match.groups()
if f"Problem {entity_name}" == entity:
models.append({"model": model, "filename": filename, "filepath": pkl_file})
elif dataset == "math":
# Format: qwen72_math_{number}.pkl
match = re.match(r"qwen72_math_(\d+)\.pkl", filename)
if match:
entity_name = match.group(1)
if f"Math Problem {entity_name}" == entity:
models.append({"model": "qwen72b", "filename": filename, "filepath": pkl_file})
elif dataset == "aime":
# Format: aime_qwen72b_{number}.pkl
match = re.match(r"aime_qwen72b_(\d+)\.pkl", filename)
if match:
entity_name = match.group(1)
if f"AIME Problem {entity_name}" == entity:
models.append({"model": "qwen72b", "filename": filename, "filepath": pkl_file})
else:
# Generic pattern for other datasets
match = re.match(r"(\w+)_graph_(.+?)_merged_(\w+)\.pkl", filename)
if match:
task, entity_name, model = match.groups()
if entity_name == entity:
models.append({"model": model, "filename": filename, "filepath": pkl_file})
return jsonify({"models": models})
@app.route("/api/entities", methods=["GET"])
def get_entities():
"""Get available entities for a dataset"""
dataset = request.args.get("dataset")
if not dataset or dataset not in GRAPH_PATHS:
return jsonify({"error": "Invalid dataset"}), 400
path = GRAPH_PATHS[dataset]
if not os.path.exists(path):
return jsonify({"error": "Dataset path not found"}), 404
entities = []
pkl_files = glob.glob(os.path.join(path, "*.pkl"))
for pkl_file in pkl_files:
filename = os.path.basename(pkl_file)
# Different filename patterns for different datasets
if dataset == "bio":
# Format: bio_graph_{entity}_merged_{model}.pkl
match = re.match(r"bio_graph_(.+?)_merged_(\w+)\.pkl", filename)
if match:
entity_name, model = match.groups()
entities.append(
{
"entity": entity_name,
"model": model,
"filename": filename,
"filepath": pkl_file,
}
)
elif dataset == "fp":
# Format: fp_graph_{number}_merged_{model}.pkl
match = re.match(r"fp_graph_(\d+)_merged_(\w+)\.pkl", filename)
if match:
entity_name, model = match.groups()
entities.append(
{
"entity": f"Problem {entity_name}",
"model": model,
"filename": filename,
"filepath": pkl_file,
}
)
elif dataset == "math":
# Format: qwen72_math_{number}.pkl
match = re.match(r"qwen72_math_(\d+)\.pkl", filename)
if match:
entity_name = match.group(1)
entities.append(
{
"entity": f"Math Problem {entity_name}",
"model": "qwen72b",
"filename": filename,
"filepath": pkl_file,
}
)
elif dataset == "aime":
# Format: aime_qwen72b_{number}.pkl
match = re.match(r"aime_qwen72b_(\d+)\.pkl", filename)
if match:
entity_name = match.group(1)
entities.append(
{
"entity": f"AIME Problem {entity_name}",
"model": "qwen72b",
"filename": filename,
"filepath": pkl_file,
}
)
else:
# Generic pattern for other datasets
match = re.match(r"(\w+)_graph_(.+?)_merged_(\w+)\.pkl", filename)
if match:
task, entity_name, model = match.groups()
entities.append(
{
"entity": entity_name,
"model": model,
"filename": filename,
"filepath": pkl_file,
}
)
return jsonify({"entities": entities})
@app.route("/api/load_existing_graph", methods=["POST"])
def load_existing_graph():
"""Load an existing graph from the stored pickle files"""
try:
data = request.get_json()
filepath = data.get("filepath")
if not filepath or not os.path.exists(filepath):
return jsonify({"error": "Graph file not found"}), 404
# Read and load the pickle file
try:
with open(filepath, "rb") as f:
graph = pickle.load(f)
except Exception as e:
return jsonify({"error": f"Error loading pickle file: {str(e)}"}), 500
if not isinstance(graph, TextPOAGraph):
return jsonify({"error": "File does not contain a valid POA graph"}), 400
# Convert to JSON format for vis.js
nodes = []
edges = []
try:
# Get consensus nodes for coloring
consensus_nodes = set(graph.consensus_node_ids)
# Create nodes using the same logic as jsOutput
for node in graph.nodeiterator()():
title_text = ""
if node.sequences:
title_text += f"Sequences: {node.sequences}"
if node.variations:
title_text += ";;;".join(
[f"{sequence_id}: {text}" for sequence_id, text in node.variations.items()]
)
title_text = title_text.replace('"', "'")
# Use the same color logic as jsOutput
color = "#ceeab2" if node.ID in consensus_nodes else "#cae0e6"
node_data = {
"id": node.ID,
"label": f"{node.ID}: {node.text}",
"title": title_text,
"color": color,
}
nodes.append(node_data)
# Create edges using the same logic as jsOutput
for node in graph.nodeiterator()():
nodeID = node.ID # Keep as integer
for edge in node.outEdges:
target = edge # Keep as integer
weight = node.outEdges[edge].weight + 1.5
edge_data = {
"from": nodeID,
"to": target,
"value": weight,
"color": "#cae0e6",
"arrows": "to",
}
edges.append(edge_data)
except Exception as e:
return jsonify({"error": f"Error processing graph data: {str(e)}"}), 500
# Extract metadata from filename
filename = os.path.basename(filepath)
metadata = {}
try:
# Different filename patterns for different datasets
if filename.startswith("bio_graph_"):
# Format: bio_graph_{entity}_merged_{model}.pkl
match = re.match(r"bio_graph_(.+?)_merged_(\w+)\.pkl", filename)
if match:
entity_name, model = match.groups()
metadata = {
"task": "bio",
"entity": entity_name,
"model": model,
"filename": filename,
}
elif filename.startswith("fp_graph_"):
# Format: fp_graph_{number}_merged_{model}.pkl
match = re.match(r"fp_graph_(\d+)_merged_(\w+)\.pkl", filename)
if match:
entity_name, model = match.groups()
metadata = {
"task": "fp",
"entity": f"Problem {entity_name}",
"model": model,
"filename": filename,
}
elif filename.startswith("qwen72_math_"):
# Format: qwen72_math_{number}.pkl
match = re.match(r"qwen72_math_(\d+)\.pkl", filename)
if match:
entity_name = match.group(1)
metadata = {
"task": "math",
"entity": f"Math Problem {entity_name}",
"model": "qwen72b",
"filename": filename,
}
elif filename.startswith("aime_qwen72b_"):
# Format: aime_qwen72b_{number}.pkl
match = re.match(r"aime_qwen72b_(\d+)\.pkl", filename)
if match:
entity_name = match.group(1)
metadata = {
"task": "aime",
"entity": f"AIME Problem {entity_name}",
"model": "qwen72b",
"filename": filename,
}
else:
# Generic pattern for other datasets
match = re.match(r"(\w+)_graph_(.+?)_merged_(\w+)\.pkl", filename)
if match:
task, entity_name, model = match.groups()
metadata = {
"task": task,
"entity": entity_name,
"model": model,
"filename": filename,
}
except Exception:
# Don't fail the request if metadata extraction fails
pass
# Extract text from consensus nodes
consensus_text = ""
try:
consensus_nodes = set(graph.consensus_node_ids)
consensus_node_texts = []
for node in graph.nodeiterator()():
if node.ID in consensus_nodes and node.text and node.text.strip():
consensus_node_texts.append(node.text.strip())
consensus_text = " ".join(consensus_node_texts)
except Exception:
consensus_text = ""
# Check if we should compute consensus using decode_consensus
compute_consensus = data.get("compute_consensus", False)
if compute_consensus and decode_consensus:
try:
# Determine task from metadata or default to "bio"
task = metadata.get("task", "bio") if metadata else "bio"
consensus_text = decode_consensus(graph, selection_threshold=0.5, task=task)
except Exception as e:
print(f"DEBUG: Error computing consensus with decode_consensus: {e}")
# Keep the original consensus text if decode_consensus fails
# Get original sequences
try:
raw_sequences = graph._seqs if hasattr(graph, "_seqs") else []
# Process sequences: join with spaces and remove "||"
print(f"DEBUG: Raw sequences: {raw_sequences}")
original_sequences = []
for seq in raw_sequences:
if isinstance(seq, list):
# Join list elements with spaces
processed_seq = " ".join(str(item) for item in seq)
else:
processed_seq = str(seq)
# Remove "||" characters
processed_seq = processed_seq.replace("||", "")
print(f"DEBUG: Processed sequence: {processed_seq}")
original_sequences.append(processed_seq)
except Exception:
original_sequences = []
result = {
"success": True,
"nodes": nodes,
"edges": edges,
"num_sequences": graph.num_sequences,
"num_nodes": len(nodes),
"num_edges": len(edges),
"metadata": metadata,
"consensus_text": consensus_text,
"original_sequences": original_sequences,
}
return jsonify(result)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/create_graph", methods=["POST"])
def create_graph():
"""Create a POA graph from text sequences"""
try:
data = request.get_json()
sequences = data.get("sequences", [])
if len(sequences) < 2:
return jsonify({"error": "At least 2 sequences are required"}), 400
print(f"DEBUG: Creating graph with sequences: {sequences}")
# Create the graph with first sequence as string
graph = TextPOAGraph(sequences[0], label=0)
print("DEBUG: Initial graph created")
# Add remaining sequences
for i, sequence in enumerate(sequences[1:], 1):
print(f"DEBUG: Adding sequence {i}: {sequence}")
alignment = TextSeqGraphAlignment(
text=sequence,
graph=graph,
fastMethod=True,
globalAlign=True,
matchscore=1,
mismatchscore=-2,
gap_open=-1,
)
graph.incorporateSeqAlignment(alignment, sequence, label=i)
print("DEBUG: All sequences added")
# Refine the graph with proper domain and model parameters
graph.refine_graph(verbose=False, domain="text", model="gpt-4o-mini")
print("DEBUG: Graph refined")
# Convert to JSON format for vis.js
nodes = []
edges = []
try:
print("DEBUG: Starting to process graph data")
# Get consensus nodes for coloring (make it optional)
try:
consensus_nodes = set(graph.consensus_node_ids)
print(f"DEBUG: Consensus nodes: {consensus_nodes}")
except Exception as e:
print(f"DEBUG: Error getting consensus nodes: {e}")
consensus_nodes = set() # Fallback to empty set if consensus fails
# Create nodes using the same logic as jsOutput
for node in graph.nodeiterator()():
title_text = ""
if node.sequences:
title_text += f"Sequences: {node.sequences}"
if node.variations:
title_text += ";;;".join(
[f"{sequence_id}: {text}" for sequence_id, text in node.variations.items()]
)
title_text = title_text.replace('"', "'")
# Use the same color logic as jsOutput
color = "#ceeab2" if node.ID in consensus_nodes else "#cae0e6"
node_data = {
"id": node.ID,
"label": f"{node.ID}: {node.text}",
"title": title_text,
"color": color,
}
nodes.append(node_data)
print(f"DEBUG: Created {len(nodes)} nodes")
# Create edges using the same logic as jsOutput
for node in graph.nodeiterator()():
nodeID = node.ID # Keep as integer
for edge in node.outEdges:
target = edge # Keep as integer
weight = node.outEdges[edge].weight + 1.5
edge_data = {
"from": nodeID,
"to": target,
"value": weight,
"color": "#cae0e6",
"arrows": "to",
}
edges.append(edge_data)
print(f"DEBUG: Created {len(edges)} edges")
except Exception as e:
print(f"DEBUG: Error processing graph data: {e}")
return jsonify({"error": f"Error processing graph data: {str(e)}"}), 500
# Extract text from consensus nodes
consensus_text = ""
try:
consensus_node_texts = []
for node in graph.nodeiterator()():
if node.ID in consensus_nodes and node.text and node.text.strip():
consensus_node_texts.append(node.text.strip())
consensus_text = " ".join(consensus_node_texts)
except Exception:
consensus_text = ""
# Check if we should compute consensus using decode_consensus
compute_consensus = data.get("compute_consensus", False)
if compute_consensus and decode_consensus:
try:
# Default to "bio" task for new graphs
consensus_text = decode_consensus(graph, selection_threshold=0.5, task="bio")
except Exception as e:
print(f"DEBUG: Error computing consensus with decode_consensus: {e}")
# Keep the original consensus text if decode_consensus fails
# Get original sequences
try:
raw_sequences = graph._seqs if hasattr(graph, "_seqs") else []
# Process sequences: join with spaces and remove "||"
original_sequences = []
for seq in raw_sequences:
if isinstance(seq, list):
# Join list elements with spaces
processed_seq = " ".join(str(item) for item in seq)
else:
processed_seq = str(seq)
# Remove "||" characters
processed_seq = processed_seq.replace("||", "")
original_sequences.append(processed_seq)
except Exception:
original_sequences = []
print("DEBUG: Returning success response")
return jsonify(
{
"success": True,
"nodes": nodes,
"edges": edges,
"num_sequences": len(sequences),
"num_nodes": len(nodes),
"num_edges": len(edges),
"original_sequences": original_sequences,
"consensus_text": consensus_text,
}
)
except Exception as e:
print(f"DEBUG: Main exception in create_graph: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/api/save_graph", methods=["POST"])
def save_graph():
"""Save a POA graph to a pickle file"""
try:
data = request.get_json()
sequences = data.get("sequences", [])
filename = data.get("filename", "graph.pkl")
if len(sequences) < 2:
return jsonify({"error": "At least 2 sequences are required"}), 400
# Create the graph
graph = TextPOAGraph(sequences[0], label=0)
# Add remaining sequences
for i, sequence in enumerate(sequences[1:], 1):
alignment = TextSeqGraphAlignment(
text=sequence,
graph=graph,
fastMethod=True,
globalAlign=True,
matchscore=1,
mismatchscore=-2,
gap_open=-1,
)
graph.incorporateSeqAlignment(alignment, sequence, label=i)
# Refine the graph
graph.refine_graph(verbose=False)
# Save to pickle file
graph.save_to_pickle(filename)
return jsonify(
{"success": True, "filename": filename, "message": f"Graph saved to {filename}"}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/graph_info", methods=["POST"])
def graph_info():
"""Get information about a graph without creating the full visualization"""
try:
data = request.get_json()
sequences = data.get("sequences", [])
if len(sequences) < 2:
return jsonify({"error": "At least 2 sequences are required"}), 400
# Create the graph
graph = TextPOAGraph(sequences[0], label=0)
# Add remaining sequences
for i, sequence in enumerate(sequences[1:], 1):
alignment = TextSeqGraphAlignment(
text=sequence,
graph=graph,
fastMethod=True,
globalAlign=True,
matchscore=1,
mismatchscore=-2,
gap_open=-1,
)
graph.incorporateSeqAlignment(alignment, sequence, label=i)
# Refine the graph
graph.refine_graph(verbose=False)
# Get consensus response
consensus_text = graph.consensus_response()
return jsonify(
{
"success": True,
"num_sequences": len(sequences),
"num_nodes": graph._nnodes,
"consensus_text": consensus_text,
"consensus_node_ids": graph.consensus_node_ids,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
print("Starting POA Graph Web Interface Server...")
print("Open http://localhost:8080 in your browser")
app.run(debug=True, host="0.0.0.0", port=8080)
|