wdeback commited on
Commit
ea93266
·
1 Parent(s): 5c67119

Upload folder using huggingface_hub

Browse files
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY="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"
.github/workflows/update_space.yml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Run Python script
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout
14
+ uses: actions/checkout@v2
15
+
16
+ - name: Set up Python
17
+ uses: actions/setup-python@v2
18
+ with:
19
+ python-version: '3.9'
20
+
21
+ - name: Install Gradio
22
+ run: python -m pip install gradio
23
+
24
+ - name: Log in to Hugging Face
25
+ run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
26
+
27
+ - name: Deploy to Spaces
28
+ run: gradio deploy
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Translation Game
3
- emoji: 🦀
4
- colorFrom: purple
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.0.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: translation_game
3
+ app_file: app/app.py
 
 
4
  sdk: gradio
5
+ sdk_version: 3.37.0
 
 
6
  ---
 
 
app/app.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import emoji
3
+ from random import shuffle
4
+ from loguru import logger
5
+ from dotenv import load_dotenv
6
+ load_dotenv()
7
+ from retry import retry
8
+ from typing import List, Dict
9
+
10
+ #
11
+ # -- llm.py --
12
+ #
13
+
14
+ load_dotenv()
15
+ from retry import retry
16
+ from typing import List, Dict
17
+
18
+ openai.api_key = os.getenv("OPENAI_API_KEY")
19
+
20
+
21
+ country = {"nl": {"language":"Dutch", "country":"Netherlands"},
22
+ "de": {"language":"German", "country":"Germany"},
23
+ "se": {"language":"Swedish", "country":"Sweden"},
24
+ "en": {"language":"English", "country":"England"}
25
+ }
26
+
27
+ levels = {"easy": [3, 6],
28
+ "medium": [5, 12],
29
+ "hard": [12, 20]}
30
+
31
+
32
+ def get_languages():
33
+ return list(country.keys())
34
+
35
+ def get_level():
36
+ return list(levels.keys())
37
+
38
+ def parse_response(response:dict, min_length:int=10) -> List[Dict[str, str]]:
39
+
40
+ logger.debug(f"{response = }")
41
+
42
+ logger.info(f"Parsing response from OpenAI API.")
43
+
44
+ text = response.choices[0].message.content
45
+ tokens = text.split("Sentence ")
46
+
47
+ logger.debug(f"Number of tokens: {len(tokens)}")
48
+ logger.debug(f"{tokens = }")
49
+
50
+
51
+ qas = []
52
+ for token in tokens:
53
+ if len(token) == 0:
54
+ continue
55
+ lines = token.split("\n")
56
+ #print(f"{lines = }")
57
+ original = lines[0][3:]
58
+
59
+ true_trans = ""
60
+ for line in lines:
61
+ if line.startswith("True: "):
62
+ true_trans = line[6:]
63
+
64
+ # get line starting with "False: "
65
+ false_trans = ""
66
+ for line in lines:
67
+ if line.startswith("False: "):
68
+ false_trans = line[7:]
69
+
70
+ # get line starting with "Funny: "
71
+ funny_trans = ""
72
+ for line in lines:
73
+ if line.startswith("Funny: "):
74
+ funny_trans = line[7:]
75
+
76
+ qa = {"original": original, "true": true_trans, "false": false_trans, "funny": funny_trans}
77
+
78
+ logger.debug(f"------------------------")
79
+ logger.debug(f"{qa = }")
80
+ logger.debug(f"------------------------")
81
+
82
+ if len(qa["original"]) > min_length and len(qa["true"]) > min_length and len(qa["false"]) > min_length and len(qa["funny"]) > min_length:
83
+ qas.append(qa)
84
+
85
+ logger.info(f"Returning {len(qas)} valid QA pairs.")
86
+
87
+ return qas
88
+
89
+ @retry(delay=0, backoff=2, max_delay=20, tries=10, logger=logger)
90
+ def generate(n:int, input_country:str, target_country:str, level:str, temperature:float=0.80) -> Dict[str, str]:
91
+ input_length = levels[level]
92
+
93
+ logger.info(f"Generating {str(n)} QA pairs for {country[input_country]['language']} to {country[target_country]['language']} with level: {level}.")
94
+
95
+ assert temperature >= 0.0 and temperature <= 1.0, "temperature must be between 0 and 1"
96
+ response = openai.ChatCompletion.create(
97
+ model="gpt-3.5-turbo",
98
+ messages=[
99
+ {"role": "system", "content":
100
+ f"You are a helpful assistant at creating a translation game.\
101
+ You create sentences in {str(country[input_country]['language'])} with a length that is exactly between {str(input_length[0])} to {str(input_length[1])} words.\
102
+ You create translations in {str(country[target_country]['language'])}.\
103
+ Formulate your answer in exactly this format: Sentence N: [X],\True: [A],\nFalse: [B],\nFunny: [C]."},
104
+ {"role": "assistant", "content":
105
+ f"Create original sentences with words, locations, concepts and phrases are typical for {str(country[input_country]['country'])} ."},
106
+ {"role": "user", "content":
107
+ f"Generate {str(n)} funny sentences. You create 1 correct translation, 1 incorrect translations, and 1 which very wrong and funny."},
108
+ ],
109
+ temperature=temperature,
110
+
111
+ )
112
+ return response
113
+
114
+
115
+
116
+ # n = 10
117
+ # input_country = "nl"
118
+ # target_country = "de"
119
+ # input_length = "easy"
120
+
121
+ def get_QAs(n:int, input_country:str, target_country:str, level:str, debug:bool):
122
+
123
+ if debug:
124
+ return [ {"original": "The Netherlands is a country in Europe.",
125
+ "true": "Nederland is een land in Europa.",
126
+ "false": "Nederland is een land in Azië.",
127
+ "funny": "Nederland is een aap in Europa."},
128
+ {"original": "Aap, noot, mies.",
129
+ "true": "Aap.",
130
+ "false": "Noot.",
131
+ "funny": "Mies."} ]
132
+ else:
133
+ return parse_response(generate(n=n, input_country=input_country, target_country=target_country, level=level))
134
+
135
+ #
136
+ # -- gui.py --
137
+ #
138
+ languages = get_languages()
139
+ levels = get_level()
140
+
141
+ global input_language
142
+ input_language = "en"
143
+ global target_language
144
+ target_language = "nl"
145
+ global level
146
+ level = "medium"
147
+ global debug
148
+ debug = False
149
+ global share
150
+ share = True
151
+
152
+ qas = llm.get_QAs(n=10, input_country=input_language, target_country=target_language, level=level, debug=debug)
153
+
154
+ def get_qa(n:int=1, input_country:str=input_language, target_country:str=target_language, level:str=level, debug:bool=debug):
155
+ logger.info(f"QAs left: {len(qas)}")
156
+ qa = qas.pop(0)
157
+
158
+ if len(qas) < 2:
159
+ logger.info(f"Generating new QAs...")
160
+ logger.debug(f"{debug = }")
161
+ qas.extend(llm.get_QAs(n=n,
162
+ input_country=input_country,
163
+ target_country=target_country,
164
+ level=level,
165
+ debug=debug))
166
+
167
+ question_md = f"## {qa['original']}"
168
+ options = [qa["true"], qa["false"], qa["funny"]]
169
+ true = qa["true"]
170
+ shuffle(options)
171
+ options_md = f"""### A: {options[0]}\n\n### B: {options[1]}\n\n### C: {options[2]}"""
172
+
173
+ global correct_answer
174
+ correct_answer = [letter for letter, option in zip(["A", "B", "C"], options) if option == true][0] # set global
175
+
176
+ logger.info(f"get_qa()")
177
+ logger.info(f"Question: {question_md}")
178
+ logger.info(f"Options: {options}")
179
+ logger.info(f"Correct answer: {correct_answer}")
180
+ return question_md, options_md
181
+
182
+ def update(answer):
183
+
184
+ # check answer
185
+ logger.info(f"Checking answer \"{answer}\" against \"{correct_answer}\"")
186
+ result = answer == correct_answer
187
+ # display output
188
+ gr.Info(emoji.emojize(":thumbs_up:"), ) if result else gr.Info(emoji.emojize(":thumbs_down:"))
189
+ #output = emoji.emojize("# :thumbs_up:") if result else emoji.emojize("# :thumbs_down:")
190
+
191
+ # update question
192
+ question, options = get_qa()
193
+
194
+ logger.info(f"update()")
195
+ logger.info(f"Question: {question}")
196
+ logger.info(f"Options: {options}")
197
+
198
+ return question, options
199
+
200
+
201
+ def get_interface(question, options):
202
+
203
+ with gr.Blocks() as blocks:
204
+ with gr.Column():
205
+ # show question
206
+ question_md = gr.Markdown(question)
207
+
208
+ # show answers
209
+ answers_md = gr.Markdown(options)
210
+
211
+ # add radio buttons
212
+ radio = gr.Radio(choices=["A", "B", "C"], label=None, show_label=False, info=None)
213
+
214
+ # add submit button
215
+ button = gr.Button("Submit")
216
+
217
+ # # show output box
218
+ # output = gr.Markdown()
219
+
220
+ # event listener
221
+ button.click(fn=update, inputs=radio, outputs=[question_md, answers_md], scroll_to_output=True)
222
+
223
+
224
+ with gr.Accordion("See Details", open=False):
225
+ # Advanced options
226
+ input_language = gr.Radio(languages, label="Input language", value="se", interactive=True)
227
+ target_language = gr.Radio(languages, label="Output language", value="nl", interactive=True)
228
+ level = gr.Radio(levels, label="Level", value="medium", interactive=True)
229
+
230
+ return blocks.queue().launch(share=share, debug=debug)
231
+
232
+
233
+ question, options = get_qa(n=1, input_country=input_language, target_country=target_language, level=level, debug=debug)
234
+ interface = get_interface(question, options)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ emoji
2
+ gradio
3
+ dotenv
4
+ openai
src/game/__init__.py ADDED
File without changes
src/game/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (154 Bytes). View file
 
src/game/__pycache__/gui.cpython-310.pyc ADDED
Binary file (2.82 kB). View file
 
src/game/__pycache__/llm.cpython-310.pyc ADDED
Binary file (3.82 kB). View file
 
src/game/gui.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import emoji
3
+ from random import shuffle
4
+ from . import llm
5
+ from loguru import logger
6
+
7
+ languages = llm.get_languages()
8
+ levels = llm.get_level()
9
+
10
+ global input_language
11
+ input_language = "en"
12
+ global target_language
13
+ target_language = "nl"
14
+ global level
15
+ level = "medium"
16
+ global debug
17
+ debug = False
18
+ global share
19
+ share = True
20
+
21
+ qas = llm.get_QAs(n=10, input_country=input_language, target_country=target_language, level=level, debug=debug)
22
+
23
+ def get_qa(n:int=1, input_country:str=input_language, target_country:str=target_language, level:str=level, debug:bool=debug):
24
+ logger.info(f"QAs left: {len(qas)}")
25
+ qa = qas.pop(0)
26
+
27
+ if len(qas) < 2:
28
+ logger.info(f"Generating new QAs...")
29
+ logger.debug(f"{debug = }")
30
+ qas.extend(llm.get_QAs(n=n,
31
+ input_country=input_country,
32
+ target_country=target_country,
33
+ level=level,
34
+ debug=debug))
35
+
36
+ question_md = f"## {qa['original']}"
37
+ options = [qa["true"], qa["false"], qa["funny"]]
38
+ true = qa["true"]
39
+ shuffle(options)
40
+ options_md = f"""### A: {options[0]}\n\n### B: {options[1]}\n\n### C: {options[2]}"""
41
+
42
+ global correct_answer
43
+ correct_answer = [letter for letter, option in zip(["A", "B", "C"], options) if option == true][0] # set global
44
+
45
+ logger.info(f"get_qa()")
46
+ logger.info(f"Question: {question_md}")
47
+ logger.info(f"Options: {options}")
48
+ logger.info(f"Correct answer: {correct_answer}")
49
+ return question_md, options_md
50
+
51
+ def update(answer):
52
+
53
+ # check answer
54
+ logger.info(f"Checking answer \"{answer}\" against \"{correct_answer}\"")
55
+ result = answer == correct_answer
56
+ # display output
57
+ gr.Info(emoji.emojize(":thumbs_up:"), ) if result else gr.Info(emoji.emojize(":thumbs_down:"))
58
+ #output = emoji.emojize("# :thumbs_up:") if result else emoji.emojize("# :thumbs_down:")
59
+
60
+ # update question
61
+ question, options = get_qa()
62
+
63
+ logger.info(f"update()")
64
+ logger.info(f"Question: {question}")
65
+ logger.info(f"Options: {options}")
66
+
67
+ return question, options
68
+
69
+
70
+ def get_interface(question, options):
71
+
72
+ with gr.Blocks() as blocks:
73
+ with gr.Column():
74
+ # show question
75
+ question_md = gr.Markdown(question)
76
+
77
+ # show answers
78
+ answers_md = gr.Markdown(options)
79
+
80
+ # add radio buttons
81
+ radio = gr.Radio(choices=["A", "B", "C"], label=None, show_label=False, info=None)
82
+
83
+ # add submit button
84
+ button = gr.Button("Submit")
85
+
86
+ # # show output box
87
+ # output = gr.Markdown()
88
+
89
+ # event listener
90
+ button.click(fn=update, inputs=radio, outputs=[question_md, answers_md], scroll_to_output=True)
91
+
92
+
93
+ with gr.Accordion("See Details", open=False):
94
+ # Advanced options
95
+ input_language = gr.Radio(languages, label="Input language", value="se", interactive=True)
96
+ target_language = gr.Radio(languages, label="Output language", value="nl", interactive=True)
97
+ level = gr.Radio(levels, label="Level", value="medium", interactive=True)
98
+
99
+ return blocks.queue().launch(share=share, debug=debug)
100
+
101
+
102
+ question, options = get_qa(n=1, input_country=input_language, target_country=target_language, level=level, debug=debug)
103
+ interface = get_interface(question, options)
src/game/llm.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+ from loguru import logger
4
+ from dotenv import load_dotenv
5
+ load_dotenv()
6
+ import gradio as gr
7
+ from retry import retry
8
+ from typing import List, Dict
9
+
10
+ openai.api_key = os.getenv("OPENAI_API_KEY")
11
+
12
+
13
+ country = {"nl": {"language":"Dutch", "country":"Netherlands"},
14
+ "de": {"language":"German", "country":"Germany"},
15
+ "se": {"language":"Swedish", "country":"Sweden"},
16
+ "en": {"language":"English", "country":"England"}
17
+ }
18
+
19
+ levels = {"easy": [3, 6],
20
+ "medium": [5, 12],
21
+ "hard": [12, 20]}
22
+
23
+
24
+ def get_languages():
25
+ return list(country.keys())
26
+
27
+ def get_level():
28
+ return list(levels.keys())
29
+
30
+ def parse_response(response:dict, min_length:int=10) -> List[Dict[str, str]]:
31
+
32
+ logger.debug(f"{response = }")
33
+
34
+ logger.info(f"Parsing response from OpenAI API.")
35
+
36
+ text = response.choices[0].message.content
37
+ tokens = text.split("Sentence ")
38
+
39
+ logger.debug(f"Number of tokens: {len(tokens)}")
40
+ logger.debug(f"{tokens = }")
41
+
42
+
43
+ qas = []
44
+ for token in tokens:
45
+ if len(token) == 0:
46
+ continue
47
+ lines = token.split("\n")
48
+ #print(f"{lines = }")
49
+ original = lines[0][3:]
50
+
51
+ true_trans = ""
52
+ for line in lines:
53
+ if line.startswith("True: "):
54
+ true_trans = line[6:]
55
+
56
+ # get line starting with "False: "
57
+ false_trans = ""
58
+ for line in lines:
59
+ if line.startswith("False: "):
60
+ false_trans = line[7:]
61
+
62
+ # get line starting with "Funny: "
63
+ funny_trans = ""
64
+ for line in lines:
65
+ if line.startswith("Funny: "):
66
+ funny_trans = line[7:]
67
+
68
+ qa = {"original": original, "true": true_trans, "false": false_trans, "funny": funny_trans}
69
+
70
+ logger.debug(f"------------------------")
71
+ logger.debug(f"{qa = }")
72
+ logger.debug(f"------------------------")
73
+
74
+ if len(qa["original"]) > min_length and len(qa["true"]) > min_length and len(qa["false"]) > min_length and len(qa["funny"]) > min_length:
75
+ qas.append(qa)
76
+
77
+ logger.info(f"Returning {len(qas)} valid QA pairs.")
78
+
79
+ return qas
80
+
81
+ @retry(delay=0, backoff=2, max_delay=20, tries=10, logger=logger)
82
+ def generate(n:int, input_country:str, target_country:str, level:str, temperature:float=0.80) -> Dict[str, str]:
83
+ input_length = levels[level]
84
+
85
+ logger.info(f"Generating {str(n)} QA pairs for {country[input_country]['language']} to {country[target_country]['language']} with level: {level}.")
86
+
87
+ assert temperature >= 0.0 and temperature <= 1.0, "temperature must be between 0 and 1"
88
+ response = openai.ChatCompletion.create(
89
+ model="gpt-3.5-turbo",
90
+ messages=[
91
+ {"role": "system", "content":
92
+ f"You are a helpful assistant at creating a translation game.\
93
+ You create sentences in {str(country[input_country]['language'])} with a length that is exactly between {str(input_length[0])} to {str(input_length[1])} words.\
94
+ You create translations in {str(country[target_country]['language'])}.\
95
+ Formulate your answer in exactly this format: Sentence N: [X],\True: [A],\nFalse: [B],\nFunny: [C]."},
96
+ {"role": "assistant", "content":
97
+ f"Create original sentences with words, locations, concepts and phrases are typical for {str(country[input_country]['country'])} ."},
98
+ {"role": "user", "content":
99
+ f"Generate {str(n)} funny sentences. You create 1 correct translation, 1 incorrect translations, and 1 which very wrong and funny."},
100
+ ],
101
+ temperature=temperature,
102
+
103
+ )
104
+ return response
105
+
106
+
107
+
108
+ # n = 10
109
+ # input_country = "nl"
110
+ # target_country = "de"
111
+ # input_length = "easy"
112
+
113
+ def get_QAs(n:int, input_country:str, target_country:str, level:str, debug:bool):
114
+
115
+ if debug:
116
+ return [ {"original": "The Netherlands is a country in Europe.",
117
+ "true": "Nederland is een land in Europa.",
118
+ "false": "Nederland is een land in Azië.",
119
+ "funny": "Nederland is een aap in Europa."},
120
+ {"original": "Aap, noot, mies.",
121
+ "true": "Aap.",
122
+ "false": "Noot.",
123
+ "funny": "Mies."} ]
124
+ else:
125
+ return parse_response(generate(n=n, input_country=input_country, target_country=target_country, level=level))
transfer.sh ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Source and destination paths
4
+ source_file="models/llama-2-7b-chat.ggmlv3.q4_0.bin"
5
+ destination_host="walter@katana2"
6
+ destination_path="/home/walter/repos/translation_game/models"
7
+
8
+ # rsync command with options
9
+ rsync -avz --partial --progress --append-verify -L -e "ssh" "$source_file" "$destination_host:$destination_path"