phamson02
commited on
Commit
路
4d5648e
1
Parent(s):
06eb913
update
Browse files- app.py +56 -5
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,11 +1,62 @@
|
|
1 |
-
import gradio
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
def my_inference_function(name):
|
5 |
-
return "Hello " + name + "!"
|
6 |
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
)
|
11 |
gradio_interface.launch()
|
|
|
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 generate_poem(text, style):
|
23 |
+
# Choose the model based on the selected style
|
24 |
+
model = models[style]
|
25 |
|
26 |
+
# Tokenize the input line
|
27 |
+
input_ids = tokenizer.encode(text, return_tensors="pt")
|
28 |
+
|
29 |
+
# Generate text
|
30 |
+
output = model.generate(input_ids, max_length=100, do_sample=True, temperature=0.7)
|
31 |
+
|
32 |
+
# Decode the output
|
33 |
+
generated_text = tokenizer.decode(
|
34 |
+
output[:, input_ids.shape[-1] :][0], skip_special_tokens=True
|
35 |
+
)
|
36 |
+
|
37 |
+
text = text + generated_text
|
38 |
+
|
39 |
+
# Post-process the output
|
40 |
+
text = text.replace("<unk>", "\n")
|
41 |
+
pretty_text = ""
|
42 |
+
for idx, line in enumerate(text.split("\n")):
|
43 |
+
line = line.strip()
|
44 |
+
if not line:
|
45 |
+
continue
|
46 |
+
line = line[0].upper() + line[1:]
|
47 |
+
pretty_text += line + "\n"
|
48 |
+
|
49 |
+
return pretty_text
|
50 |
+
|
51 |
+
|
52 |
+
gradio_interface = gr.Interface(
|
53 |
+
fn=generate_poem,
|
54 |
+
inputs=[
|
55 |
+
gr.inputs.Textbox(lines=1, placeholder="First words of the poem"),
|
56 |
+
gr.inputs.Dropdown(
|
57 |
+
choices=["Luc Bat", "Bay Chu", "Tam Chu", "Nam Chu"], label="Style"
|
58 |
+
),
|
59 |
+
],
|
60 |
+
outputs="text",
|
61 |
)
|
62 |
gradio_interface.launch()
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
gradio==3.35.2
|
|
|
|
1 |
gradio==3.35.2
|
2 |
+
transformers
|