import gradio as gr import evaluate # 创建困惑度计算工具 perplexity = evaluate.load("perplexity", module_type="metric") def compute_perplexity(text): # 直接使用输入的文本计算困惑度 perplexity_results = perplexity.compute(model_id='gpt2', add_start_token=False, predictions=[text]) perplexity_value = perplexity_results['perplexity'] # 返回困惑度结果 return f"Perplexity of the input text: {perplexity_value}" # 设置 Gradio 界面 demo = gr.Interface( fn=compute_perplexity, inputs="text", outputs="text", title="Compute Perplexity", description="Enter a text to compute its perplexity based on the gpt2 model." ) if __name__ == "__main__": demo.launch()