-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from hannguyen2880/develop
Complete homework module1/week4 (complete all weeks in module1)
- Loading branch information
Showing
8 changed files
with
2,075 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"sonarCloudOrganization": "hannguyen2880", | ||
"projectKey": "hannguyen2880_AIO-Exercise" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"sonarlint.connectedMode.project": { | ||
"connectionId": "hannguyen2880", | ||
"projectKey": "hannguyen2880_AIO-Exercise" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import streamlit as st | ||
from hugchat import hugchat | ||
from hugchat.login import Login | ||
|
||
#Generating LLM response | ||
def generate_response(prompt_input, email, password): | ||
sign = Login(email, password) | ||
cookies = sign.login() | ||
chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) | ||
return chatbot.chat(prompt_input) | ||
|
||
# MAIN PROGRAM | ||
# ChatBot Title | ||
st.title('Simple ChatBot') | ||
with st.sidebar: | ||
st.title('Login HugChat') | ||
email = st.text_input('Enter email:') | ||
password = st.text_input('Enter password:', type='password') | ||
if email and password: | ||
st.success('Proceed to entering your prompt message!') | ||
else: | ||
st.warning('Please enter your account!') | ||
|
||
# Store LLM generated responses and display first message | ||
if "messages" not in st.session_state.keys(): | ||
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}] | ||
|
||
for message in st.session_state.messages: | ||
with st.chat_message("assistant"): | ||
st.write(message["content"]) | ||
|
||
# User-provided prompt | ||
if prompt := st.chat_input(disabled=not (email and password)): | ||
st.session_state.messages.append({"role": "user", "content": prompt}) | ||
with st.chat_message("user"): | ||
st.write(prompt) | ||
|
||
# Generate other responses | ||
if st.session_state.messages[-1]["role"] != "assistant": | ||
with st.chat_message("assistant"): | ||
with st.spinner("Thinking..."): | ||
response = generate_response(prompt, email, password) | ||
st.write(response) | ||
st.session_state.messages.append({"role": "assistant", "content": response}) | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import streamlit as st | ||
|
||
|
||
def levenshtein_distance(token1, token2): | ||
dist = [[0] * (len(token2) + 1) for _ in range(len(token1) + 1)] | ||
for i in range(len(token1) + 1): | ||
dist[i][0] = i | ||
for i in range(len(token2) + 1): | ||
dist[0][i] = i | ||
|
||
for i in range(1, len(token1) + 1): | ||
for j in range(1, len(token2) + 1): | ||
if (token1[i - 1] == token2[j - 1]): | ||
dist[i][j] = dist[i - 1][j - 1] | ||
else: | ||
dist[i][j] = min([dist[i][j - 1], dist[i - 1] | ||
[j], dist[i - 1][j - 1]]) + 1 | ||
|
||
return dist[len(token1)][len(token2)] | ||
|
||
|
||
def load_vocab_file(file_path): | ||
with open(file_path, 'r') as f: | ||
data = f.readlines() | ||
vocab = sorted(set(line.strip().lower() for line in data)) | ||
return vocab | ||
|
||
|
||
def main(): | ||
vocabs = load_vocab_file(file_path='Module1/Week4-Streamlit/vocab.txt') | ||
# print(vocabs) | ||
st.title('Word Correction using Levenshtein Distance') | ||
word = st.text_input('Word:') | ||
|
||
if st.button('Compute'): | ||
leven_dist = dict() | ||
|
||
for vocab in vocabs: | ||
leven_dist[vocab] = levenshtein_distance(word, vocab) | ||
|
||
sorted_dists = dict( | ||
sorted(leven_dist.items(), key=lambda item: item[1])) | ||
correct_word = list(sorted_dists.keys())[0] | ||
st.write('Correct Word: ', correct_word) | ||
col1, col2 = st.columns(2) | ||
col1.write('Vocabulary:') | ||
col1.write(vocabs) | ||
col2.write('Distances') | ||
col2.write(sorted_dists) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Binary file not shown.
Oops, something went wrong.