Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
import pandas as pd
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if not os.path.exists(default_excel_path):
|
| 41 |
-
return "預設的 Excel 文件不存在。"
|
| 42 |
-
try:
|
| 43 |
-
# 使用 pandas 讀取 Excel 文件
|
| 44 |
-
df = pd.read_excel(default_excel_path)
|
| 45 |
-
# 提取 "Output" 和 "Initial Output" 列並返回第一行
|
| 46 |
-
output_first_line = parse_markdown(str(df["Output"].iloc[0])) if "Output" in df.columns else "Output 列不存在。"
|
| 47 |
-
initial_output_first_line = parse_markdown(str(df["Initial Output"].iloc[0])) if "Initial Output" in df.columns else "Initial Output 列不存在。"
|
| 48 |
-
return f"Output 第一行: {output_first_line}\nInitial Output 第一行: {initial_output_first_line}"
|
| 49 |
-
except Exception as e:
|
| 50 |
-
return f"處理文件時發生錯誤:{str(e)}"
|
| 51 |
-
|
| 52 |
-
# Gradio 界面
|
| 53 |
-
def main(password, file):
|
| 54 |
-
if password: # 如果輸入了密碼,進入管理員模式
|
| 55 |
-
return admin_mode(password, file)
|
| 56 |
-
else: # 否則進入用戶模式
|
| 57 |
-
return user_mode()
|
| 58 |
-
|
| 59 |
-
# 設置 Gradio 介面
|
| 60 |
-
iface = gr.Interface(
|
| 61 |
-
fn=main,
|
| 62 |
-
inputs=[
|
| 63 |
-
gr.Textbox(label="管理員密碼", placeholder="輸入密碼以進入管理員模式(可選)"),
|
| 64 |
-
gr.File(label="上傳 Excel 文件(僅管理員模式需要)"),
|
| 65 |
-
],
|
| 66 |
-
outputs=gr.Textbox(label="分析結果"),
|
| 67 |
-
title="問卷調查系統",
|
| 68 |
-
description="輸入密碼以進入管理員模式,或留空以進入用戶模式。管理員模式可上傳自定義的 Excel 文件,用戶模式會使用預設的 Excel 文件進行分析。",
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
# 啟動應用
|
| 72 |
-
iface.launch()
|
|
|
|
| 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].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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|