coroianpetruta commited on
Commit
50b88d8
1 Parent(s): fa06ccb

Gradio blocks interface

Browse files
Files changed (2) hide show
  1. app.py +75 -13
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,8 +1,8 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: enro-app.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['model_name', 'tokenizer', 'model', 'text', 'translationsout_1', 'translationsout_2', 'translationsout_3',
5
- 'translationsout_4', 'examples', 'intf', 'get_translations']
6
 
7
  # %% enro-app.ipynb 1
8
  from transformers import MarianMTModel, MarianTokenizer
@@ -14,28 +14,90 @@ tokenizer = MarianTokenizer.from_pretrained(model_name)
14
  model = MarianMTModel.from_pretrained(model_name)
15
 
16
  # %% enro-app.ipynb 3
 
 
17
  def get_translations(input_text):
18
  tokenized_text = tokenizer(input_text, return_tensors="pt")
19
-
20
  # Generate multiple translations
21
  # Set num_return_sequences to the number of translations you want
22
  translated = model.generate(**tokenized_text, num_return_sequences=4)
23
  translations = []
24
  for t in translated:
25
  translations.append(tokenizer.decode(t, skip_special_tokens=True))
26
-
27
- return translations[0], translations[1], translations[2], translations[3]
28
 
29
  # %% enro-app.ipynb 5
30
- text = gr.components.Text()
31
- translationsout_1 = gr.components.Text(label="Translation 1.")
32
- translationsout_2 = gr.components.Text(label="Translation 2")
33
- translationsout_3 = gr.components.Text(label="Translation 3")
34
- translationsout_4 = gr.components.Text(label="Translation 4")
35
- examples = ['The small mouse jumped.',"He's a bad writer.",'What is the way to the park?']
36
- intf = gr.Interface(fn=get_translations, inputs=text,outputs=[translationsout_1, translationsout_2, translationsout_3, translationsout_4],examples=examples)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
 
 
 
 
 
 
39
 
40
- intf.launch()
 
 
 
 
 
 
 
41
 
 
 
 
 
 
 
 
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: enro-app.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['model_name', 'tokenizer', 'model', 'example_translations', 'encoded_key', 'decoded_bytes', 'firebase_creds',
5
+ 'get_translations', 'save_option_to_repo', 'get_random_sentence', 'update_prompt']
6
 
7
  # %% enro-app.ipynb 1
8
  from transformers import MarianMTModel, MarianTokenizer
 
14
  model = MarianMTModel.from_pretrained(model_name)
15
 
16
  # %% enro-app.ipynb 3
17
+ import random
18
+
19
  def get_translations(input_text):
20
  tokenized_text = tokenizer(input_text, return_tensors="pt")
 
21
  # Generate multiple translations
22
  # Set num_return_sequences to the number of translations you want
23
  translated = model.generate(**tokenized_text, num_return_sequences=4)
24
  translations = []
25
  for t in translated:
26
  translations.append(tokenizer.decode(t, skip_special_tokens=True))
27
+ random_translations = random.sample(translations, 2)
28
+ return random_translations[0], random_translations[1]
29
 
30
  # %% enro-app.ipynb 5
31
+ example_translations = ["What is the way to get to the mosque?",
32
+ "Practice makes perfect.",
33
+ "The cat is eating the small mouse.",
34
+ "I'm not good at learning foreign languages.",
35
+ "What do you mean you don't know?",
36
+ "I should have sent a letter of apology.",
37
+ "His handwriting is poor.",
38
+ "He bowed his head."]
39
+
40
+ # %% enro-app.ipynb 6
41
+ import os
42
+ import firebase_admin
43
+ from firebase_admin import credentials
44
+ from firebase_admin import db
45
+ import json
46
+ import base64
47
+
48
+ encoded_key = os.getenv('FIREBASE_KEY')
49
+ decoded_bytes = base64.b64decode(encoded_key)
50
+ firebase_creds = json.loads(decoded_bytes.decode('utf-8'))
51
+ if not firebase_admin._apps:
52
+ cred = credentials.Certificate(firebase_creds)
53
+ firebase_admin.initialize_app(cred, {
54
+ 'databaseURL': 'https://ro-en-llm-default-rtdb.firebaseio.com/'
55
+ })
56
 
57
+ # %% enro-app.ipynb 7
58
+ def save_option_to_repo(trans_prompt, translation1, translation2, button):
59
+ ref = db.reference('feedback')
60
+ # Push new data to the database
61
+ ref.push({
62
+ 'prompt': trans_prompt,
63
+ 'translation_1': translation1,
64
+ 'translation_2': translation2,
65
+ 'feedback': button
66
+ })
67
+
68
+ def get_random_sentence():
69
+ return random.sample(example_translations, 1)[0]
70
+
71
+
72
+ def update_prompt():
73
+ prompt.change(value=get_random_sentence())
74
+
75
+ with gr.Blocks() as demo:
76
+
77
+
78
+ translations = []
79
+ option_buttons = []
80
 
81
+ with gr.Row():
82
+ prompt = gr.components.Textbox(scale = 4)
83
+ example_sentence = gr.Button("Get Random Sentence", scale = 1)
84
+
85
+ with gr.Row():
86
+ generate = gr.Button("Translate")
87
 
88
+ with gr.Row(equal_height=True):
89
+ translations.append(gr.components.Text(label=f"Translation 1"))
90
+ translations.append(gr.components.Text(label=f"Translation 2"))
91
+
92
+ with gr.Row():
93
+ option_buttons.append(gr.Button(value="Translation 1"))
94
+ option_buttons.append(gr.Button(value="Equal"))
95
+ option_buttons.append(gr.Button(value="Translation 2"))
96
 
97
+ generate.click(get_translations, inputs=prompt, outputs=translations)
98
+ example_sentence.click(get_random_sentence, outputs=prompt)
99
+ for i in range(0,3):
100
+ option_buttons[i].click(save_option_to_repo, inputs=[prompt, translations[0], translations[1], option_buttons[i]])
101
+
102
+
103
+ demo.launch()
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  transformers
2
  SentencePiece
3
- torch
 
 
1
  transformers
2
  SentencePiece
3
+ torch
4
+ firebase-admin