MAZALA2024 commited on
Commit
93f33db
·
verified ·
1 Parent(s): 1b123c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -19
app.py CHANGED
@@ -7,8 +7,18 @@ from io import BytesIO
7
  import asyncio
8
  import os
9
  import tempfile
 
10
  from gradio.data_classes import FileData
11
 
 
 
 
 
 
 
 
 
 
12
  async def convert_tts(model_name, audio_file, slang_rate):
13
  if audio_file is None:
14
  return {"error": "No audio file uploaded."}, None
@@ -30,18 +40,23 @@ async def convert_tts(model_name, audio_file, slang_rate):
30
  if audio_output is None:
31
  return {"error": "No audio output generated"}, None
32
 
33
- # Save output to a file in Gradio's file directory
34
  output_filename = f"output_{os.urandom(4).hex()}.wav"
35
- output_path = os.path.join("outputs", output_filename)
36
- os.makedirs("outputs", exist_ok=True)
37
 
38
- if isinstance(audio_output, np.ndarray):
39
- wavfile.write(output_path, tgt_sr, audio_output)
40
- else:
41
- with open(output_path, "wb") as f:
42
- f.write(audio_output)
43
-
44
- return {"info": info}, output_path
 
 
 
 
 
 
45
 
46
  except Exception as e:
47
  print(f"Error in convert_tts: {str(e)}")
@@ -57,7 +72,7 @@ iface = gr.Interface(
57
  fn=convert_tts,
58
  inputs=[
59
  gr.Dropdown(choices=get_models(), label="Model", interactive=True),
60
- gr.Audio(label="Upload Audio", type="filepath", source="upload"),
61
  gr.Slider(minimum=0, maximum=1, step=0.01, label="Slang Rate"),
62
  ],
63
  outputs=[
@@ -65,20 +80,15 @@ iface = gr.Interface(
65
  gr.Audio(label="Converted Audio")
66
  ],
67
  title="Voice Conversion",
68
- examples=[
69
- ["mongolian6-male", "example.wav", 0.0],
70
- ]
71
  ).queue()
72
 
73
  # Launch the interface
74
  if __name__ == "__main__":
75
- # Create output directory
76
- os.makedirs("outputs", exist_ok=True)
77
-
78
  iface.launch(
79
  debug=True,
80
  show_error=True,
81
  max_threads=10,
82
- share=False, # Set to True if you want to create a public link
83
- root_path="/content" if os.path.exists("/content") else None
84
  )
 
7
  import asyncio
8
  import os
9
  import tempfile
10
+ from pathlib import Path
11
  from gradio.data_classes import FileData
12
 
13
+ # Create necessary directories
14
+ OUTPUTS_DIR = Path(__file__).parent / "outputs"
15
+ TEMP_DIR = Path(__file__).parent / "temp"
16
+ UPLOAD_DIR = Path(__file__).parent / "uploaded"
17
+
18
+ # Ensure directories exist
19
+ for directory in [OUTPUTS_DIR, TEMP_DIR, UPLOAD_DIR]:
20
+ directory.mkdir(exist_ok=True)
21
+
22
  async def convert_tts(model_name, audio_file, slang_rate):
23
  if audio_file is None:
24
  return {"error": "No audio file uploaded."}, None
 
40
  if audio_output is None:
41
  return {"error": "No audio output generated"}, None
42
 
43
+ # Save output to a file in the outputs directory
44
  output_filename = f"output_{os.urandom(4).hex()}.wav"
45
+ output_path = str(OUTPUTS_DIR / output_filename)
 
46
 
47
+ try:
48
+ if isinstance(audio_output, np.ndarray):
49
+ wavfile.write(output_path, tgt_sr, audio_output)
50
+ else:
51
+ with open(output_path, "wb") as f:
52
+ f.write(audio_output)
53
+
54
+ print(f"Saved output to: {output_path}")
55
+ return {"info": info}, output_path
56
+
57
+ except Exception as save_error:
58
+ print(f"Error saving output: {save_error}")
59
+ return {"error": f"Error saving output: {str(save_error)}"}, None
60
 
61
  except Exception as e:
62
  print(f"Error in convert_tts: {str(e)}")
 
72
  fn=convert_tts,
73
  inputs=[
74
  gr.Dropdown(choices=get_models(), label="Model", interactive=True),
75
+ gr.Audio(label="Upload Audio", source="upload"),
76
  gr.Slider(minimum=0, maximum=1, step=0.01, label="Slang Rate"),
77
  ],
78
  outputs=[
 
80
  gr.Audio(label="Converted Audio")
81
  ],
82
  title="Voice Conversion",
83
+ analytics_enabled=False,
84
+ cache_examples=False
 
85
  ).queue()
86
 
87
  # Launch the interface
88
  if __name__ == "__main__":
 
 
 
89
  iface.launch(
90
  debug=True,
91
  show_error=True,
92
  max_threads=10,
93
+ share=False # Set to True if you want to create a public link
 
94
  )