Aidan Phillips
commited on
Commit
·
36599ed
1
Parent(s):
77a9ebf
frontend
Browse files- app.py +84 -0
- categories/accuracy.py +4 -1
- requirements.txt +2 -1
- scorer.ipynb +2 -2
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
from categories.accuracy import *
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def response_generator(prompt):
|
| 9 |
+
source = st.session_state.german
|
| 10 |
+
acc = accuracy(source, prompt)
|
| 11 |
+
|
| 12 |
+
response = "Your response is: " + str(acc["score"]) + "\n"
|
| 13 |
+
|
| 14 |
+
if acc["errors"]:
|
| 15 |
+
response += "Your errors are:\n"
|
| 16 |
+
|
| 17 |
+
for error in acc["errors"]:
|
| 18 |
+
response += f" - {error['message']}\n"
|
| 19 |
+
|
| 20 |
+
lines = response.split("\n")
|
| 21 |
+
for line in lines:
|
| 22 |
+
for word in line.split():
|
| 23 |
+
yield word + " "
|
| 24 |
+
time.sleep(0.05)
|
| 25 |
+
# After each line, yield a newline character or trigger a line break in Markdown
|
| 26 |
+
yield "\n"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def translation_generator():
|
| 30 |
+
st.session_state.german = "Danke shoen."
|
| 31 |
+
|
| 32 |
+
message = (
|
| 33 |
+
f"Please translate the following sentence into English:"
|
| 34 |
+
f" {st.session_state.german}"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
lines = message.split("\n")
|
| 38 |
+
for line in lines:
|
| 39 |
+
for word in line.split():
|
| 40 |
+
yield word + " "
|
| 41 |
+
time.sleep(0.05)
|
| 42 |
+
# After each line, yield a newline character or trigger a line break in Markdown
|
| 43 |
+
yield "\n"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
st.title("Translation bot")
|
| 47 |
+
|
| 48 |
+
# Initialize chat history
|
| 49 |
+
if "messages" not in st.session_state:
|
| 50 |
+
st.session_state.messages = [
|
| 51 |
+
{
|
| 52 |
+
"role": "assistant",
|
| 53 |
+
"content": (
|
| 54 |
+
"Hello! I am a translation bot. Please translate the following"
|
| 55 |
+
" sentence into English: 'Das ist ein Test.'"
|
| 56 |
+
),
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
st.session_state.german = "Das ist ein Test."
|
| 60 |
+
|
| 61 |
+
# Display chat messages from history on app rerun
|
| 62 |
+
for message in st.session_state.messages:
|
| 63 |
+
with st.chat_message(message["role"]):
|
| 64 |
+
st.markdown(message["content"])
|
| 65 |
+
|
| 66 |
+
# Accept user input
|
| 67 |
+
if prompt := st.chat_input("What is up?"):
|
| 68 |
+
# Add user message to chat history
|
| 69 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 70 |
+
# Display user message in chat message container
|
| 71 |
+
with st.chat_message("user"):
|
| 72 |
+
st.markdown(prompt)
|
| 73 |
+
|
| 74 |
+
# Display assistant response in chat message container
|
| 75 |
+
with st.chat_message("assistant"):
|
| 76 |
+
response = st.write_stream(response_generator(prompt))
|
| 77 |
+
|
| 78 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 79 |
+
|
| 80 |
+
with st.chat_message("assistant"):
|
| 81 |
+
message = st.write_stream(translation_generator())
|
| 82 |
+
|
| 83 |
+
st.session_state.messages.append({"role": "assistant", "content": message})
|
| 84 |
+
# Add assistant response to chat history
|
categories/accuracy.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import string
|
| 2 |
|
| 3 |
import torch
|
|
|
|
| 4 |
from scipy.spatial.distance import cosine
|
| 5 |
from simalign import SentenceAligner
|
| 6 |
from transformers import AutoModel, AutoTokenizer
|
|
@@ -97,7 +98,9 @@ def __bertscore_to_percentage(similarity: float) -> float:
|
|
| 97 |
int: A score from 0 to 100.
|
| 98 |
"""
|
| 99 |
# Scale the similarity score from [-1, 1] range to [0, 100] (rarely negative)
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
return round(scaled_score, 2)
|
| 102 |
|
| 103 |
|
|
|
|
| 1 |
import string
|
| 2 |
|
| 3 |
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
from scipy.spatial.distance import cosine
|
| 6 |
from simalign import SentenceAligner
|
| 7 |
from transformers import AutoModel, AutoTokenizer
|
|
|
|
| 98 |
int: A score from 0 to 100.
|
| 99 |
"""
|
| 100 |
# Scale the similarity score from [-1, 1] range to [0, 100] (rarely negative)
|
| 101 |
+
# Logistic function: 100 / (1 + exp(-k * (x - 0.5))), where k controls steepness
|
| 102 |
+
k = 35 # Steepness parameter - higher values create a sharper transition
|
| 103 |
+
scaled_score = 100 / (1 + np.exp(-k * (similarity - 0.65)))
|
| 104 |
return round(scaled_score, 2)
|
| 105 |
|
| 106 |
|
requirements.txt
CHANGED
|
@@ -2,4 +2,5 @@ language_tool_python
|
|
| 2 |
transformers
|
| 3 |
torch
|
| 4 |
wordfreq
|
| 5 |
-
simalign
|
|
|
|
|
|
| 2 |
transformers
|
| 3 |
torch
|
| 4 |
wordfreq
|
| 5 |
+
simalign
|
| 6 |
+
streamlit
|
scorer.ipynb
CHANGED
|
@@ -81,14 +81,14 @@
|
|
| 81 |
},
|
| 82 |
{
|
| 83 |
"cell_type": "code",
|
| 84 |
-
"execution_count":
|
| 85 |
"metadata": {},
|
| 86 |
"outputs": [
|
| 87 |
{
|
| 88 |
"name": "stdout",
|
| 89 |
"output_type": "stream",
|
| 90 |
"text": [
|
| 91 |
-
"Fluency Score: 76.
|
| 92 |
"Accuracy Score: 24.45\n"
|
| 93 |
]
|
| 94 |
}
|
|
|
|
| 81 |
},
|
| 82 |
{
|
| 83 |
"cell_type": "code",
|
| 84 |
+
"execution_count": 5,
|
| 85 |
"metadata": {},
|
| 86 |
"outputs": [
|
| 87 |
{
|
| 88 |
"name": "stdout",
|
| 89 |
"output_type": "stream",
|
| 90 |
"text": [
|
| 91 |
+
"Fluency Score: 76.62\n",
|
| 92 |
"Accuracy Score: 24.45\n"
|
| 93 |
]
|
| 94 |
}
|