Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import nltk
|
| 3 |
+
from nltk.tree import Tree
|
| 4 |
+
from nltk import pos_tag, word_tokenize
|
| 5 |
+
from nltk.parse import CoreNLPParser
|
| 6 |
+
import os
|
| 7 |
+
import base64
|
| 8 |
+
from graphviz import Source
|
| 9 |
+
|
| 10 |
+
# Ensure necessary NLTK resources are downloaded
|
| 11 |
+
nltk.download('punkt')
|
| 12 |
+
nltk.download('averaged_perceptron_tagger')
|
| 13 |
+
|
| 14 |
+
# Define a function to generate and render a syntax tree
|
| 15 |
+
def generate_syntax_tree(sentence):
|
| 16 |
+
# Tokenize and part-of-speech tag the sentence
|
| 17 |
+
tokens = word_tokenize(sentence)
|
| 18 |
+
tagged_tokens = pos_tag(tokens)
|
| 19 |
+
|
| 20 |
+
# Use NLTK's CoreNLPParser to parse the sentence
|
| 21 |
+
parser = CoreNLPParser(url='http://localhost:9000') # Make sure CoreNLP server is running
|
| 22 |
+
parsed_tree = next(parser.parse(tagged_tokens))
|
| 23 |
+
|
| 24 |
+
# Convert the tree to a DOT format for visualization
|
| 25 |
+
dot_format = parsed_tree._repr_svg_()
|
| 26 |
+
return dot_format
|
| 27 |
+
|
| 28 |
+
# Function to display syntax tree image
|
| 29 |
+
def display_tree(dot_format):
|
| 30 |
+
src = Source(dot_format)
|
| 31 |
+
svg = src.pipe(format='svg')
|
| 32 |
+
svg_base64 = base64.b64encode(svg).decode('utf-8')
|
| 33 |
+
svg_html = f"<div style='width:100%;height:100%;'><img src='data:image/svg+xml;base64,{svg_base64}'></div>"
|
| 34 |
+
return svg_html
|
| 35 |
+
|
| 36 |
+
# Set up the Streamlit interface
|
| 37 |
+
st.title("Syntax Tree Generator for English")
|
| 38 |
+
sentence = st.text_input("Enter a sentence:", "")
|
| 39 |
+
|
| 40 |
+
if sentence:
|
| 41 |
+
try:
|
| 42 |
+
dot_format = generate_syntax_tree(sentence)
|
| 43 |
+
svg_html = display_tree(dot_format)
|
| 44 |
+
st.markdown(svg_html, unsafe_allow_html=True)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
st.error(f"An error occurred: {e}")
|