Spaces:
Running
Running
add graph data output for api
Browse files
app.py
CHANGED
|
@@ -423,8 +423,8 @@ def visualize_graph(graph: GPGraph) -> Optional[str]:
|
|
| 423 |
# Gradio Interface
|
| 424 |
# ============================================================================
|
| 425 |
|
| 426 |
-
def process_sentence(sentence: str) -> Tuple[Optional[str], str]:
|
| 427 |
-
"""Process a sentence and return the visualization."""
|
| 428 |
try:
|
| 429 |
# Load model if not already loaded
|
| 430 |
model_loader = ModelLoader()
|
|
@@ -436,21 +436,30 @@ def process_sentence(sentence: str) -> Tuple[Optional[str], str]:
|
|
| 436 |
graph, error = parse_sentence(sentence, model_loader, use_constrained=use_constrained)
|
| 437 |
|
| 438 |
if error:
|
| 439 |
-
return None, error
|
| 440 |
|
| 441 |
# Visualize
|
| 442 |
img_path = visualize_graph(graph)
|
| 443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
decoding_mode = "constrained" if (use_constrained and model_loader.constrained_decoder) else "simple"
|
| 445 |
if img_path:
|
| 446 |
-
return img_path, f"Graph generated successfully! (Decoding: {decoding_mode})"
|
| 447 |
else:
|
| 448 |
-
return None, "Failed to generate visualization."
|
| 449 |
|
| 450 |
except Exception as e:
|
| 451 |
import traceback
|
| 452 |
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 453 |
-
return None, error_msg
|
| 454 |
|
| 455 |
|
| 456 |
def load_model_on_startup():
|
|
@@ -500,6 +509,10 @@ with gr.Blocks(title="Lingua Graph Parser") as demo:
|
|
| 500 |
type="filepath",
|
| 501 |
height=600
|
| 502 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
|
| 504 |
# Load model on startup
|
| 505 |
demo.load(
|
|
@@ -511,7 +524,7 @@ with gr.Blocks(title="Lingua Graph Parser") as demo:
|
|
| 511 |
parse_btn.click(
|
| 512 |
fn=process_sentence,
|
| 513 |
inputs=[sentence_input],
|
| 514 |
-
outputs=[output_image, output_text]
|
| 515 |
)
|
| 516 |
|
| 517 |
# Example sentences
|
|
@@ -537,7 +550,6 @@ with gr.Blocks(title="Lingua Graph Parser") as demo:
|
|
| 537 |
- Discourse markers
|
| 538 |
- And more...
|
| 539 |
|
| 540 |
-
**Model**: [rudaoshi/lingua](https://huggingface.co/rudaoshi/lingua)
|
| 541 |
""")
|
| 542 |
|
| 543 |
|
|
|
|
| 423 |
# Gradio Interface
|
| 424 |
# ============================================================================
|
| 425 |
|
| 426 |
+
def process_sentence(sentence: str) -> Tuple[Optional[str], str, Optional[Dict]]:
|
| 427 |
+
"""Process a sentence and return the visualization and graph data."""
|
| 428 |
try:
|
| 429 |
# Load model if not already loaded
|
| 430 |
model_loader = ModelLoader()
|
|
|
|
| 436 |
graph, error = parse_sentence(sentence, model_loader, use_constrained=use_constrained)
|
| 437 |
|
| 438 |
if error:
|
| 439 |
+
return None, error, None
|
| 440 |
|
| 441 |
# Visualize
|
| 442 |
img_path = visualize_graph(graph)
|
| 443 |
|
| 444 |
+
# Convert graph to dictionary (for JSON output)
|
| 445 |
+
graph_data = None
|
| 446 |
+
if graph is not None:
|
| 447 |
+
try:
|
| 448 |
+
graph_data = graph.data()
|
| 449 |
+
except Exception as e:
|
| 450 |
+
print(f"Warning: Failed to serialize graph to dict: {e}")
|
| 451 |
+
graph_data = None
|
| 452 |
+
|
| 453 |
decoding_mode = "constrained" if (use_constrained and model_loader.constrained_decoder) else "simple"
|
| 454 |
if img_path:
|
| 455 |
+
return img_path, f"Graph generated successfully! (Decoding: {decoding_mode})", graph_data
|
| 456 |
else:
|
| 457 |
+
return None, "Failed to generate visualization.", graph_data
|
| 458 |
|
| 459 |
except Exception as e:
|
| 460 |
import traceback
|
| 461 |
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 462 |
+
return None, error_msg, None
|
| 463 |
|
| 464 |
|
| 465 |
def load_model_on_startup():
|
|
|
|
| 509 |
type="filepath",
|
| 510 |
height=600
|
| 511 |
)
|
| 512 |
+
output_json = gr.JSON(
|
| 513 |
+
label="Graph Data (JSON)",
|
| 514 |
+
visible=False
|
| 515 |
+
)
|
| 516 |
|
| 517 |
# Load model on startup
|
| 518 |
demo.load(
|
|
|
|
| 524 |
parse_btn.click(
|
| 525 |
fn=process_sentence,
|
| 526 |
inputs=[sentence_input],
|
| 527 |
+
outputs=[output_image, output_text, output_json]
|
| 528 |
)
|
| 529 |
|
| 530 |
# Example sentences
|
|
|
|
| 550 |
- Discourse markers
|
| 551 |
- And more...
|
| 552 |
|
|
|
|
| 553 |
""")
|
| 554 |
|
| 555 |
|