|
import streamlit as st |
|
import tl_calamancy_lg |
|
import os |
|
import pandas as pd |
|
import json |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
|
|
|
|
@st.cache_resource() |
|
def load_nlp_model(): |
|
return tl_calamancy_lg.load() |
|
|
|
|
|
nlp = load_nlp_model() |
|
|
|
|
|
@st.cache_data |
|
def load_data(file_path): |
|
|
|
with open(file_path, 'r') as file: |
|
|
|
data = json.load(file) |
|
|
|
|
|
patterns_data = [] |
|
responses_data = [] |
|
|
|
for intent in data["intents"]: |
|
tag = intent["tag"] |
|
patterns = intent.get("patterns", []) |
|
responses = intent.get("responses", []) |
|
|
|
for pattern in patterns: |
|
patterns_data.append({"tag": tag, "pattern": pattern}) |
|
|
|
for response in responses: |
|
responses_data.append({"tag": tag, "response": response}) |
|
|
|
|
|
patterns_df = pd.DataFrame(patterns_data) |
|
responses_df = pd.DataFrame(responses_data) |
|
return patterns_df, responses_df |
|
|
|
|
|
cwd = os.getcwd() |
|
|
|
|
|
file_path = os.path.join(cwd, "dataset_v2.json") |
|
|
|
|
|
patterns_df, responses_df = load_data(file_path) |
|
|
|
|
|
@st.cache_data |
|
def get_most_similar_tag(user_query, dataframe): |
|
|
|
all_queries = list(dataframe['pattern']) + [user_query] |
|
processed_queries = [nlp(query) for query in all_queries] |
|
|
|
|
|
vectors = [query.vector for query in processed_queries] |
|
|
|
|
|
similarity_matrix = cosine_similarity(vectors, vectors) |
|
|
|
|
|
user_similarity_scores = similarity_matrix[-1, :-1] |
|
|
|
|
|
most_similar_index = user_similarity_scores.argmax() |
|
|
|
|
|
most_similar_tag = dataframe['tag'].iloc[most_similar_index] |
|
|
|
|
|
return most_similar_tag, user_similarity_scores[most_similar_index] |
|
|
|
def main(): |
|
|
|
st.title("TagaCare") |
|
|
|
|
|
if prompt := st.chat_input("Magtanong ng lunas sa sakit"): |
|
|
|
|
|
returned_tag, returned_score = get_most_similar_tag(prompt, patterns_df) |
|
|
|
st.success(returned_tag + str(returned_score)) |
|
st.success(responses_df[responses_df['tag']==returned_tag]['response']) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|