Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import json | |
| # 读取Excel文件 | |
| def read_excel(file_path): | |
| df = pd.read_excel(file_path) | |
| # 提取 Output 和 Initial Output 列的第一行数据 | |
| output_str = df["Output"].iloc[0] # 第一行 Output 列的值 | |
| initial_output_str = df["Initial Output"].iloc[0] # 第一行 Initial Output 列的值 | |
| return output_str, initial_output_str | |
| # 生成问卷调查界面 | |
| def generate_survey(output_str, initial_output_str): | |
| # 创建Gradio界面 | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 问卷调查") | |
| # 将 Output 列的数据解析为 Markdown 并显示 | |
| gr.Markdown("### Output (Markdown)") | |
| gr.Markdown(output_str) # 解析并显示 Markdown | |
| # 将 Initial Output 列的数据解析为 Markdown 并显示 | |
| gr.Markdown("### Initial Output (Markdown)") | |
| gr.Markdown(initial_output_str) # 解析并显示 Markdown | |
| # 创建一个文本输入框用于打分 | |
| score = gr.Textbox(label="请打分 (1-5)", placeholder="输入1到5之间的数字") | |
| # 创建一个提交按钮 | |
| submit_button = gr.Button("提交") | |
| # 创建一个输出文本框 | |
| output_text = gr.Textbox(label="提交结果", interactive=False) | |
| # 定义一个回调函数来处理提交 | |
| def submit(score): | |
| try: | |
| score = int(score) | |
| if 1 <= score <= 5: | |
| # 将结果保存到JSON文件 | |
| result = {"score": score} | |
| with open("survey_result.json", "w") as f: | |
| json.dump(result, f) | |
| return "提交成功!" | |
| else: | |
| return "请输入1到5之间的数字。" | |
| except ValueError: | |
| return "请输入有效的数字。" | |
| # 绑定提交按钮的回调函数 | |
| submit_button.click(submit, inputs=score, outputs=output_text) | |
| return demo | |
| # 主函数 | |
| def main(): | |
| # 读取Excel文件 | |
| file_path = "survey_data.xlsx" # 替换为你的Excel文件路径 | |
| output_str, initial_output_str = read_excel(file_path) | |
| # 生成问卷调查界面 | |
| demo = generate_survey(output_str, initial_output_str) | |
| # 启动Gradio应用 | |
| demo.launch() | |
| if __name__ == "__main__": | |
| main() |