Shahzaib98 commited on
Commit
cb83116
·
verified ·
1 Parent(s): 51935fe

Removed customization feature for now

Browse files
Files changed (1) hide show
  1. app.py +243 -239
app.py CHANGED
@@ -334,244 +334,248 @@ def load_existing_graph():
334
  return jsonify({"error": str(e)}), 500
335
 
336
 
337
- @app.route("/api/create_graph", methods=["POST"])
338
- def create_graph():
339
- """Create a new POA graph from text sequences"""
340
- try:
341
- print("DEBUG: Received create_graph request")
342
- data = request.get_json()
343
- sequences = data.get("sequences", [])
344
-
345
- print(f"DEBUG: Number of sequences: {len(sequences)}")
346
-
347
- if len(sequences) < 2:
348
- return jsonify({"error": "At least 2 sequences are required"}), 400
349
-
350
- print("DEBUG: Creating initial graph")
351
- # Create the graph from first sequence
352
- graph = TextPOAGraph(sequences[0], label=0)
353
- print("DEBUG: Initial graph created")
354
-
355
- # Add remaining sequences
356
- for i, sequence in enumerate(sequences[1:], 1):
357
- print(f"DEBUG: Adding sequence {i}")
358
- alignment = TextSeqGraphAlignment(
359
- text=sequence,
360
- graph=graph,
361
- fastMethod=True,
362
- globalAlign=True,
363
- matchscore=1,
364
- mismatchscore=-2,
365
- gap_open=-1,
366
- )
367
- graph.incorporateSeqAlignment(alignment, sequence, label=i)
368
-
369
- print("DEBUG: All sequences added")
370
-
371
- # Refine the graph with proper domain and model parameters
372
- graph.refine_graph(verbose=False, domain="text", model="gpt-4o-mini")
373
- print("DEBUG: Graph refined")
374
-
375
- # Convert to JSON format for vis.js
376
- nodes = []
377
- edges = []
378
-
379
- try:
380
- print("DEBUG: Starting to process graph data")
381
- # Get consensus nodes for coloring (make it optional)
382
- try:
383
- consensus_nodes = set(graph.consensus_node_ids)
384
- print(f"DEBUG: Consensus nodes: {consensus_nodes}")
385
- except Exception as e:
386
- print(f"DEBUG: Error getting consensus nodes: {e}")
387
- consensus_nodes = set() # Fallback to empty set if consensus fails
388
-
389
- # Create nodes using the same logic as jsOutput
390
- for node in graph.nodeiterator()():
391
- title_text = ""
392
- if node.sequences:
393
- title_text += f"Sequences: {node.sequences}"
394
- if node.variations:
395
- title_text += ";;;".join(
396
- [f"{sequence_id}: {text}" for sequence_id, text in node.variations.items()]
397
- )
398
- title_text = title_text.replace('"', "'")
399
-
400
- # Use the same color logic as jsOutput
401
- color = "#ceeab2" if node.ID in consensus_nodes else "#cae0e6"
402
-
403
- node_data = {
404
- "id": node.ID,
405
- "label": f"{node.ID}: {node.text}",
406
- "title": title_text,
407
- "color": color,
408
- }
409
- nodes.append(node_data)
410
-
411
- print(f"DEBUG: Created {len(nodes)} nodes")
412
-
413
- # Create edges using the same logic as jsOutput
414
- for node in graph.nodeiterator()():
415
- nodeID = node.ID # Keep as integer
416
- for edge in node.outEdges:
417
- target = edge # Keep as integer
418
- weight = node.outEdges[edge].weight + 1.5
419
- edge_data = {
420
- "from": nodeID,
421
- "to": target,
422
- "value": weight,
423
- "color": "#cae0e6",
424
- "arrows": "to",
425
- }
426
- edges.append(edge_data)
427
-
428
- print(f"DEBUG: Created {len(edges)} edges")
429
- except Exception as e:
430
- print(f"DEBUG: Error processing graph data: {e}")
431
- return jsonify({"error": f"Error processing graph data: {str(e)}"}), 500
432
-
433
- # Extract text from consensus nodes
434
- consensus_text = ""
435
- try:
436
- consensus_node_texts = []
437
- for node in graph.nodeiterator()():
438
- if node.ID in consensus_nodes and node.text and node.text.strip():
439
- consensus_node_texts.append(node.text.strip())
440
- consensus_text = " ".join(consensus_node_texts)
441
- except Exception:
442
- consensus_text = ""
443
-
444
- # Check if we should compute consensus using decode_consensus
445
- compute_consensus = data.get("compute_consensus", False)
446
- if compute_consensus and decode_consensus:
447
- try:
448
- # Default to "bio" task for new graphs
449
- consensus_text = decode_consensus(graph, selection_threshold=0.5, task="bio")
450
- except Exception as e:
451
- print(f"DEBUG: Error computing consensus with decode_consensus: {e}")
452
- # Keep the original consensus text if decode_consensus fails
453
-
454
- # Get original sequences
455
- try:
456
- raw_sequences = graph._seqs if hasattr(graph, "_seqs") else []
457
- # Process sequences: join with spaces and remove "||"
458
- original_sequences = []
459
- for seq in raw_sequences:
460
- if isinstance(seq, list):
461
- # Join list elements with spaces
462
- processed_seq = " ".join(str(item) for item in seq)
463
- else:
464
- processed_seq = str(seq)
465
- # Remove "||" characters
466
- processed_seq = processed_seq.replace("||", "")
467
- original_sequences.append(processed_seq)
468
- except Exception:
469
- original_sequences = []
470
-
471
- print("DEBUG: Returning success response")
472
- return jsonify(
473
- {
474
- "success": True,
475
- "nodes": nodes,
476
- "edges": edges,
477
- "num_sequences": len(sequences),
478
- "num_nodes": len(nodes),
479
- "num_edges": len(edges),
480
- "original_sequences": original_sequences,
481
- "consensus_text": consensus_text,
482
- }
483
- )
484
-
485
- except Exception as e:
486
- print(f"DEBUG: Main exception in create_graph: {e}")
487
- return jsonify({"error": str(e)}), 500
488
-
489
-
490
- @app.route("/api/save_graph", methods=["POST"])
491
- def save_graph():
492
- """Save a POA graph to a pickle file"""
493
- try:
494
- data = request.get_json()
495
- sequences = data.get("sequences", [])
496
- filename = data.get("filename", "graph.pkl")
497
-
498
- if len(sequences) < 2:
499
- return jsonify({"error": "At least 2 sequences are required"}), 400
500
-
501
- # Create the graph
502
- graph = TextPOAGraph(sequences[0], label=0)
503
-
504
- # Add remaining sequences
505
- for i, sequence in enumerate(sequences[1:], 1):
506
- alignment = TextSeqGraphAlignment(
507
- text=sequence,
508
- graph=graph,
509
- fastMethod=True,
510
- globalAlign=True,
511
- matchscore=1,
512
- mismatchscore=-2,
513
- gap_open=-1,
514
- )
515
- graph.incorporateSeqAlignment(alignment, sequence, label=i)
516
-
517
- # Refine the graph
518
- graph.refine_graph(verbose=False)
519
-
520
- # Save to pickle file
521
- graph.save_to_pickle(filename)
522
-
523
- return jsonify(
524
- {"success": True, "filename": filename, "message": f"Graph saved to {filename}"}
525
- )
526
-
527
- except Exception as e:
528
- return jsonify({"error": str(e)}), 500
529
-
530
-
531
- @app.route("/api/graph_info", methods=["POST"])
532
- def graph_info():
533
- """Get information about a graph without creating the full visualization"""
534
- try:
535
- data = request.get_json()
536
- sequences = data.get("sequences", [])
537
-
538
- if len(sequences) < 2:
539
- return jsonify({"error": "At least 2 sequences are required"}), 400
540
-
541
- # Create the graph
542
- graph = TextPOAGraph(sequences[0], label=0)
543
-
544
- # Add remaining sequences
545
- for i, sequence in enumerate(sequences[1:], 1):
546
- alignment = TextSeqGraphAlignment(
547
- text=sequence,
548
- graph=graph,
549
- fastMethod=True,
550
- globalAlign=True,
551
- matchscore=1,
552
- mismatchscore=-2,
553
- gap_open=-1,
554
- )
555
- graph.incorporateSeqAlignment(alignment, sequence, label=i)
556
-
557
- # Refine the graph
558
- graph.refine_graph(verbose=False)
559
-
560
- # Get consensus response
561
- consensus_text = graph.consensus_response()
562
-
563
- return jsonify(
564
- {
565
- "success": True,
566
- "num_sequences": len(sequences),
567
- "num_nodes": graph._nnodes,
568
- "consensus_text": consensus_text,
569
- "consensus_node_ids": graph.consensus_node_ids,
570
- }
571
- )
572
-
573
- except Exception as e:
574
- return jsonify({"error": str(e)}), 500
 
 
 
 
575
 
576
 
577
  if __name__ == "__main__":
@@ -581,4 +585,4 @@ if __name__ == "__main__":
581
  print(f"Repository root: {REPO_ROOT}")
582
  print(f"Serving static files from: {os.path.join(REPO_ROOT, 'web_interface')}")
583
  print(f"Open http://localhost:{port} in your browser")
584
- app.run(debug=False, host="0.0.0.0", port=port)
 
334
  return jsonify({"error": str(e)}), 500
335
 
336
 
337
+ # ============================================================================
338
+ # COMMENTED OUT: Create New Graph Feature - Disabled per user request
339
+ # ============================================================================
340
+
341
+ # @app.route("/api/create_graph", methods=["POST"])
342
+ # def create_graph():
343
+ # """Create a new POA graph from text sequences"""
344
+ # try:
345
+ # print("DEBUG: Received create_graph request")
346
+ # data = request.get_json()
347
+ # sequences = data.get("sequences", [])
348
+ #
349
+ # print(f"DEBUG: Number of sequences: {len(sequences)}")
350
+ #
351
+ # if len(sequences) < 2:
352
+ # return jsonify({"error": "At least 2 sequences are required"}), 400
353
+ #
354
+ # print("DEBUG: Creating initial graph")
355
+ # # Create the graph from first sequence
356
+ # graph = TextPOAGraph(sequences[0], label=0)
357
+ # print("DEBUG: Initial graph created")
358
+ #
359
+ # # Add remaining sequences
360
+ # for i, sequence in enumerate(sequences[1:], 1):
361
+ # print(f"DEBUG: Adding sequence {i}")
362
+ # alignment = TextSeqGraphAlignment(
363
+ # text=sequence,
364
+ # graph=graph,
365
+ # fastMethod=True,
366
+ # globalAlign=True,
367
+ # matchscore=1,
368
+ # mismatchscore=-2,
369
+ # gap_open=-1,
370
+ # )
371
+ # graph.incorporateSeqAlignment(alignment, sequence, label=i)
372
+ #
373
+ # print("DEBUG: All sequences added")
374
+ #
375
+ # # Refine the graph with proper domain and model parameters
376
+ # graph.refine_graph(verbose=False, domain="text", model="gpt-4o-mini")
377
+ # print("DEBUG: Graph refined")
378
+ #
379
+ # # Convert to JSON format for vis.js
380
+ # nodes = []
381
+ # edges = []
382
+ #
383
+ # try:
384
+ # print("DEBUG: Starting to process graph data")
385
+ # # Get consensus nodes for coloring (make it optional)
386
+ # try:
387
+ # consensus_nodes = set(graph.consensus_node_ids)
388
+ # print(f"DEBUG: Consensus nodes: {consensus_nodes}")
389
+ # except Exception as e:
390
+ # print(f"DEBUG: Error getting consensus nodes: {e}")
391
+ # consensus_nodes = set() # Fallback to empty set if consensus fails
392
+ #
393
+ # # Create nodes using the same logic as jsOutput
394
+ # for node in graph.nodeiterator()():
395
+ # title_text = ""
396
+ # if node.sequences:
397
+ # title_text += f"Sequences: {node.sequences}"
398
+ # if node.variations:
399
+ # title_text += ";;;".join(
400
+ # [f"{sequence_id}: {text}" for sequence_id, text in node.variations.items()]
401
+ # )
402
+ # title_text = title_text.replace('"', "'")
403
+ #
404
+ # # Use the same color logic as jsOutput
405
+ # color = "#ceeab2" if node.ID in consensus_nodes else "#cae0e6"
406
+ #
407
+ # node_data = {
408
+ # "id": node.ID,
409
+ # "label": f"{node.ID}: {node.text}",
410
+ # "title": title_text,
411
+ # "color": color,
412
+ # }
413
+ # nodes.append(node_data)
414
+ #
415
+ # print(f"DEBUG: Created {len(nodes)} nodes")
416
+ #
417
+ # # Create edges using the same logic as jsOutput
418
+ # for node in graph.nodeiterator()():
419
+ # nodeID = node.ID # Keep as integer
420
+ # for edge in node.outEdges:
421
+ # target = edge # Keep as integer
422
+ # weight = node.outEdges[edge].weight + 1.5
423
+ # edge_data = {
424
+ # "from": nodeID,
425
+ # "to": target,
426
+ # "value": weight,
427
+ # "color": "#cae0e6",
428
+ # "arrows": "to",
429
+ # }
430
+ # edges.append(edge_data)
431
+ #
432
+ # print(f"DEBUG: Created {len(edges)} edges")
433
+ # except Exception as e:
434
+ # print(f"DEBUG: Error processing graph data: {e}")
435
+ # return jsonify({"error": f"Error processing graph data: {str(e)}"}), 500
436
+ #
437
+ # # Extract text from consensus nodes
438
+ # consensus_text = ""
439
+ # try:
440
+ # consensus_node_texts = []
441
+ # for node in graph.nodeiterator()():
442
+ # if node.ID in consensus_nodes and node.text and node.text.strip():
443
+ # consensus_node_texts.append(node.text.strip())
444
+ # consensus_text = " ".join(consensus_node_texts)
445
+ # except Exception:
446
+ # consensus_text = ""
447
+ #
448
+ # # Check if we should compute consensus using decode_consensus
449
+ # compute_consensus = data.get("compute_consensus", False)
450
+ # if compute_consensus and decode_consensus:
451
+ # try:
452
+ # # Default to "bio" task for new graphs
453
+ # consensus_text = decode_consensus(graph, selection_threshold=0.5, task="bio")
454
+ # except Exception as e:
455
+ # print(f"DEBUG: Error computing consensus with decode_consensus: {e}")
456
+ # # Keep the original consensus text if decode_consensus fails
457
+ #
458
+ # # Get original sequences
459
+ # try:
460
+ # raw_sequences = graph._seqs if hasattr(graph, "_seqs") else []
461
+ # # Process sequences: join with spaces and remove "||"
462
+ # original_sequences = []
463
+ # for seq in raw_sequences:
464
+ # if isinstance(seq, list):
465
+ # # Join list elements with spaces
466
+ # processed_seq = " ".join(str(item) for item in seq)
467
+ # else:
468
+ # processed_seq = str(seq)
469
+ # # Remove "||" characters
470
+ # processed_seq = processed_seq.replace("||", "")
471
+ # original_sequences.append(processed_seq)
472
+ # except Exception:
473
+ # original_sequences = []
474
+ #
475
+ # print("DEBUG: Returning success response")
476
+ # return jsonify(
477
+ # {
478
+ # "success": True,
479
+ # "nodes": nodes,
480
+ # "edges": edges,
481
+ # "num_sequences": len(sequences),
482
+ # "num_nodes": len(nodes),
483
+ # "num_edges": len(edges),
484
+ # "original_sequences": original_sequences,
485
+ # "consensus_text": consensus_text,
486
+ # }
487
+ # )
488
+ #
489
+ # except Exception as e:
490
+ # print(f"DEBUG: Main exception in create_graph: {e}")
491
+ # return jsonify({"error": str(e)}), 500
492
+
493
+
494
+ # @app.route("/api/save_graph", methods=["POST"])
495
+ # def save_graph():
496
+ # """Save a POA graph to a pickle file"""
497
+ # try:
498
+ # data = request.get_json()
499
+ # sequences = data.get("sequences", [])
500
+ # filename = data.get("filename", "graph.pkl")
501
+ #
502
+ # if len(sequences) < 2:
503
+ # return jsonify({"error": "At least 2 sequences are required"}), 400
504
+ #
505
+ # # Create the graph
506
+ # graph = TextPOAGraph(sequences[0], label=0)
507
+ #
508
+ # # Add remaining sequences
509
+ # for i, sequence in enumerate(sequences[1:], 1):
510
+ # alignment = TextSeqGraphAlignment(
511
+ # text=sequence,
512
+ # graph=graph,
513
+ # fastMethod=True,
514
+ # globalAlign=True,
515
+ # matchscore=1,
516
+ # mismatchscore=-2,
517
+ # gap_open=-1,
518
+ # )
519
+ # graph.incorporateSeqAlignment(alignment, sequence, label=i)
520
+ #
521
+ # # Refine the graph
522
+ # graph.refine_graph(verbose=False)
523
+ #
524
+ # # Save to pickle file
525
+ # graph.save_to_pickle(filename)
526
+ #
527
+ # return jsonify(
528
+ # {"success": True, "filename": filename, "message": f"Graph saved to {filename}"}
529
+ # )
530
+ #
531
+ # except Exception as e:
532
+ # return jsonify({"error": str(e)}), 500
533
+
534
+
535
+ # @app.route("/api/graph_info", methods=["POST"])
536
+ # def graph_info():
537
+ # """Get information about a graph without creating the full visualization"""
538
+ # try:
539
+ # data = request.get_json()
540
+ # sequences = data.get("sequences", [])
541
+ #
542
+ # if len(sequences) < 2:
543
+ # return jsonify({"error": "At least 2 sequences are required"}), 400
544
+ #
545
+ # # Create the graph
546
+ # graph = TextPOAGraph(sequences[0], label=0)
547
+ #
548
+ # # Add remaining sequences
549
+ # for i, sequence in enumerate(sequences[1:], 1):
550
+ # alignment = TextSeqGraphAlignment(
551
+ # text=sequence,
552
+ # graph=graph,
553
+ # fastMethod=True,
554
+ # globalAlign=True,
555
+ # matchscore=1,
556
+ # mismatchscore=-2,
557
+ # gap_open=-1,
558
+ # )
559
+ # graph.incorporateSeqAlignment(alignment, sequence, label=i)
560
+ #
561
+ # # Refine the graph
562
+ # graph.refine_graph(verbose=False)
563
+ #
564
+ # # Get consensus response
565
+ # consensus_text = graph.consensus_response()
566
+ #
567
+ # return jsonify(
568
+ # {
569
+ # "success": True,
570
+ # "num_sequences": len(sequences),
571
+ # "num_nodes": graph._nnodes,
572
+ # "consensus_text": consensus_text,
573
+ # "consensus_node_ids": graph.consensus_node_ids,
574
+ # }
575
+ # )
576
+ #
577
+ # except Exception as e:
578
+ # return jsonify({"error": str(e)}), 500
579
 
580
 
581
  if __name__ == "__main__":
 
585
  print(f"Repository root: {REPO_ROOT}")
586
  print(f"Serving static files from: {os.path.join(REPO_ROOT, 'web_interface')}")
587
  print(f"Open http://localhost:{port} in your browser")
588
+ app.run(debug=False, host="0.0.0.0", port=port)