Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
def extract_timestamp(filename):
|
| 6 |
+
# 从文件名中提取数字时间戳
|
| 7 |
+
match = re.search(r'(\d+)', filename)
|
| 8 |
+
if match:
|
| 9 |
+
return int(match.group(1))
|
| 10 |
+
return 0
|
| 11 |
+
|
| 12 |
+
def csv_to_dict(df):
|
| 13 |
+
df = df.fillna('')
|
| 14 |
+
res = {}
|
| 15 |
+
for i, row in df.iterrows():
|
| 16 |
+
if row['CRN'] not in res:
|
| 17 |
+
res[ row['CRN' ] ] = {
|
| 18 |
+
'Instructor': row['Instructor'],
|
| 19 |
+
'Max': row['Max'],
|
| 20 |
+
}
|
| 21 |
+
return res
|
| 22 |
+
|
| 23 |
+
def compare_dicts(dict_a, dict_b):
|
| 24 |
+
# 遍历两个字典,查找Instructor或Max不同的CRN
|
| 25 |
+
changed = []
|
| 26 |
+
all_crns = set(dict_a.keys()).union(set(dict_b.keys()))
|
| 27 |
+
|
| 28 |
+
for crn in all_crns:
|
| 29 |
+
instructor_a = dict_a.get(crn, {}).get('Instructor', None)
|
| 30 |
+
instructor_b = dict_b.get(crn, {}).get('Instructor', None)
|
| 31 |
+
max_a = dict_a.get(crn, {}).get('Max', None)
|
| 32 |
+
max_b = dict_b.get(crn, {}).get('Max', None)
|
| 33 |
+
|
| 34 |
+
# 如果Instructor或Max不同,则记录CRN
|
| 35 |
+
if instructor_a != instructor_b:
|
| 36 |
+
msg = ' * CRN: {}, Instructor change: {} --> {}'.format(crn, instructor_a, instructor_b)
|
| 37 |
+
changed.append(msg)
|
| 38 |
+
if max_a != max_b:
|
| 39 |
+
msg = ' * CRN: {}, Max change: {} --> {}'.format(crn, max_a, max_b)
|
| 40 |
+
changed.append(msg)
|
| 41 |
+
|
| 42 |
+
return changed
|
| 43 |
+
|
| 44 |
+
def process_files(files):
|
| 45 |
+
if len(files) < 2:
|
| 46 |
+
return "Please upload at least 2 files."
|
| 47 |
+
|
| 48 |
+
# 根据文件名中的时间戳进行排序
|
| 49 |
+
files_sorted = sorted(files, key=lambda x: extract_timestamp(x.name))
|
| 50 |
+
|
| 51 |
+
result = []
|
| 52 |
+
|
| 53 |
+
# 对排序后的文件进行两两比较
|
| 54 |
+
for i in range(len(files_sorted) - 1):
|
| 55 |
+
df_older = pd.read_csv(files_sorted[i].name)
|
| 56 |
+
df_newer = pd.read_csv(files_sorted[i + 1].name)
|
| 57 |
+
|
| 58 |
+
dict_a = csv_to_dict(df_older)
|
| 59 |
+
dict_b = csv_to_dict(df_newer)
|
| 60 |
+
|
| 61 |
+
f1 = files_sorted[i].name.split('/')[-1]
|
| 62 |
+
f2 = files_sorted[i+1].name.split('/')[-1]
|
| 63 |
+
|
| 64 |
+
crns = compare_dicts(dict_a, dict_b)
|
| 65 |
+
if crns:
|
| 66 |
+
result.append("{} --> {} \n{}".format(f1, f2, "\n".join(crns)))
|
| 67 |
+
else:
|
| 68 |
+
result.append("{} --> {} \n * No change.".format(f1, f2))
|
| 69 |
+
|
| 70 |
+
return "\n".join(result)
|
| 71 |
+
|
| 72 |
+
# 使用Gradio构建界面
|
| 73 |
+
gr.Interface(
|
| 74 |
+
fn=process_files,
|
| 75 |
+
inputs=gr.Files(label="Upload CCSV files"),
|
| 76 |
+
outputs="text",
|
| 77 |
+
title="Course Schedule Tracker",
|
| 78 |
+
description="Upload multiple CSV files, sort them based on the timestamp in the filenames, compare two adjacent files at a time, and output the CRNs where the Instructor or Max has changed."
|
| 79 |
+
).launch()
|
| 80 |
+
|
| 81 |
+
|