ryanlinjui commited on
Commit
2de302a
1 Parent(s): 8656350

feat: txt file support

Browse files
Files changed (2) hide show
  1. app.py +30 -5
  2. taiko.py +1 -1
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import json
2
  import gradio as gr
3
  import numpy as np
4
 
@@ -8,11 +8,36 @@ from taiko import (
8
  COURSE
9
  )
10
 
11
- def handle(chart_path, music_path):
12
- data = json.loads(open(chart_path, "r").read())
 
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  if len(data["data"]) < 0 and len(data["data"]) > 5:
15
- raise("Issue occur: Json data")
16
 
17
  for d in data["data"]:
18
  chart = preprocess(d["chart"])
@@ -31,7 +56,7 @@ def handle(chart_path, music_path):
31
 
32
  if __name__ == "__main__":
33
  inputs = [
34
- gr.File(label="太鼓達人譜面/Taiko Chart Data (json)", file_types=[".json"]),
35
  gr.File(label="譜面音樂/Chart Music (ogg) (Optional)", file_types=[".ogg"])
36
  ]
37
  outputs = [gr.Audio(label=course["label"], format="mp3") for course in COURSE]
 
1
+ import json, re
2
  import gradio as gr
3
  import numpy as np
4
 
 
8
  COURSE
9
  )
10
 
11
+ def txt_loads(data_str:str):
12
+ data = []
13
+ current_course = None
14
+ chart = []
15
 
16
+ for line in data_str.split('\n'):
17
+ if line.startswith("course:"):
18
+ if current_course is not None:
19
+ data.append({"course": current_course, "chart": chart})
20
+ chart = []
21
+ current_course = int(re.search(r'\d+', line).group())
22
+ elif line:
23
+ line = line.strip("[]")
24
+ nums = line.split(",")
25
+ chart.append([int(nums[0]), float(nums[1])])
26
+
27
+ if current_course is not None:
28
+ data.append({"course": current_course, "chart": chart})
29
+
30
+ output_data = {"data": data}
31
+ return output_data
32
+
33
+ def handle(chart_path:str, music_path:str):
34
+ if chart_path.endswith(".json"):
35
+ data = json.loads(open(chart_path, "r").read())
36
+ elif chart_path.endswith(".txt"):
37
+ data = txt_loads(open(chart_path, "r").read())
38
+
39
  if len(data["data"]) < 0 and len(data["data"]) > 5:
40
+ raise("Issue occur: chart data")
41
 
42
  for d in data["data"]:
43
  chart = preprocess(d["chart"])
 
56
 
57
  if __name__ == "__main__":
58
  inputs = [
59
+ gr.File(label="太鼓達人譜面/Taiko Chart Data (json or txt)", file_types=[".json", ".txt"]),
60
  gr.File(label="譜面音樂/Chart Music (ogg) (Optional)", file_types=[".ogg"])
61
  ]
62
  outputs = [gr.Audio(label=course["label"], format="mp3") for course in COURSE]
taiko.py CHANGED
@@ -54,7 +54,7 @@ def preprocess(data:list, offset:float=0):
54
  if balloon_count >= m[3]:
55
  chart.append((BALLOON_BANG_WAV, offset + m[1]))
56
  else:
57
- raise ValueError("Your json file has some problems.")
58
 
59
  return chart
60
 
 
54
  if balloon_count >= m[3]:
55
  chart.append((BALLOON_BANG_WAV, offset + m[1]))
56
  else:
57
+ raise ValueError("Your chart file has some problems.")
58
 
59
  return chart
60