Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import json
|
4 |
-
import markdown
|
5 |
|
6 |
-
#
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
first_column_data = df.iloc[:, 0].astype(str).tolist()
|
13 |
-
|
14 |
-
# 解析 Markdown 字符串
|
15 |
-
parsed_texts = [markdown.markdown(text) for text in first_column_data]
|
16 |
-
|
17 |
-
return parsed_texts
|
18 |
|
19 |
-
#
|
20 |
-
def
|
21 |
-
#
|
22 |
-
|
23 |
-
"question": question,
|
24 |
-
"selected_option": option
|
25 |
-
}
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
return
|
32 |
|
33 |
-
#
|
34 |
-
def
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
fn=questionnaire,
|
42 |
-
inputs=[
|
43 |
-
gr.Dropdown(choices=questions, label="请选择一个问题"),
|
44 |
-
gr.Radio(choices=["选项1", "选项2", "选项3"], label="请选择一个选项")
|
45 |
-
],
|
46 |
-
outputs="text",
|
47 |
-
title="问卷调查"
|
48 |
-
)
|
49 |
|
50 |
-
|
|
|
51 |
|
52 |
-
# 启动 Gradio 应用
|
53 |
if __name__ == "__main__":
|
54 |
-
|
55 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import json
|
|
|
4 |
|
5 |
+
# 读取Excel文件
|
6 |
+
def read_excel(file_path):
|
7 |
+
df = pd.read_excel(file_path)
|
8 |
+
# 提取第一列的数据
|
9 |
+
first_column_data = df.iloc[:, 0].tolist()
|
10 |
+
return first_column_data
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# 生成问卷调查界面
|
13 |
+
def generate_survey(data):
|
14 |
+
# 将数据转换为Markdown格式
|
15 |
+
markdown_text = "\n".join([f"- {item}" for item in data])
|
|
|
|
|
|
|
16 |
|
17 |
+
# 创建Gradio界面
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
gr.Markdown("## 问卷调查")
|
20 |
+
gr.Markdown(markdown_text)
|
21 |
+
|
22 |
+
# 创建一个文本输入框用于打分
|
23 |
+
score = gr.Textbox(label="请打分 (1-5)", placeholder="输入1到5之间的数字")
|
24 |
+
|
25 |
+
# 创建一个提交按钮
|
26 |
+
submit_button = gr.Button("提交")
|
27 |
+
|
28 |
+
# 定义一个回调函数来处理提交
|
29 |
+
def submit(score):
|
30 |
+
try:
|
31 |
+
score = int(score)
|
32 |
+
if 1 <= score <= 5:
|
33 |
+
# 将结果保存到JSON文件
|
34 |
+
result = {"score": score}
|
35 |
+
with open("survey_result.json", "w") as f:
|
36 |
+
json.dump(result, f)
|
37 |
+
return "提交成功!"
|
38 |
+
else:
|
39 |
+
return "请输入1到5之间的数字。"
|
40 |
+
except ValueError:
|
41 |
+
return "请输入有效的数字。"
|
42 |
+
|
43 |
+
# 绑定提交按钮的回调函数
|
44 |
+
submit_button.click(submit, inputs=score, outputs="text")
|
45 |
|
46 |
+
return demo
|
47 |
|
48 |
+
# 主函数
|
49 |
+
def main():
|
50 |
+
# 读取Excel文件
|
51 |
+
file_path = "survey_data.xlsx" # 替换为你的Excel文件路径
|
52 |
+
data = read_excel(file_path)
|
53 |
|
54 |
+
# 生成问卷调查界面
|
55 |
+
demo = generate_survey(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# 启动Gradio应用
|
58 |
+
demo.launch()
|
59 |
|
|
|
60 |
if __name__ == "__main__":
|
61 |
+
main()
|
|