mzltest commited on
Commit
8dd7f4b
·
1 Parent(s): c8f3c37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -35
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import os,secrets,requests
3
  from basic_pitch.inference import predict_and_save
4
  from urllib.parse import quote
5
- import deta
6
  from deta import Deta # Import Deta
7
 
8
  # Initialize
@@ -36,40 +36,42 @@ def addrecord(orig,pitmp3,mid,gname,ghint):
36
 
37
 
38
  def downloadaud(video_id,gname,ghint):
39
- filename=secrets.token_hex()
40
- # Step 1: Search for videos with the given query
41
-
42
- if not video_id:
43
- return None
44
-
45
- # Step 3: Get the formats for the video and find the URL for the best audio-only format
46
- formats_url = f"https://draw-8fj.begin.app/api/info/{video_id}"
47
- formats = requests.get(formats_url)
48
- if formats.ok!=True:
49
- formats_url = f"https://draw-8fj-staging.begin.app/api/info/{video_id}"
50
- formats = requests.get(formats_url)
51
- formats_response=formats.json()
52
- #print(formats_response["formats"])
53
- best_audio_format = None
54
- for fmt in formats_response.get("formats", []):
55
- if fmt.get("hasVideo") is False and fmt.get("hasAudio") is True and fmt.get("container") == "mp4":
56
- if not best_audio_format or fmt.get("audioBitrate") > best_audio_format.get("audioBitrate"):
57
- best_audio_format = fmt
58
- print(best_audio_format)
59
- # If no suitable audio format was found, return None
60
- if not best_audio_format:
61
- return None
62
-
63
- aurl=best_audio_format["url"]
64
- print(aurl)
65
- command = f'./pget -o {filename}.mp4 -p 4 "{aurl}" '
66
- os.system(command)
67
- os.system(f'ffmpeg -i {filename}.mp4 -q:a 0 -map a {filename}.mp3')
68
- os.system(f'mv {filename}.mp3 /tmp/{filename}.mp3')
69
- o,m,i= predict(f'/tmp/{filename}.mp3')
70
- res=addrecord(o,m,i,gname,ghint)
71
- return o,m,i,res
72
-
 
 
73
 
74
  def predict(ina):
75
  fn=ina
 
2
  import os,secrets,requests
3
  from basic_pitch.inference import predict_and_save
4
  from urllib.parse import quote
5
+ import traceback
6
  from deta import Deta # Import Deta
7
 
8
  # Initialize
 
36
 
37
 
38
  def downloadaud(video_id,gname,ghint):
39
+ try:
40
+ filename=secrets.token_hex()
41
+ # Step 1: Search for videos with the given query
42
+
43
+ if not video_id:
44
+ return None
45
+
46
+ # Step 3: Get the formats for the video and find the URL for the best audio-only format
47
+ formats_url = f"https://draw-8fj.begin.app/api/info/{video_id}"
48
+ formats = requests.get(formats_url)
49
+ if formats.ok!=True:
50
+ formats_url = f"https://draw-8fj-staging.begin.app/api/info/{video_id}"
51
+ formats = requests.get(formats_url)
52
+ formats_response=formats.json()
53
+ #print(formats_response["formats"])
54
+ best_audio_format = None
55
+ for fmt in formats_response.get("formats", []):
56
+ if fmt.get("hasVideo") is False and fmt.get("hasAudio") is True and fmt.get("container") == "mp4":
57
+ if not best_audio_format or fmt.get("audioBitrate") > best_audio_format.get("audioBitrate"):
58
+ best_audio_format = fmt
59
+ print(best_audio_format)
60
+ # If no suitable audio format was found, return None
61
+ if not best_audio_format:
62
+ return None
63
+
64
+ aurl=best_audio_format["url"]
65
+ print(aurl)
66
+ command = f'./pget -o {filename}.mp4 -p 4 "{aurl}" '
67
+ os.system(command)
68
+ os.system(f'ffmpeg -i {filename}.mp4 -q:a 0 -map a {filename}.mp3')
69
+ os.system(f'mv {filename}.mp3 /tmp/{filename}.mp3')
70
+ o,m,i= predict(f'/tmp/{filename}.mp3')
71
+ res=addrecord(o,m,i,gname,ghint)
72
+ return o,m,i,res
73
+ except Exception as e:
74
+ print(traceback(e))
75
 
76
  def predict(ina):
77
  fn=ina