Aidan Phillips commited on
Commit
052b859
·
1 Parent(s): d7d2fdd

pull random sentences and better formatting

Browse files
app.py CHANGED
@@ -1,21 +1,51 @@
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:
@@ -27,7 +57,14 @@ def response_generator(prompt):
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:"
@@ -58,6 +95,16 @@ if "messages" not in st.session_state:
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"]):
 
1
  import time
2
+ import json
3
 
4
  import streamlit as st
5
 
6
  from categories.accuracy import *
7
+ from categories.fluency import *
8
+ import random
9
 
10
+ # Set the sidebar title
11
+ st.sidebar.title("DE-EN")
12
+
13
+ def load_translations():
14
+ try:
15
+ with open("./translations.json", "r") as f:
16
+ return json.loads(f.read())
17
+ except Exception as e:
18
+ print(e)
19
+ return None
20
+
21
+ if "translations" not in st.session_state:
22
+ st.session_state.translations = load_translations()
23
 
24
  def response_generator(prompt):
25
  source = st.session_state.german
26
  acc = accuracy(source, prompt)
27
+ ppl = pseudo_perplexity(prompt)
28
+ gre = grammar_errors(prompt)
29
+
30
+ total_score = 0.5 * acc["score"] + 0.2 * gre["score"] + 0.3 * ppl["score"]
31
 
32
+ response = "Your total translation score is: " + str(total_score) + "\n"
33
 
34
+ acc_s = acc["score"]
35
+ response += f"\nYour accuracy score is {acc_s}:\n"
36
 
37
+ for error in acc["errors"]:
38
+ response += f" - {error['message']}\n"
39
+
40
+ gre_s = gre["score"]
41
+ ppl_s = ppl["score"]
42
+ response += f"\nYour fluency score is {0.4 * gre_s + 0.6 * ppl_s}:\n"
43
+
44
+ for error in gre["errors"]:
45
+ response += f" - {error['message']}\n"
46
+
47
+ for error in ppl["errors"]:
48
+ response += f" - {error['message']}\n"
49
 
50
  lines = response.split("\n")
51
  for line in lines:
 
57
 
58
 
59
  def translation_generator():
60
+ # Check if translations are available and not empty
61
+ if st.session_state.translations:
62
+ # Randomly select a translation from the list
63
+ st.session_state.german = random.choice(st.session_state.translations)["german"]
64
+ else:
65
+ st.error("No translations available.")
66
+ return
67
+
68
 
69
  message = (
70
  f"Please translate the following sentence into English:"
 
95
  ]
96
  st.session_state.german = "Das ist ein Test."
97
 
98
+ if "translations" not in st.session_state:
99
+ try:
100
+ with open("translations.json", "r") as f:
101
+ st.session_state.translations = json.loads(f.read())
102
+ print(st.session_state.translations)
103
+ except (FileNotFoundError, json.JSONDecodeError):
104
+ st.session_state.translations = None
105
+ # Create an empty translations dictionary if none exists
106
+ st.error("No previous translations found. Starting with an empty translation history.")
107
+
108
  # Display chat messages from history on app rerun
109
  for message in st.session_state.messages:
110
  with st.chat_message(message["role"]):
categories/accuracy.py CHANGED
@@ -112,8 +112,8 @@ def __get_alignment_score(src_sentence: str, trg_sentence: str) -> list:
112
  # Each method has a list of pairs indicating the indexes of aligned words (The alignments are zero-indexed).
113
  alignments = aligner.get_word_aligns(src_list, trg_list)
114
 
115
- src_aligns = {x[0] for x in alignments["mwmf"]}
116
- trg_aligns = {x[1] for x in alignments["mwmf"]}
117
 
118
  mistranslations = []
119
  for i in range(len(src_list)):
 
112
  # Each method has a list of pairs indicating the indexes of aligned words (The alignments are zero-indexed).
113
  alignments = aligner.get_word_aligns(src_list, trg_list)
114
 
115
+ src_aligns = {x[0] for x in alignments["inter"]}
116
+ trg_aligns = {x[1] for x in alignments["inter"]}
117
 
118
  mistranslations = []
119
  for i in range(len(src_list)):
pages/1_ EN-DE.py ADDED
File without changes
pages/2_Scoring.py ADDED
File without changes
requirements.txt CHANGED
@@ -3,4 +3,6 @@ transformers
3
  torch
4
  wordfreq
5
  simalign
6
- streamlit
 
 
 
3
  torch
4
  wordfreq
5
  simalign
6
+ streamlit
7
+ requests
8
+ laser_encoders
translations.json ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {"german": "Ich bin müde.", "english": "I am tired."},
3
+ {"german": "Das ist mein Buch.", "english": "That is my book."},
4
+ {"german": "Er hat einen Hund.", "english": "He has a dog."},
5
+ {"german": "Wir gehen nach Hause.", "english": "We are going home."},
6
+ {"german": "Sie liest ein Buch.", "english": "She is reading a book."},
7
+ {"german": "Ich liebe Schokolade.", "english": "I love chocolate."},
8
+ {"german": "Hast du Geschwister?", "english": "Do you have siblings?"},
9
+ {"german": "Heute ist es kalt.", "english": "It is cold today."},
10
+ {"german": "Wo ist der Bahnhof?", "english": "Where is the train station?"},
11
+ {"german": "Das Wetter ist schön.", "english": "The weather is nice."},
12
+ {"german": "Er arbeitet im Büro.", "english": "He works in the office."},
13
+ {"german": "Ich trinke Wasser.", "english": "I am drinking water."},
14
+ {"german": "Sie tanzt gerne.", "english": "She likes dancing."},
15
+ {"german": "Kannst du mir helfen?", "english": "Can you help me?"},
16
+ {"german": "Wir spielen Fußball.", "english": "We are playing soccer."},
17
+ {"german": "Das Auto ist neu.", "english": "The car is new."},
18
+ {"german": "Ich habe Hunger.", "english": "I am hungry."},
19
+ {"german": "Guten Morgen!", "english": "Good morning!"},
20
+ {"german": "Er spricht Deutsch.", "english": "He speaks German."},
21
+ {"german": "Ich mag Katzen.", "english": "I like cats."},
22
+ {"german": "Sie kocht gern.", "english": "She likes cooking."},
23
+ {"german": "Der Apfel ist rot.", "english": "The apple is red."},
24
+ {"german": "Das Fenster ist offen.", "english": "The window is open."},
25
+ {"german": "Ich wohne in Berlin.", "english": "I live in Berlin."},
26
+ {"german": "Was machst du?", "english": "What are you doing?"},
27
+ {"german": "Er spielt Gitarre.", "english": "He plays guitar."},
28
+ {"german": "Wir fahren morgen.", "english": "We are leaving tomorrow."},
29
+ {"german": "Ich verstehe nicht.", "english": "I don't understand."},
30
+ {"german": "Die Katze schläft.", "english": "The cat is sleeping."},
31
+ {"german": "Sie ist meine Freundin.", "english": "She is my friend."},
32
+ {"german": "Ich höre Musik.", "english": "I am listening to music."},
33
+ {"german": "Das Haus ist groß.", "english": "The house is big."},
34
+ {"german": "Er geht zur Schule.", "english": "He goes to school."},
35
+ {"german": "Hast du Zeit?", "english": "Do you have time?"},
36
+ {"german": "Ich schreibe einen Brief.", "english": "I am writing a letter."},
37
+ {"german": "Die Tür ist geschlossen.", "english": "The door is closed."},
38
+ {"german": "Sie arbeitet viel.", "english": "She works a lot."},
39
+ {"german": "Ich sehe einen Vogel.", "english": "I see a bird."},
40
+ {"german": "Das Kind lacht.", "english": "The child is laughing."},
41
+ {"german": "Wo wohnst du?", "english": "Where do you live?"},
42
+ {"german": "Ich lerne Deutsch.", "english": "I am learning German."},
43
+ {"german": "Kann ich helfen?", "english": "Can I help?"},
44
+ {"german": "Das ist mein Bruder.", "english": "That is my brother."},
45
+ {"german": "Sie hat lange Haare.", "english": "She has long hair."},
46
+ {"german": "Wir sind müde.", "english": "We are tired."},
47
+ {"german": "Der Hund bellt.", "english": "The dog is barking."},
48
+ {"german": "Ich esse einen Apfel.", "english": "I am eating an apple."},
49
+ {"german": "Wie heißt du?", "english": "What is your name?"},
50
+ {"german": "Mein Name ist Anna.", "english": "My name is Anna."},
51
+ {"german": "Ich liebe dich.", "english": "I love you."},
52
+ {"german": "Es ist sehr spät.", "english": "It is very late."},
53
+ {"german": "Ich habe keine Zeit.", "english": "I have no time."},
54
+ {"german": "Er liest die Zeitung.", "english": "He is reading the newspaper."},
55
+ {"german": "Sie singt ein Lied.", "english": "She is singing a song."},
56
+ {"german": "Ich sehe fern.", "english": "I am watching TV."},
57
+ {"german": "Das ist einfach.", "english": "That is easy."},
58
+ {"german": "Ich gehe einkaufen.", "english": "I am going shopping."},
59
+ {"german": "Wo arbeitest du?", "english": "Where do you work?"},
60
+ {"german": "Ich arbeite im Krankenhaus.", "english": "I work at the hospital."},
61
+ {"german": "Sie hat blaue Augen.", "english": "She has blue eyes."},
62
+ {"german": "Wir essen zu Abend.", "english": "We are having dinner."},
63
+ {"german": "Der Himmel ist blau.", "english": "The sky is blue."},
64
+ {"german": "Was kostet das?", "english": "How much does that cost?"},
65
+ {"german": "Ich nehme das.", "english": "I'll take that."},
66
+ {"german": "Ich bin krank.", "english": "I am sick."},
67
+ {"german": "Es tut mir leid.", "english": "I'm sorry."},
68
+ {"german": "Ich verstehe dich.", "english": "I understand you."},
69
+ {"german": "Mach das Fenster zu.", "english": "Close the window."},
70
+ {"german": "Wie spät ist es?", "english": "What time is it?"},
71
+ {"german": "Ich bin zu Hause.", "english": "I am at home."},
72
+ {"german": "Gute Nacht!", "english": "Good night!"},
73
+ {"german": "Sie tanzen gern.", "english": "They like to dance."},
74
+ {"german": "Ich gehe spazieren.", "english": "I am going for a walk."},
75
+ {"german": "Der Kaffee ist heiß.", "english": "The coffee is hot."},
76
+ {"german": "Woher kommst du?", "english": "Where are you from?"},
77
+ {"german": "Ich komme aus Deutschland.", "english": "I am from Germany."},
78
+ {"german": "Sie hat ein schönes Lächeln.", "english": "She has a beautiful smile."},
79
+ {"german": "Ich brauche Hilfe.", "english": "I need help."},
80
+ {"german": "Das ist mein Vater.", "english": "That is my father."},
81
+ {"german": "Sie ist meine Mutter.", "english": "She is my mother."},
82
+ {"german": "Ich liebe meine Familie.", "english": "I love my family."},
83
+ {"german": "Heute ist Montag.", "english": "Today is Monday."},
84
+ {"german": "Ich bin glücklich.", "english": "I am happy."},
85
+ {"german": "Er ist traurig.", "english": "He is sad."},
86
+ {"german": "Wir lernen zusammen.", "english": "We are learning together."},
87
+ {"german": "Das ist interessant.", "english": "That is interesting."},
88
+ {"german": "Ich bin bereit.", "english": "I am ready."},
89
+ {"german": "Er ist mein Lehrer.", "english": "He is my teacher."},
90
+ {"german": "Der Ball ist rund.", "english": "The ball is round."},
91
+ {"german": "Ich gehe zur Arbeit.", "english": "I am going to work."},
92
+ {"german": "Sie trägt ein rotes Kleid.", "english": "She is wearing a red dress."},
93
+ {"german": "Ich mag deinen Stil.", "english": "I like your style."},
94
+ {"german": "Er lacht laut.", "english": "He is laughing loudly."},
95
+ {"german": "Wir sehen uns morgen.", "english": "See you tomorrow."},
96
+ {"german": "Ich wünsche dir viel Glück.", "english": "I wish you good luck."},
97
+ {"german": "Bitte sei leise.", "english": "Please be quiet."},
98
+ {"german": "Ich habe es vergessen.", "english": "I forgot it."},
99
+ {"german": "Es ist nicht wichtig.", "english": "It is not important."},
100
+ {"german": "Können wir reden?", "english": "Can we talk?"}
101
+ ]
102
+