fdbw commited on
Commit
31044dc
·
verified ·
1 Parent(s): 2783bbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -37
app.py CHANGED
@@ -1,44 +1,60 @@
1
- import gradio as gr
2
- import dashscope
3
- from dashscope.audio.asr import Recognition
4
- import os
5
 
6
-
7
- # 从HF Secrets读取密钥,不要硬编码写在这里
8
- DASHSCOPE_API_KEY = os.environ.get("DASHSCOPE_API_KEY")
9
- dashscope.api_key = DASHSCOPE_API_KEY
10
-
11
- def translate_text(text):
12
- resp = dashscope.Generation.call(
13
- model="qwen3.7‑flash",
14
- messages=[{"role":"user","content":f"把下面中文翻译成英文:{text}"}]
 
 
 
 
 
 
 
15
  )
16
- return resp.output.choices[0].message.content
17
-
18
- def process_audio(audio):
19
- if audio is None:
20
- return "", ""
21
- sample_rate, data = audio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  rec = Recognition(
23
  model="paraformer‑realtime‑v2",
24
  format="pcm",
25
  sample_rate=sample_rate,
26
  )
27
- rec.start()
28
- rec.send_audio_frame(data.tobytes())
29
- rec.stop()
30
- asr_text = rec.result.output.text
31
- trans_text = translate_text(asr_text)
32
- return asr_text, trans_text
33
-
34
- demo = gr.Blocks()
35
- with demo:
36
- gr.Markdown("# 同声传译(百炼ASR+Qwen3.7‑Flash)")
37
- audio_input = gr.Audio(sources=["microphone"], type="numpy")
38
- btn = gr.Button("识别+翻译")
39
- text_asr = gr.Textbox(label="识别原文")
40
- text_trans = gr.Textbox(label="翻译结果")
41
- btn.click(process_audio, inputs=[audio_input], outputs=[text_asr, text_trans])
42
-
43
- if __name__ == "__main__":
44
- demo.launch()
 
1
+ ===== Application Startup at 2026-08-15 08:45:57 =====
 
 
 
2
 
3
+ * Running on local URL: http://0.0.0.0:7860, with SSR ⚡ (Node proxy -> Python :7861)
4
+ * To create a public link, set `share=True` in `launch()`.
5
+ /usr/local/lib/python3.13/site-packages/gradio/routes.py:1368: StarletteDeprecationWarning: 'HTTP_422_UNPROCESSABLE_ENTITY' is deprecated. Use 'HTTP_422_UNPROCESSABLE_CONTENT' instead.
6
+ return await queue_join_helper(body, request, username)
7
+ /usr/local/lib/python3.13/site-packages/gradio/routes.py:1368: StarletteDeprecationWarning: 'HTTP_422_UNPROCESSABLE_ENTITY' is deprecated. Use 'HTTP_422_UNPROCESSABLE_CONTENT' instead.
8
+ return await queue_join_helper(body, request, username)
9
+ Exception ignored in: <function Recognition.__del__ at 0x7f788ec639c0>
10
+ Traceback (most recent call last):
11
+ File "/usr/local/lib/python3.13/site-packages/dashscope/audio/asr/recognition.py", line 197, in __del__
12
+ if self._running:
13
+ AttributeError: 'Recognition' object has no attribute '_running'
14
+ Traceback (most recent call last):
15
+ File "/usr/local/lib/python3.13/site-packages/gradio/queueing.py", line 867, in process_events
16
+ response = await route_utils.call_process_api(
17
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18
+ ...<5 lines>...
19
  )
20
+ ^
21
+ File "/usr/local/lib/python3.13/site-packages/gradio/route_utils.py", line 386, in call_process_api
22
+ output = await app.get_blocks().process_api(
23
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24
+ ...<11 lines>...
25
+ )
26
+ ^
27
+ File "/usr/local/lib/python3.13/site-packages/gradio/blocks.py", line 2277, in process_api
28
+ result = await self.call_function(
29
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
30
+ ...<8 lines>...
31
+ )
32
+ ^
33
+ File "/usr/local/lib/python3.13/site-packages/gradio/blocks.py", line 1654, in call_function
34
+ prediction = await anyio.to_thread.run_sync( # type: ignore
35
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36
+ fn, *processed_input, limiter=self.limiter
37
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38
+ )
39
+ ^
40
+ File "/usr/local/lib/python3.13/site-packages/anyio/to_thread.py", line 65, in run_sync
41
+ return await get_async_backend().run_sync_in_worker_thread(
42
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43
+ func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter
44
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45
+ )
46
+ ^
47
+ File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 2641, in run_sync_in_worker_thread
48
+ return await future
49
+ ^^^^^^^^^^^^
50
+ File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 1033, in run
51
+ result = context.run(func, *args)
52
+ File "/usr/local/lib/python3.13/site-packages/gradio/utils.py", line 1061, in wrapper
53
+ response = f(*args, **kwargs)
54
+ File "/app/app.py", line 22, in process_audio
55
  rec = Recognition(
56
  model="paraformer‑realtime‑v2",
57
  format="pcm",
58
  sample_rate=sample_rate,
59
  )
60
+ TypeError: Recognition.__init__() missing 1 required positional argument: 'callback'