File size: 2,466 Bytes
36599ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import time

import streamlit as st

from categories.accuracy import *


def response_generator(prompt):
    source = st.session_state.german
    acc = accuracy(source, prompt)

    response = "Your response is: " + str(acc["score"]) + "\n"

    if acc["errors"]:
        response += "Your errors are:\n"

        for error in acc["errors"]:
            response += f" - {error['message']}\n"

    lines = response.split("\n")
    for line in lines:
        for word in line.split():
            yield word + " "
            time.sleep(0.05)
        # After each line, yield a newline character or trigger a line break in Markdown
        yield "\n"


def translation_generator():
    st.session_state.german = "Danke shoen."

    message = (
        f"Please translate the following sentence into English:"
        f" {st.session_state.german}"
    )

    lines = message.split("\n")
    for line in lines:
        for word in line.split():
            yield word + " "
            time.sleep(0.05)
        # After each line, yield a newline character or trigger a line break in Markdown
        yield "\n"


st.title("Translation bot")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = [
        {
            "role": "assistant",
            "content": (
                "Hello! I am a translation bot. Please translate the following"
                " sentence into English: 'Das ist ein Test.'"
            ),
        }
    ]
    st.session_state.german = "Das ist ein Test."

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Accept user input
if prompt := st.chat_input("What is up?"):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)

    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        response = st.write_stream(response_generator(prompt))

    st.session_state.messages.append({"role": "assistant", "content": response})

    with st.chat_message("assistant"):
        message = st.write_stream(translation_generator())

    st.session_state.messages.append({"role": "assistant", "content": message})
    # Add assistant response to chat history