phamson02
commited on
Commit
•
ebf036f
1
Parent(s):
cd29b18
update
Browse files- app.py +8 -63
- complete_poem.py +65 -0
- generate_poem.py +14 -0
app.py
CHANGED
@@ -1,66 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
"
|
8 |
-
"Libosa2707/vietnamese-poem-luc-bat-gpt2"
|
9 |
-
),
|
10 |
-
"Bay Chu": AutoModelForCausalLM.from_pretrained(
|
11 |
-
"Libosa2707/vietnamese-poem-bay-chu-gpt2"
|
12 |
-
),
|
13 |
-
"Tam Chu": AutoModelForCausalLM.from_pretrained(
|
14 |
-
"Libosa2707/vietnamese-poem-tam-chu-gpt2"
|
15 |
-
),
|
16 |
-
"Nam Chu": AutoModelForCausalLM.from_pretrained(
|
17 |
-
"Libosa2707/vietnamese-poem-nam-chu-gpt2"
|
18 |
-
),
|
19 |
-
}
|
20 |
-
|
21 |
-
|
22 |
-
def generate_poem(text, style):
|
23 |
-
# Preprocess the input text
|
24 |
-
text = text.strip()
|
25 |
-
text = text.lower()
|
26 |
-
|
27 |
-
# Choose the model based on the selected style
|
28 |
-
model = models[style]
|
29 |
-
|
30 |
-
# Tokenize the input line
|
31 |
-
input_ids = tokenizer.encode(text, return_tensors="pt")[:, :-1]
|
32 |
-
|
33 |
-
# Generate text
|
34 |
-
output = model.generate(input_ids, max_length=100, do_sample=True, temperature=0.7)
|
35 |
-
|
36 |
-
# Decode the output
|
37 |
-
generated_text = tokenizer.decode(
|
38 |
-
output[:, input_ids.shape[-1] :][0], skip_special_tokens=True
|
39 |
-
)
|
40 |
-
|
41 |
-
text = text + " " + generated_text
|
42 |
-
|
43 |
-
# Post-process the output
|
44 |
-
text = text.replace("<unk>", "\n")
|
45 |
-
pretty_text = ""
|
46 |
-
for idx, line in enumerate(text.split("\n")):
|
47 |
-
line = line.strip()
|
48 |
-
if not line:
|
49 |
-
continue
|
50 |
-
line = line[0].upper() + line[1:]
|
51 |
-
pretty_text += line + "\n"
|
52 |
-
|
53 |
-
return pretty_text
|
54 |
-
|
55 |
-
|
56 |
-
gradio_interface = gr.Interface(
|
57 |
-
fn=generate_poem,
|
58 |
-
inputs=[
|
59 |
-
gr.components.Textbox(lines=1, placeholder="First words of the poem"),
|
60 |
-
gr.components.Dropdown(
|
61 |
-
choices=["Luc Bat", "Bay Chu", "Tam Chu", "Nam Chu"], label="Style"
|
62 |
-
),
|
63 |
-
],
|
64 |
-
outputs="text",
|
65 |
)
|
66 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from complete_poem import complete_poem_interface
|
3 |
+
from generate_poem import generate_poem_interface
|
4 |
|
5 |
+
gr_interface = gr.TabbedInterface(
|
6 |
+
[complete_poem_interface, generate_poem_interface],
|
7 |
+
title="Thơ AI",
|
8 |
+
description="Ứng dụng mô hình ngôn ngữ cực lớn để sinh ra các bài thơ Việt Nam",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
)
|
10 |
+
|
11 |
+
gr_interface.launch()
|
complete_poem.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base")
|
5 |
+
# Define your models
|
6 |
+
models = {
|
7 |
+
"Luc Bat": AutoModelForCausalLM.from_pretrained(
|
8 |
+
"Libosa2707/vietnamese-poem-luc-bat-gpt2"
|
9 |
+
),
|
10 |
+
"Bay Chu": AutoModelForCausalLM.from_pretrained(
|
11 |
+
"Libosa2707/vietnamese-poem-bay-chu-gpt2"
|
12 |
+
),
|
13 |
+
"Tam Chu": AutoModelForCausalLM.from_pretrained(
|
14 |
+
"Libosa2707/vietnamese-poem-tam-chu-gpt2"
|
15 |
+
),
|
16 |
+
"Nam Chu": AutoModelForCausalLM.from_pretrained(
|
17 |
+
"Libosa2707/vietnamese-poem-nam-chu-gpt2"
|
18 |
+
),
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
def complete_poem(text, style):
|
23 |
+
# Preprocess the input text
|
24 |
+
text = text.strip()
|
25 |
+
text = text.lower()
|
26 |
+
|
27 |
+
# Choose the model based on the selected style
|
28 |
+
model = models[style]
|
29 |
+
|
30 |
+
# Tokenize the input line
|
31 |
+
input_ids = tokenizer.encode(text, return_tensors="pt")[:, :-1]
|
32 |
+
|
33 |
+
# Generate text
|
34 |
+
output = model.generate(input_ids, max_length=100, do_sample=True, temperature=0.7)
|
35 |
+
|
36 |
+
# Decode the output
|
37 |
+
generated_text = tokenizer.decode(
|
38 |
+
output[:, input_ids.shape[-1] :][0], skip_special_tokens=True
|
39 |
+
)
|
40 |
+
|
41 |
+
text = text + " " + generated_text
|
42 |
+
|
43 |
+
# Post-process the output
|
44 |
+
text = text.replace("<unk>", "\n")
|
45 |
+
pretty_text = ""
|
46 |
+
for idx, line in enumerate(text.split("\n")):
|
47 |
+
line = line.strip()
|
48 |
+
if not line:
|
49 |
+
continue
|
50 |
+
line = line[0].upper() + line[1:]
|
51 |
+
pretty_text += line + "\n"
|
52 |
+
|
53 |
+
return pretty_text
|
54 |
+
|
55 |
+
|
56 |
+
complete_poem_interface = gr.Interface(
|
57 |
+
fn=complete_poem,
|
58 |
+
inputs=[
|
59 |
+
gr.components.Textbox(lines=1, placeholder="First words of the poem"),
|
60 |
+
gr.components.Dropdown(
|
61 |
+
choices=["Luc Bat", "Bay Chu", "Tam Chu", "Nam Chu"], label="Style"
|
62 |
+
),
|
63 |
+
],
|
64 |
+
outputs="text",
|
65 |
+
)
|
generate_poem.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
|
4 |
+
def generate_poem(text):
|
5 |
+
pass
|
6 |
+
|
7 |
+
|
8 |
+
generate_poem_interface = gr.Interface(
|
9 |
+
fn=generate_poem,
|
10 |
+
inputs=[
|
11 |
+
gr.components.Textbox(lines=1, placeholder="First words of the poem"),
|
12 |
+
],
|
13 |
+
outputs="text",
|
14 |
+
)
|