| import gradio as gr | |
| import re | |
| def count_characters(input_str): | |
| # 去除空格和标点符号 | |
| filtered_str = re.sub(r'\s+|[^\w\s]', '', input_str) | |
| char_count = len(filtered_str) | |
| return f"该文本共有 {char_count} 个字符(不包含空格和标点)" | |
| def main(): | |
| interface = gr.Interface( | |
| fn=count_characters, | |
| inputs=gr.Textbox(label="输入文本"), | |
| outputs="text", | |
| title="字数检查工具", | |
| description="输入文本,统计不包含空格和标点的字数" | |
| ) | |
| interface.launch() | |
| if __name__ == '__main__': | |
| main() | |