ELVISIO commited on
Commit
3c44397
·
verified ·
1 Parent(s): 76ead4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -43
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
- # Excel 文件中提取第一列的数据并解析 Markdown
7
- def extract_and_parse_markdown(excel_file):
8
- # 读取 Excel 文件
9
- df = pd.read_excel(excel_file)
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
- # Gradio 界面函数
20
- def questionnaire(question, option):
21
- # 将用户的选择记录到 JSON 文件中
22
- data = {
23
- "question": question,
24
- "selected_option": option
25
- }
26
 
27
- with open("responses.json", "a") as f:
28
- json.dump(data, f)
29
- f.write("\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- return f"您选择了: {option}"
32
 
33
- # 创建 Gradio 界面
34
- def create_interface():
35
- # Excel 文件中提取并解析 Markdown
36
- excel_file = "questions.xlsx" # 替换为你的 Excel 文件路径
37
- questions = extract_and_parse_markdown(excel_file)
38
 
39
- # 创建 Gradio 界面
40
- iface = gr.Interface(
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
- return iface
 
51
 
52
- # 启动 Gradio 应用
53
  if __name__ == "__main__":
54
- iface = create_interface()
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()