Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,19 +57,15 @@ def process_audio_input(audio):
|
|
| 57 |
print(f"Error processing audio: {e}")
|
| 58 |
return None
|
| 59 |
|
| 60 |
-
@spaces.GPU(duration=
|
| 61 |
-
def process_audio_text(audio):
|
| 62 |
-
"""主要处理函数"""
|
| 63 |
global _MODEL_ON_CUDA, inference_model
|
| 64 |
|
| 65 |
-
# 初始化模型(如果还没初始化)
|
| 66 |
if inference_model is None:
|
| 67 |
init_model()
|
| 68 |
|
| 69 |
-
# 首次使用GPU时移动模型
|
| 70 |
if not _MODEL_ON_CUDA:
|
| 71 |
try:
|
| 72 |
-
# 将模型移动到GPU
|
| 73 |
if hasattr(inference_model, 'model'):
|
| 74 |
inference_model.model = inference_model.model.to("cuda")
|
| 75 |
if hasattr(inference_model, 'unit_translator'):
|
|
@@ -81,11 +77,8 @@ def process_audio_text(audio):
|
|
| 81 |
except Exception as e:
|
| 82 |
print(f"Error moving model to GPU: {e}")
|
| 83 |
|
| 84 |
-
# 处理音频输入
|
| 85 |
audio_path = process_audio_input(audio)
|
| 86 |
|
| 87 |
-
text = ""
|
| 88 |
-
|
| 89 |
tmp = [{
|
| 90 |
"conversations": [
|
| 91 |
{
|
|
@@ -111,26 +104,25 @@ def process_audio_text(audio):
|
|
| 111 |
except Exception as e:
|
| 112 |
yield None, f"Error: {str(e)}"
|
| 113 |
finally:
|
| 114 |
-
# 清理临时文件
|
| 115 |
if audio_path and audio_path != audio and os.path.exists(audio_path):
|
| 116 |
try:
|
| 117 |
os.unlink(audio_path)
|
| 118 |
except:
|
| 119 |
pass
|
| 120 |
|
| 121 |
-
# 初始化模型(在CPU上)
|
| 122 |
init_model()
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
examples = [
|
| 126 |
-
["./show_case/1.wav"],
|
| 127 |
-
["./show_case/2.wav"],
|
| 128 |
]
|
| 129 |
|
| 130 |
iface = gr.Interface(
|
| 131 |
fn=process_audio_text,
|
| 132 |
inputs=[
|
| 133 |
-
gr.
|
|
|
|
| 134 |
],
|
| 135 |
outputs=[
|
| 136 |
gr.Audio(label="Streamed Audio", streaming=True, autoplay=True),
|
|
|
|
| 57 |
print(f"Error processing audio: {e}")
|
| 58 |
return None
|
| 59 |
|
| 60 |
+
@spaces.GPU(duration=120)
|
| 61 |
+
def process_audio_text(text, audio):
|
|
|
|
| 62 |
global _MODEL_ON_CUDA, inference_model
|
| 63 |
|
|
|
|
| 64 |
if inference_model is None:
|
| 65 |
init_model()
|
| 66 |
|
|
|
|
| 67 |
if not _MODEL_ON_CUDA:
|
| 68 |
try:
|
|
|
|
| 69 |
if hasattr(inference_model, 'model'):
|
| 70 |
inference_model.model = inference_model.model.to("cuda")
|
| 71 |
if hasattr(inference_model, 'unit_translator'):
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
print(f"Error moving model to GPU: {e}")
|
| 79 |
|
|
|
|
| 80 |
audio_path = process_audio_input(audio)
|
| 81 |
|
|
|
|
|
|
|
| 82 |
tmp = [{
|
| 83 |
"conversations": [
|
| 84 |
{
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
yield None, f"Error: {str(e)}"
|
| 106 |
finally:
|
|
|
|
| 107 |
if audio_path and audio_path != audio and os.path.exists(audio_path):
|
| 108 |
try:
|
| 109 |
os.unlink(audio_path)
|
| 110 |
except:
|
| 111 |
pass
|
| 112 |
|
|
|
|
| 113 |
init_model()
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
examples = [
|
| 117 |
+
["Provide a short answer.", "./show_case/1.wav"],
|
| 118 |
+
["", "./show_case/2.wav"],
|
| 119 |
]
|
| 120 |
|
| 121 |
iface = gr.Interface(
|
| 122 |
fn=process_audio_text,
|
| 123 |
inputs=[
|
| 124 |
+
gr.Textbox(label="Input Text", value=examples[0][0]),
|
| 125 |
+
gr.Audio(type="filepath", label="Upload Audio", value=examples[0][1])
|
| 126 |
],
|
| 127 |
outputs=[
|
| 128 |
gr.Audio(label="Streamed Audio", streaming=True, autoplay=True),
|