bowphs commited on
Commit
637d1b6
·
1 Parent(s): a56f9a4

Add simple translation app

Browse files
Files changed (3) hide show
  1. README.md +3 -3
  2. app.py +34 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Ancient Text Generation
3
- emoji: 📉
4
- colorFrom: blue
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 3.47.1
8
  app_file: app.py
 
1
  ---
2
  title: Ancient Text Generation
3
+ emoji: 📜
4
+ colorFrom: indigo
5
+ colorTo: gray
6
  sdk: gradio
7
  sdk_version: 3.47.1
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
+ import os
4
+
5
+ AUTH_TOKEN = os.environ.get("TOKEN")
6
+ model = AutoModelForSeq2SeqLM.from_pretrained('bowphs/ancient-t5-translation', use_auth_token=AUTH_TOKEN)
7
+ tokenizer = AutoTokenizer.from_pretrained('bowphs/ancient-t5-translation', use_auth_token=AUTH_TOKEN)
8
+ generator = pipeline('text2text-generation', model=model, tokenizer=tokenizer)
9
+
10
+
11
+ def generate(text, generation_args):
12
+ arguments = {}
13
+ if generation_args:
14
+ pairs = generation_args.split(",")
15
+ for pair in pairs:
16
+ key, value = pair.strip().split('=')
17
+ arguments[key] = eval(value)
18
+
19
+ result = generator(text, max_length=30, num_return_sequences=1, **arguments)
20
+ return result[0]["generated_text"]
21
+
22
+ examples = [
23
+ ["translate english to latin: and he took the sword and killed the man."],
24
+ ["translate english to latin: and he took the sword and killed the man."],
25
+ ]
26
+
27
+ demo = gr.Interface(
28
+ fn=generate,
29
+ inputs=[gr.components.Textbox(value="translate greek to english: ὁ ἄνθρωπος τὸν οἶνον πίνειν ἐθέλων τὸν κρατῆρα ἔλαβεν.", label="Input Text"), gr.components.Textbox(value="do_sample=False, num_beams=3", label="Generation Parameters")],
30
+ outputs=gr.components.Textbox(value="the man took the bowl with the intention of drinking wine.", label="Generated Text"),
31
+ examples=examples
32
+ )
33
+
34
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers==4.34.0
2
+ torch==2.1.0
3
+ gradio==3.47.1