ML-App / app.py
Arhashmi's picture
Create app.py
c468147
import streamlit as st
import subprocess
# Install NLTK directly within the script
subprocess.run(["pip", "install", "nltk"])
import nltk
nltk.download('punkt')
from nltk import ngrams
from nltk.tokenize import word_tokenize
# Function to generate n-grams from a given text
def generate_ngrams(text, n):
tokens = word_tokenize(text)
n_grams = ngrams(tokens, n)
return [' '.join(gram) for gram in n_grams]
# Streamlit web application
def main():
st.title("N-gram Generator")
# User input for text passage
text_input = st.text_area("Enter text passage:")
# User input for selecting n-gram type
n_gram_type = st.selectbox("Select n-gram type:", ["Bigram", "Trigram", "Custom N-gram"])
# Set n value based on user selection
if n_gram_type == "Bigram":
n_value = 2
elif n_gram_type == "Trigram":
n_value = 3
else:
n_value = st.number_input("Enter the value of N:", min_value=1, value=2, step=1)
# Generate n-grams and display the result
if st.button("Generate N-grams"):
if text_input:
ngrams_result = generate_ngrams(text_input, n_value)
st.write(f"{n_gram_type}s:")
for gram in ngrams_result:
st.write(gram)
else:
st.warning("Please enter a text passage.")
if __name__ == "__main__":
main()