ahmedoumar commited on
Commit
3f0a242
1 Parent(s): 7836d4c

Upload turjumandemo.py

Browse files
Files changed (1) hide show
  1. turjumandemo.py +48 -0
turjumandemo.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """TurjumanDemo
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1VVJ7uPEYD8Q1pR-IINWWAQVpqyP1XnzD
8
+ """
9
+
10
+ # Installing dependencies
11
+ !pip install gradio
12
+ !pip install turjuman transformers
13
+ !git clone https://huggingface.co/spaces/ahmedoumar/TurjumanDemo
14
+
15
+ # Import our modules
16
+ import gradio as gr
17
+ from turjuman import turjuman
18
+ import logging
19
+ import os
20
+ from transformers import AutoTokenizer
21
+
22
+ logging.basicConfig(
23
+ format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
24
+ datefmt="%Y-%m-%d %H:%M:%S",
25
+ level=os.environ.get("LOGLEVEL", "INFO").upper(),
26
+ )
27
+ logger = logging.getLogger("turjuman.translate")
28
+ cache_dir="/content/mycache"
29
+
30
+ # Get the turjuman object and its tokenizer
31
+ turj = turjuman.turjuman(logger, cache_dir)
32
+ tokenizer = AutoTokenizer.from_pretrained('UBC-NLP/AraT5-base-title-generation')
33
+
34
+ # The translate function
35
+ def translate(sent):
36
+ beam_options = {"search_method":"beam", "seq_length": 300, "num_beams":5, "no_repeat_ngram_size":2, "max_outputs":1}
37
+ targets = turj.translate(sent,**beam_options)
38
+ #print(targets)
39
+ ans = ""
40
+ for target in targets:
41
+ target = tokenizer.decode(target, skip_special_tokens=True, clean_up_tokenization_spaces=True)
42
+ ans += target
43
+ return ans
44
+
45
+ print(translate('Здравствуй, друг'))
46
+
47
+ gr.Interface(fn=translate, inputs=['text'], outputs=['text']).launch(width=1000, height=1000)
48
+