Blake commited on
Commit
79f008a
1 Parent(s): 102d880

Added error handling

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -2,41 +2,46 @@ import gradio as gr
2
  import replicate
3
 
4
  def generate_music(genre, instruments, tempo, mood, duration, key_scale):
5
- # Creating a more detailed prompt based on the additional options
6
  prompt = f"Generate a {genre} track featuring {instruments}, in a {mood} mood, with a consistent melody throughout, with a tempo of {tempo} BPM, in the key of {key_scale}."
7
- # Assuming replicate.run() is a placeholder for actual music generation API call
8
- # Please replace with the actual function call to generate music
9
- output = replicate.run(
10
- "bamartin1618/weather-channel:77dae70c23138abd20014ba686f62da780c5aed738e00085ce698eb5e344c65a",
11
- input={
12
- "top_k": 250,
13
- "top_p": 1,
14
- "prompt": prompt,
15
- "duration": duration,
16
- "temperature": 1,
17
- "continuation": False,
18
- "output_format": "wav",
19
- "continuation_start": 0,
20
- "multi_band_diffusion": False,
21
- "normalization_strategy": "loudness",
22
- "classifier_free_guidance": 6
23
- }
24
- )
25
-
 
 
26
  if output:
27
  return f'<a href="{output}" target="_blank">Click here to listen to the generated music</a>'
28
  else:
29
  return 'Music generation failed or no URL returned.'
30
 
31
  def get_audio_path(selection):
32
- # Map selection to your audio file URLs
33
  audio_files = {
34
  "Smooth Jazz Evening": "smooth_jazz_evening.wav",
35
  "Uplifting Soft Rock": "uplifting_soft_rock.wav",
36
  "Mellow Jazz Morning": "mellow_jazz_morning.wav",
37
  "Soft Rock Serenade": "soft_rock_serenade.wav",
38
  }
39
- return audio_files[selection]
 
 
 
 
 
40
 
41
  with gr.Blocks() as app:
42
  with gr.Tab("Generate Music"):
 
2
  import replicate
3
 
4
  def generate_music(genre, instruments, tempo, mood, duration, key_scale):
 
5
  prompt = f"Generate a {genre} track featuring {instruments}, in a {mood} mood, with a consistent melody throughout, with a tempo of {tempo} BPM, in the key of {key_scale}."
6
+ try:
7
+ # Assuming replicate.run() is a placeholder for actual music generation API call
8
+ output = replicate.run(
9
+ "bamartin1618/weather-channel:77dae70c23138abd20014ba686f62da780c5aed738e00085ce698eb5e344c65a",
10
+ input={
11
+ "top_k": 250,
12
+ "top_p": 1,
13
+ "prompt": prompt,
14
+ "duration": duration,
15
+ "temperature": 1,
16
+ "continuation": False,
17
+ "output_format": "wav",
18
+ "continuation_start": 0,
19
+ "multi_band_diffusion": False,
20
+ "normalization_strategy": "loudness",
21
+ "classifier_free_guidance": 6
22
+ }
23
+ )
24
+ except Exception as e: # Consider specifying more specific exception types
25
+ return f'An error occurred during music generation: {str(e)}'
26
+
27
  if output:
28
  return f'<a href="{output}" target="_blank">Click here to listen to the generated music</a>'
29
  else:
30
  return 'Music generation failed or no URL returned.'
31
 
32
  def get_audio_path(selection):
 
33
  audio_files = {
34
  "Smooth Jazz Evening": "smooth_jazz_evening.wav",
35
  "Uplifting Soft Rock": "uplifting_soft_rock.wav",
36
  "Mellow Jazz Morning": "mellow_jazz_morning.wav",
37
  "Soft Rock Serenade": "soft_rock_serenade.wav",
38
  }
39
+ # Check if the selection exists in the dictionary to avoid KeyError
40
+ if selection in audio_files:
41
+ return audio_files[selection]
42
+ else:
43
+ # Handle unexpected selection
44
+ return 'Error: Selected audio file does not exist.'
45
 
46
  with gr.Blocks() as app:
47
  with gr.Tab("Generate Music"):