syntax-parser-comparison / src /streamlit_app.py
utkarsh1797's picture
Update src/streamlit_app.py
991956b verified
raw
history blame
2.37 kB
import streamlit as st
import os
import subprocess
import nltk
import spacy
import benepar
from nltk import Tree
# Set NLTK path to writable /tmp
nltk_data_path = "/tmp/nltk_data"
nltk.data.path.append(nltk_data_path)
# Download safely
nltk.download('punkt', download_dir=nltk_data_path)
# Load spaCy model with fallback install
try:
nlp = spacy.load("en_core_web_sm")
except:
subprocess.run(["python", "-m", "pip", "install", "--user", "https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl"])
import importlib
importlib.invalidate_caches()
nlp = spacy.load("en_core_web_sm")
# Load benepar model safely
if "benepar" not in nlp.pipe_names:
try:
benepar.download('benepar_en3') # safe internal method
except:
subprocess.run(["python", "-m", "benepar.download", "benepar_en3"])
nlp.add_pipe("benepar", config={"model": "benepar_en3"})
# Streamlit UI
st.set_page_config(page_title="Syntax Parser Comparison", layout="wide")
st.title("🌐 Syntax Parser Comparison Tool")
st.write("This tool compares Dependency Parsing, Constituency Parsing, and a simulated Abstract Syntax Representation (ASR).")
# Input
sentence = st.text_input("Enter a sentence:", "John eats an apple.")
if sentence:
doc = nlp(sentence)
sent = list(doc.sents)[0]
col1, col2, col3 = st.columns(3)
with col1:
st.header("Dependency Parsing")
for token in sent:
st.write(f"{token.text} --> {token.dep_} --> {token.head.text}")
st.code(" ".join(f"({token.text}, {token.dep_}, {token.head.text})" for token in sent))
with col2:
st.header("Constituency Parsing")
tree = sent._.parse_string
st.text(tree)
st.code(Tree.fromstring(tree).pformat())
with col3:
st.header("Simulated ASR Output")
st.write("Combining phrase structure with dependency head annotations:")
for token in sent:
if token.dep_ in ("nsubj", "obj", "det", "ROOT"):
st.write(f"[{token.text}] - {token.dep_} --> {token.head.text} ({token.pos_})")
st.markdown("_(ASR is simulated by combining POS tags, dependency heads, and phrase information.)_")
st.code(" ".join(f"[{token.text}: {token.dep_} β†’ {token.head.text}]({token.pos_})" for token in sent))