Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
-
|
| 5 |
-
model_name = "NlpHUST/gpt2-vietnamese"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 9 |
|
| 10 |
-
# Hàm sinh bio
|
| 11 |
def generate_bio(prompt, max_length, temperature):
|
| 12 |
-
# Prompt cứng để định hướng model
|
| 13 |
full_prompt = (
|
| 14 |
-
f"Viết
|
| 15 |
-
f"Chủ đề: {prompt.strip()}
|
| 16 |
)
|
| 17 |
-
|
| 18 |
outputs = text_generator(
|
| 19 |
full_prompt,
|
| 20 |
max_length=max_length,
|
| 21 |
do_sample=True,
|
| 22 |
temperature=temperature,
|
| 23 |
pad_token_id=tokenizer.eos_token_id,
|
| 24 |
-
num_return_sequences=1,
|
| 25 |
top_k=50,
|
| 26 |
-
top_p=0.95
|
|
|
|
| 27 |
)
|
| 28 |
-
|
| 29 |
-
# Xử lý đầu ra, loại bỏ phần prompt ban đầu
|
| 30 |
generated = outputs[0]["generated_text"]
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
with gr.Blocks(title="AI Viết Bio Thương Hiệu Cá Nhân (Tiếng Việt)") as app:
|
| 36 |
gr.Markdown("✨ **AI Viết Bio Thương Hiệu Cá Nhân (Tiếng Việt)**")
|
| 37 |
-
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column():
|
| 40 |
-
prompt_input = gr.Textbox(label="
|
| 41 |
-
max_length = gr.Slider(
|
| 42 |
-
temperature = gr.Slider(
|
| 43 |
-
submit_btn = gr.Button("
|
| 44 |
-
clear_btn = gr.Button("
|
| 45 |
-
|
| 46 |
with gr.Column():
|
| 47 |
-
output_box = gr.Textbox(label="
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
submit_btn.click(fn=generate_bio, inputs=[prompt_input, max_length, temperature], outputs=output_box)
|
| 51 |
-
clear_btn.click(fn=lambda: "", inputs=None, outputs=output_box)
|
| 52 |
|
| 53 |
app.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
+
model_name = "antphb/gpt2-vietnamese"
|
|
|
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 6 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 7 |
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 8 |
|
|
|
|
| 9 |
def generate_bio(prompt, max_length, temperature):
|
|
|
|
| 10 |
full_prompt = (
|
| 11 |
+
f"Viết 1 đoạn bio 1-2 câu, ngắn gọn, vui nhộn, dùng trên mạng xã hội.\n"
|
| 12 |
+
f"Chủ đề: {prompt.strip()}.\nBio:"
|
| 13 |
)
|
|
|
|
| 14 |
outputs = text_generator(
|
| 15 |
full_prompt,
|
| 16 |
max_length=max_length,
|
| 17 |
do_sample=True,
|
| 18 |
temperature=temperature,
|
| 19 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 20 |
top_k=50,
|
| 21 |
+
top_p=0.95,
|
| 22 |
+
num_return_sequences=1
|
| 23 |
)
|
|
|
|
|
|
|
| 24 |
generated = outputs[0]["generated_text"]
|
| 25 |
+
bio = generated.replace(full_prompt, "").strip()
|
| 26 |
+
sentences = bio.split(".")
|
| 27 |
+
result = ". ".join(sentences[:2]).strip()
|
| 28 |
+
if not result.endswith("."):
|
| 29 |
+
result += "."
|
| 30 |
+
return result
|
| 31 |
|
| 32 |
+
with gr.Blocks() as app:
|
|
|
|
| 33 |
gr.Markdown("✨ **AI Viết Bio Thương Hiệu Cá Nhân (Tiếng Việt)**")
|
|
|
|
| 34 |
with gr.Row():
|
| 35 |
with gr.Column():
|
| 36 |
+
prompt_input = gr.Textbox(lines=2, label="Mô tả bản thân / điểm mạnh")
|
| 37 |
+
max_length = gr.Slider(20, 200, value=60, step=5, label="Độ dài tối đa")
|
| 38 |
+
temperature = gr.Slider(0.1, 1.0, value=0.8, step=0.1, label="Mức sáng tạo")
|
| 39 |
+
submit_btn = gr.Button("Tạo bio")
|
| 40 |
+
clear_btn = gr.Button("Xóa")
|
|
|
|
| 41 |
with gr.Column():
|
| 42 |
+
output_box = gr.Textbox(lines=4, label="Bio gợi ý")
|
| 43 |
+
submit_btn.click(generate_bio, [prompt_input, max_length, temperature], output_box)
|
| 44 |
+
clear_btn.click(lambda: "", None, output_box)
|
|
|
|
|
|
|
| 45 |
|
| 46 |
app.launch()
|
| 47 |
+
|