YashJD's picture
Upload app.py
786eb26 verified
raw
history blame
3.31 kB
# import streamlit as st
# from pymongo import MongoClient
# import pandas as pd
# # Initialize MongoDB client
# client = MongoClient('mongodb+srv://vazeswaroop:yashdesai@cluster0.zvnjaaw.mongodb.net/')
# db = client['word_classification_db']
# collection = db['word_classification']
# # Function to save word and its category
# def save_word(word, category):
# collection.insert_one({"word": word, "category": category})
# # Function to load words
# def load_words():
# return pd.DataFrame(list(collection.find({}, {'_id': 0})))
# # Streamlit UI
# st.title('Word Classification')
# # Input fields for word and category
# word = st.text_input('Enter Word')
# category = st.selectbox('Select Category', ['Theme', 'Subtheme', 'Keywords'])
# if st.button('Save Word'):
# save_word(word, category)
# st.success(f'Word "{word}" saved under category "{category}"!')
# if st.button('View All Entries'):
# df = load_words()
# st.dataframe(df)
# # Close the MongoDB connection when the app is done
# client.close()
import streamlit as st
from pymongo import MongoClient
import pandas as pd
# Initialize MongoDB client
client = MongoClient('mongodb+srv://vazeswaroop:yashdesai@cluster0.zvnjaaw.mongodb.net/')
db = client['word_classification_db']
collection = db['word_classification']
# Function to save word and its category
def save_word(word, category):
collection.insert_one({"word": word, "category": category})
# Function to load words from the DB
def load_words():
return pd.DataFrame(list(collection.find({}, {'_id': 0})))
# Streamlit UI
st.title('Word Classification and Prompt Matching')
# Input fields for word and category
word = st.text_input('Enter Word')
category = st.selectbox('Select Category', ['Theme', 'Subtheme', 'Keywords'])
if st.button('Save Word'):
save_word(word, category)
st.success(f'Word "{word}" saved under category "{category}"!')
if st.button('View All Entries'):
df = load_words()
st.dataframe(df)
# Prompt Input for Matching
st.header('Prompt Matching')
prompt = st.text_area('Enter a prompt to check for matches')
if st.button('Check Prompt for Matches'):
df = load_words()
# Separate the words by categories
keywords = df[df['category'] == 'Keywords']['word'].tolist()
themes = df[df['category'] == 'Theme']['word'].tolist()
subthemes = df[df['category'] == 'Subtheme']['word'].tolist()
# Function to count matches
def count_matches(prompt, words_list):
return [word for word in words_list if word in prompt]
# Get the matches
matched_keywords = count_matches(prompt, keywords)
matched_themes = count_matches(prompt, themes)
matched_subthemes = count_matches(prompt, subthemes)
# Display the count and matched words
st.write(f"Number of Keywords matched: {len(matched_keywords)}")
st.write(f"Matched Keywords: {matched_keywords}")
st.write(f"Number of Themes matched: {len(matched_themes)}")
st.write(f"Matched Themes: {matched_themes}")
st.write(f"Number of Subthemes matched: {len(matched_subthemes)}")
st.write(f"Matched Subthemes: {matched_subthemes}")
# Close the MongoDB connection when the app is done
client.close()