Skip to content

Commit

Permalink
Merge pull request #14 from hannguyen2880/develop
Browse files Browse the repository at this point in the history
Complete homework module1/week4 (complete all weeks in module1)
  • Loading branch information
hannguyen2880 authored Jun 29, 2024
2 parents 863a129 + b29760c commit a77bdf2
Show file tree
Hide file tree
Showing 8 changed files with 2,075 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .sonarlint/connectedMode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sonarCloudOrganization": "hannguyen2880",
"projectKey": "hannguyen2880_AIO-Exercise"
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sonarlint.connectedMode.project": {
"connectionId": "hannguyen2880",
"projectKey": "hannguyen2880_AIO-Exercise"
}
}
49 changes: 49 additions & 0 deletions Module1/Week4-Streamlit/chatbot.py
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})





53 changes: 53 additions & 0 deletions Module1/Week4-Streamlit/levelshtein_distance.py
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.
Loading

0 comments on commit a77bdf2

Please sign in to comment.