Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,14 @@ whisper_model = whisper.load_model("small")
|
|
| 11 |
|
| 12 |
def audio_prompt_to_chatgpt(audio_path):
|
| 13 |
if audio_path is None:
|
| 14 |
-
return ""
|
| 15 |
|
| 16 |
# 1οΈβ£ Transcribe audio β Chinese text
|
| 17 |
result = whisper_model.transcribe(audio_path, language="zh")
|
| 18 |
chinese_text = result.get("text", "").strip()
|
| 19 |
|
| 20 |
if not chinese_text:
|
| 21 |
-
return "β οΈ Could not transcribe audio."
|
| 22 |
|
| 23 |
# 2οΈβ£ ChatGPT reply in Chinese
|
| 24 |
try:
|
|
@@ -48,7 +48,14 @@ def audio_prompt_to_chatgpt(audio_path):
|
|
| 48 |
except Exception as e:
|
| 49 |
chatgpt_reply_english = f"β οΈ Translation failed: {e}"
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# π Gradio UI
|
| 54 |
with gr.Blocks() as app:
|
|
@@ -60,28 +67,20 @@ with gr.Blocks() as app:
|
|
| 60 |
label="ποΈ Speak Chinese or Upload Audio"
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
submit_btn = gr.Button("Generate Response")
|
| 68 |
|
| 69 |
-
# Copy buttons
|
| 70 |
-
copy_chinese_btn = gr.Button("Copy Chinese")
|
| 71 |
-
copy_english_btn = gr.Button("Copy English")
|
| 72 |
-
copy_transcription_btn = gr.Button("Copy Transcription")
|
| 73 |
-
|
| 74 |
# Connect submit button
|
| 75 |
submit_btn.click(
|
| 76 |
fn=audio_prompt_to_chatgpt,
|
| 77 |
inputs=audio_input,
|
| 78 |
-
outputs=
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# Connect copy buttons to copy respective outputs
|
| 82 |
-
copy_chinese_btn.click(lambda x: x, inputs=chinese_output, outputs=chinese_output)
|
| 83 |
-
copy_english_btn.click(lambda x: x, inputs=english_output, outputs=english_output)
|
| 84 |
-
copy_transcription_btn.click(lambda x: x, inputs=transcription_output, outputs=transcription_output)
|
| 85 |
-
|
| 86 |
if __name__ == "__main__":
|
| 87 |
app.launch()
|
|
|
|
| 11 |
|
| 12 |
def audio_prompt_to_chatgpt(audio_path):
|
| 13 |
if audio_path is None:
|
| 14 |
+
return ""
|
| 15 |
|
| 16 |
# 1οΈβ£ Transcribe audio β Chinese text
|
| 17 |
result = whisper_model.transcribe(audio_path, language="zh")
|
| 18 |
chinese_text = result.get("text", "").strip()
|
| 19 |
|
| 20 |
if not chinese_text:
|
| 21 |
+
return "β οΈ Could not transcribe audio."
|
| 22 |
|
| 23 |
# 2οΈβ£ ChatGPT reply in Chinese
|
| 24 |
try:
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
chatgpt_reply_english = f"β οΈ Translation failed: {e}"
|
| 50 |
|
| 51 |
+
# Combine everything in one cell
|
| 52 |
+
combined_output = (
|
| 53 |
+
f"πΆ **Transcribed Chinese Prompt:**\n{chinese_text}\n\n"
|
| 54 |
+
f"π€ **ChatGPT Response (Chinese):**\n{chatgpt_reply_chinese}\n\n"
|
| 55 |
+
f"π **ChatGPT Response (English):**\n{chatgpt_reply_english}"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
return combined_output
|
| 59 |
|
| 60 |
# π Gradio UI
|
| 61 |
with gr.Blocks() as app:
|
|
|
|
| 67 |
label="ποΈ Speak Chinese or Upload Audio"
|
| 68 |
)
|
| 69 |
|
| 70 |
+
output_box = gr.Textbox(
|
| 71 |
+
label="π Results (Copy Friendly)",
|
| 72 |
+
lines=15,
|
| 73 |
+
interactive=False
|
| 74 |
+
)
|
| 75 |
|
| 76 |
submit_btn = gr.Button("Generate Response")
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Connect submit button
|
| 79 |
submit_btn.click(
|
| 80 |
fn=audio_prompt_to_chatgpt,
|
| 81 |
inputs=audio_input,
|
| 82 |
+
outputs=output_box
|
| 83 |
)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
if __name__ == "__main__":
|
| 86 |
app.launch()
|