Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# from pymongo import MongoClient
|
| 3 |
+
# import pandas as pd
|
| 4 |
+
|
| 5 |
+
# # Initialize MongoDB client
|
| 6 |
+
# client = MongoClient('mongodb+srv://vazeswaroop:[email protected]/')
|
| 7 |
+
# db = client['word_classification_db']
|
| 8 |
+
# collection = db['word_classification']
|
| 9 |
+
|
| 10 |
+
# # Function to save word and its category
|
| 11 |
+
# def save_word(word, category):
|
| 12 |
+
# collection.insert_one({"word": word, "category": category})
|
| 13 |
+
|
| 14 |
+
# # Function to load words
|
| 15 |
+
# def load_words():
|
| 16 |
+
# return pd.DataFrame(list(collection.find({}, {'_id': 0})))
|
| 17 |
+
|
| 18 |
+
# # Streamlit UI
|
| 19 |
+
# st.title('Word Classification')
|
| 20 |
+
|
| 21 |
+
# # Input fields for word and category
|
| 22 |
+
# word = st.text_input('Enter Word')
|
| 23 |
+
# category = st.selectbox('Select Category', ['Theme', 'Subtheme', 'Keywords'])
|
| 24 |
+
|
| 25 |
+
# if st.button('Save Word'):
|
| 26 |
+
# save_word(word, category)
|
| 27 |
+
# st.success(f'Word "{word}" saved under category "{category}"!')
|
| 28 |
+
|
| 29 |
+
# if st.button('View All Entries'):
|
| 30 |
+
# df = load_words()
|
| 31 |
+
# st.dataframe(df)
|
| 32 |
+
|
| 33 |
+
# # Close the MongoDB connection when the app is done
|
| 34 |
+
# client.close()
|
| 35 |
+
import streamlit as st
|
| 36 |
+
from pymongo import MongoClient
|
| 37 |
+
import pandas as pd
|
| 38 |
+
|
| 39 |
+
# Initialize MongoDB client
|
| 40 |
+
client = MongoClient('mongodb+srv://vazeswaroop:[email protected]/')
|
| 41 |
+
db = client['word_classification_db']
|
| 42 |
+
collection = db['word_classification']
|
| 43 |
+
|
| 44 |
+
# Function to save word and its category
|
| 45 |
+
def save_word(word, category):
|
| 46 |
+
collection.insert_one({"word": word, "category": category})
|
| 47 |
+
|
| 48 |
+
# Function to load words from the DB
|
| 49 |
+
def load_words():
|
| 50 |
+
return pd.DataFrame(list(collection.find({}, {'_id': 0})))
|
| 51 |
+
|
| 52 |
+
# Streamlit UI
|
| 53 |
+
st.title('Word Classification and Prompt Matching')
|
| 54 |
+
|
| 55 |
+
# Input fields for word and category
|
| 56 |
+
word = st.text_input('Enter Word')
|
| 57 |
+
category = st.selectbox('Select Category', ['Theme', 'Subtheme', 'Keywords'])
|
| 58 |
+
|
| 59 |
+
if st.button('Save Word'):
|
| 60 |
+
save_word(word, category)
|
| 61 |
+
st.success(f'Word "{word}" saved under category "{category}"!')
|
| 62 |
+
|
| 63 |
+
if st.button('View All Entries'):
|
| 64 |
+
df = load_words()
|
| 65 |
+
st.dataframe(df)
|
| 66 |
+
|
| 67 |
+
# Prompt Input for Matching
|
| 68 |
+
st.header('Prompt Matching')
|
| 69 |
+
prompt = st.text_area('Enter a prompt to check for matches')
|
| 70 |
+
|
| 71 |
+
if st.button('Check Prompt for Matches'):
|
| 72 |
+
df = load_words()
|
| 73 |
+
|
| 74 |
+
# Separate the words by categories
|
| 75 |
+
keywords = df[df['category'] == 'Keywords']['word'].tolist()
|
| 76 |
+
themes = df[df['category'] == 'Theme']['word'].tolist()
|
| 77 |
+
subthemes = df[df['category'] == 'Subtheme']['word'].tolist()
|
| 78 |
+
|
| 79 |
+
# Function to count matches
|
| 80 |
+
def count_matches(prompt, words_list):
|
| 81 |
+
return [word for word in words_list if word in prompt]
|
| 82 |
+
|
| 83 |
+
# Get the matches
|
| 84 |
+
matched_keywords = count_matches(prompt, keywords)
|
| 85 |
+
matched_themes = count_matches(prompt, themes)
|
| 86 |
+
matched_subthemes = count_matches(prompt, subthemes)
|
| 87 |
+
|
| 88 |
+
# Display the count and matched words
|
| 89 |
+
st.write(f"Number of Keywords matched: {len(matched_keywords)}")
|
| 90 |
+
st.write(f"Matched Keywords: {matched_keywords}")
|
| 91 |
+
st.write(f"Number of Themes matched: {len(matched_themes)}")
|
| 92 |
+
st.write(f"Matched Themes: {matched_themes}")
|
| 93 |
+
st.write(f"Number of Subthemes matched: {len(matched_subthemes)}")
|
| 94 |
+
st.write(f"Matched Subthemes: {matched_subthemes}")
|
| 95 |
+
|
| 96 |
+
# Close the MongoDB connection when the app is done
|
| 97 |
+
client.close()
|