Spaces:
Sleeping
Sleeping
simple demo
Browse files- app.py +124 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
from filelock import FileLock
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# 定义评价标准
|
| 9 |
+
CRITERIA = ["画质清晰度", "物理真实感", "流畅度", "色彩表现", "整体观感"]
|
| 10 |
+
|
| 11 |
+
# 数据保存路径
|
| 12 |
+
RESULTS_FILE = "results.json"
|
| 13 |
+
LOCK_FILE = "results.json.lock"
|
| 14 |
+
|
| 15 |
+
def save_result(left_video, right_video, selections):
|
| 16 |
+
"""保存用户的评价结果到文件,使用 FileLock 确保并发安全"""
|
| 17 |
+
result = {
|
| 18 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 19 |
+
"left_video": left_video,
|
| 20 |
+
"right_video": right_video,
|
| 21 |
+
"selections": selections
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# 确保文件存在
|
| 25 |
+
if not os.path.exists(RESULTS_FILE):
|
| 26 |
+
with open(RESULTS_FILE, 'w') as f:
|
| 27 |
+
json.dump([], f)
|
| 28 |
+
|
| 29 |
+
# 使用 FileLock 保证并发安全
|
| 30 |
+
lock = FileLock(LOCK_FILE, timeout=60) # 设置 60 秒超时
|
| 31 |
+
try:
|
| 32 |
+
with lock:
|
| 33 |
+
# 读取现有数据
|
| 34 |
+
with open(RESULTS_FILE, 'r') as f:
|
| 35 |
+
data = json.load(f)
|
| 36 |
+
|
| 37 |
+
# 添加新结果
|
| 38 |
+
data.append(result)
|
| 39 |
+
|
| 40 |
+
# 写入更新后的数据
|
| 41 |
+
with open(RESULTS_FILE, 'w') as f:
|
| 42 |
+
json.dump(data, f, indent=2)
|
| 43 |
+
|
| 44 |
+
return True
|
| 45 |
+
except Exception as e:
|
| 46 |
+
# 如果发生异常,尝试使用备用方法保存
|
| 47 |
+
try:
|
| 48 |
+
backup_file = f"backup_{int(time.time())}.json"
|
| 49 |
+
with open(backup_file, 'w') as bf:
|
| 50 |
+
json.dump([result], bf, indent=2)
|
| 51 |
+
return True
|
| 52 |
+
except:
|
| 53 |
+
return False
|
| 54 |
+
|
| 55 |
+
def submit_evaluation(left_video, right_video, *selections):
|
| 56 |
+
"""处理用户提交的评价"""
|
| 57 |
+
selections_dict = {}
|
| 58 |
+
for i, criterion in enumerate(CRITERIA):
|
| 59 |
+
selections_dict[criterion] = "左侧" if selections[i] == "左侧视频更好" else "右侧"
|
| 60 |
+
|
| 61 |
+
success = save_result(left_video, right_video, selections_dict)
|
| 62 |
+
|
| 63 |
+
if success:
|
| 64 |
+
return "感谢您的评价!您的反馈已成功保存。"
|
| 65 |
+
else:
|
| 66 |
+
return "保存失败,请稍后再试。"
|
| 67 |
+
|
| 68 |
+
def create_ui():
|
| 69 |
+
"""创建Gradio界面"""
|
| 70 |
+
with gr.Blocks(css="""
|
| 71 |
+
.container {max-width: 1200px; margin: 0 auto;}
|
| 72 |
+
.header {text-align: center; margin-bottom: 20px;}
|
| 73 |
+
.videos {display: flex; gap: 20px;}
|
| 74 |
+
.video-container {flex: 1; text-align: center;}
|
| 75 |
+
.criteria {margin-top: 30px;}
|
| 76 |
+
.criterion {margin-bottom: 15px; padding: 10px; background: #f7f7f7; border-radius: 8px;}
|
| 77 |
+
.submit-btn {margin-top: 30px;}
|
| 78 |
+
footer {margin-top: 50px; text-align: center; font-size: 0.8em; color: #666;}
|
| 79 |
+
""") as demo:
|
| 80 |
+
gr.Markdown("# 视频比较评价系统", elem_classes=["header"])
|
| 81 |
+
gr.Markdown("### 请观看下方两个视频,并根据各项标准选择您认为更好的一个", elem_classes=["header"])
|
| 82 |
+
|
| 83 |
+
with gr.Row(elem_classes=["videos"]):
|
| 84 |
+
with gr.Column(elem_classes=["video-container"]):
|
| 85 |
+
gr.Markdown("### 视频 A")
|
| 86 |
+
left_video = gr.Video(label="左侧视频")
|
| 87 |
+
|
| 88 |
+
with gr.Column(elem_classes=["video-container"]):
|
| 89 |
+
gr.Markdown("### 视频 B")
|
| 90 |
+
right_video = gr.Video(label="右侧视频")
|
| 91 |
+
|
| 92 |
+
selections = []
|
| 93 |
+
with gr.Group(elem_classes=["criteria"]):
|
| 94 |
+
gr.Markdown("## 评价标准")
|
| 95 |
+
for criterion in CRITERIA:
|
| 96 |
+
with gr.Row(elem_classes=["criterion"]):
|
| 97 |
+
gr.Markdown(f"### {criterion}")
|
| 98 |
+
selection = gr.Radio(
|
| 99 |
+
["左侧视频更好", "右侧视频更好"],
|
| 100 |
+
label="您的选择",
|
| 101 |
+
info="请选择您认为在此标准下表现更好的视频"
|
| 102 |
+
)
|
| 103 |
+
selections.append(selection)
|
| 104 |
+
|
| 105 |
+
submit_btn = gr.Button("提交评价", variant="primary", elem_classes=["submit-btn"])
|
| 106 |
+
result = gr.Markdown()
|
| 107 |
+
|
| 108 |
+
submit_btn.click(
|
| 109 |
+
fn=submit_evaluation,
|
| 110 |
+
inputs=[left_video, right_video] + selections,
|
| 111 |
+
outputs=result
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
gr.Markdown(
|
| 115 |
+
"---\n"
|
| 116 |
+
"© 2023 视频比较评价系统 | 感谢您的参与",
|
| 117 |
+
elem_classes=["footer"]
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
return demo
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
demo = create_ui()
|
| 124 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.50.2
|
| 2 |
+
filelock>=3.0.12
|
| 3 |
+
numpy>=1.20.0
|
| 4 |
+
pillow>=8.0.0
|
| 5 |
+
pydub>=0.25.1
|
| 6 |
+
ffmpeg-python>=0.2.0
|
| 7 |
+
matplotlib>=3.4.0
|
| 8 |
+
pandas>=1.3.0
|