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) | |
# 提取第一列的数据 | |
first_column_data = df.iloc[:, 0].tolist() | |
return first_column_data | |
# 生成问卷调查界面 | |
def generate_survey(data): | |
# 将数据转换为Markdown格式 | |
markdown_text = "\n".join([f"- {item}" for item in data]) | |
# 创建Gradio界面 | |
with gr.Blocks() as demo: | |
gr.Markdown("## 问卷调查") | |
gr.Markdown(markdown_text) | |
# 创建一个文本输入框用于打分 | |
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文件路径 | |
data = read_excel(file_path) | |
# 生成问卷调查界面 | |
demo = generate_survey(data) | |
# 启动Gradio应用 | |
demo.launch() | |
if __name__ == "__main__": | |
main() |