|
import gradio as gr |
|
from cijiang import CiJiangRhymer |
|
from cijiang.utils import print_results |
|
|
|
rhymer = CiJiangRhymer(strict=False, tone=True) |
|
|
|
def get_results(input_text, target_rhyme, beam_width=20, num_candidates=4000): |
|
out = rhymer.get_rhymes(text=input_text, target_rhyme=target_rhyme, beam_width=beam_width, num_candidates=num_candidates) |
|
mask_count = text.count("[M]") |
|
context = text.split('[M]')[0] |
|
|
|
all_outputs = f"======= 韵脚: |{target_rhyme}|\n" |
|
for i, (seq, log_prob) in enumerate(out[:top_results]): |
|
rhymes = seq[-mask_count:].split() |
|
colored_rhymes = [Fore.RED + part + Style.RESET_ALL if idx < mask_count else part for idx, part in enumerate(rhymes)] |
|
colored_rhymes = ''.join(colored_rhymes) |
|
|
|
all_outputs += f"{i+1}. {context}{colored_rhymes} (score: {log_prob:.3f})\n" |
|
all_outputs += "=" * 40 |
|
return all_out |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface(fn=get, inputs="text", outputs="text") |
|
demo.launch() |