MIAO HU commited on
Commit
5dcd53e
·
1 Parent(s): 077f094

init version

Browse files
Files changed (5) hide show
  1. .gitignore +184 -0
  2. README.md +10 -3
  3. app.py +204 -0
  4. config.py +27 -0
  5. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+ .DS_Store
139
+
140
+ # Spyder project settings
141
+ .spyderproject
142
+ .spyproject
143
+
144
+ # Rope project settings
145
+ .ropeproject
146
+
147
+ # mkdocs documentation
148
+ /site
149
+
150
+ # Pyre type checker
151
+ .pyre/
152
+
153
+ # pytype static type analyzer
154
+ .pytype/
155
+
156
+ # Cython debug symbols
157
+ cython_debug/
158
+
159
+ # PyCharm
160
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
163
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
164
+ #.idea/
165
+
166
+ # Visual Studio Code
167
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
168
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
169
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
170
+ # you could uncomment the following to ignore the enitre vscode folder
171
+ # .vscode/
172
+
173
+ # Ruff stuff:
174
+ .ruff_cache/
175
+
176
+ # PyPI configuration file
177
+ .pypirc
178
+
179
+ # Cursor
180
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
181
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
182
+ # refer to https://docs.cursor.com/context/ignore-files
183
+ .cursorignore
184
+ .cursorindexingignore
README.md CHANGED
@@ -1,12 +1,19 @@
1
  ---
2
- title: Mcp Parakeet Tdt 0.6b V2
3
- emoji: 🐢
4
  colorFrom: indigo
5
  colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.31.0
8
  app_file: app.py
9
  pinned: false
 
 
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
1
  ---
2
+ title: Parakeet ASR MCP Server
3
+ emoji: 🎧
4
  colorFrom: indigo
5
  colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.31.0
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
+ models:
12
+ - nvidia/parakeet-tdt-0.6b-v2
13
  ---
14
 
15
+ # 🎙️ Parakeet ASR MCP Server
16
+
17
+ A Model Context Protocol (MCP) server built with Gradio interfaces with a speech-to-text API, serving the model [Parakeet TDT 0.6B V2](https://huggingface.co/nvidia/parakeet-tdt-0.6b-v2) open-sourced by NVIDIA and hosted on Novita AI (https://novita.ai/templates-library/105929).
18
+
19
+ This server is free to use and provides high-quality English transcription capabilities, supporting both plain text and SRT subtitle formats.
app.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import httpx
3
+ import asyncio
4
+ from typing import Dict, Any
5
+ import os
6
+ from config import CONFIG
7
+
8
+ # API endpoint configuration from config
9
+ API_BASE_URL = CONFIG["api"]["base_url"]
10
+ API_TIMEOUT = CONFIG["api"]["timeout"]
11
+
12
+ if API_BASE_URL is None:
13
+ raise ValueError("API_BASE_URL is not set")
14
+
15
+ async def transcribe_audio(audio_file: str, output_format: str = "text") -> Dict[str, Any]:
16
+ """
17
+ Transcribe the audio file to text or SRT subtitles.
18
+
19
+ Args:
20
+ audio_file (str): Path to the audio file to transcribe
21
+ output_format (str): Output format - "text" for plain text, "srt" for SRT subtitles
22
+
23
+ Returns:
24
+ Dict containing the transcription result
25
+ """
26
+
27
+ try:
28
+ # Determine the endpoint based on output format
29
+ if output_format == "srt":
30
+ endpoint = f"{API_BASE_URL}{CONFIG['api']['endpoints']['transcribe_srt']}"
31
+ else:
32
+ endpoint = f"{API_BASE_URL}{CONFIG['api']['endpoints']['transcribe']}"
33
+
34
+ # Prepare the file for upload
35
+ async with httpx.AsyncClient(timeout=API_TIMEOUT) as client:
36
+ with open(audio_file, "rb") as f:
37
+ files = {"file": (os.path.basename(audio_file), f, "audio/wav")}
38
+
39
+ response = await client.post(endpoint, files=files)
40
+ response.raise_for_status()
41
+
42
+ if output_format == "srt":
43
+ # For SRT format, return the raw text content
44
+ return {
45
+ "success": True,
46
+ "transcription": response.text,
47
+ "format": "srt"
48
+ }
49
+ else:
50
+ # For JSON format, parse the response
51
+ result = response.json()
52
+
53
+ # Handle both old format (direct text) and new format (segments array)
54
+ transcription_text = ""
55
+ if "success" in result and result["success"] is True and "segments" in result and result["segments"]:
56
+ # New format with segments array - include timestamps
57
+ formatted_segments = []
58
+ for segment in result["segments"]:
59
+ text = segment.get("text", "")
60
+ formatted_segments.append(f"{text}")
61
+ transcription_text = "\n".join(formatted_segments)
62
+ else:
63
+ transcription_text = "No transcription text found in response"
64
+
65
+ return {
66
+ "success": True,
67
+ "transcription": transcription_text,
68
+ "format": "text",
69
+ "metadata": result
70
+ }
71
+
72
+ except httpx.TimeoutException:
73
+ return {
74
+ "success": False,
75
+ "error": "Request timed out. The audio file might be too long or the server is busy.",
76
+ "format": output_format
77
+ }
78
+ except httpx.HTTPStatusError as e:
79
+ return {
80
+ "success": False,
81
+ "error": f"HTTP error {e.response.status_code}: {e.response.text}",
82
+ "format": output_format
83
+ }
84
+ except Exception as e:
85
+ return {
86
+ "success": False,
87
+ "error": f"Unexpected error: {str(e)}",
88
+ "format": output_format
89
+ }
90
+
91
+ def transcribe_audio_sync(audio_file: str, output_format: str = "text") -> Dict[str, Any]:
92
+ """
93
+ Synchronous wrapper for the async transcribe function.
94
+ """
95
+ return asyncio.run(transcribe_audio(audio_file, output_format))
96
+
97
+ def transcribe_to_text(audio_file: str) -> str:
98
+ """
99
+ Transcribe the audio file to plain text.
100
+
101
+ Args:
102
+ audio_file (str): The URL to the audio file.
103
+
104
+ Returns:
105
+ str: Transcribed text
106
+ """
107
+ if not audio_file:
108
+ return "Please provide an audio file."
109
+
110
+ result = transcribe_audio_sync(audio_file, "text")
111
+
112
+ if result["success"]:
113
+ return result["transcription"]
114
+ else:
115
+ return f"Error: {result['error']}"
116
+
117
+ def transcribe_to_srt(audio_file: str) -> str:
118
+ """
119
+ Transcribe the audio file to SRT subtitle format.
120
+
121
+ Args:
122
+ audio_file (str): The URL to the audio file.
123
+
124
+ Returns:
125
+ str: SRT formatted subtitles
126
+ """
127
+
128
+ if not audio_file:
129
+ return "Please provide an audio file."
130
+
131
+ result = transcribe_audio_sync(audio_file, "srt")
132
+
133
+ if result["success"]:
134
+ return result["transcription"]
135
+ else:
136
+ return f"Error: {result['error']}"
137
+
138
+ # Create the Gradio interface
139
+ with gr.Blocks(title="Parakeet ASR MCP Server") as demo:
140
+ gr.Markdown("""
141
+ # 🎙️ Parakeet ASR MCP Server
142
+
143
+ A Model Context Protocol (MCP) server built with Gradio interfaces with a speech-to-text API, serving the model [Parakeet TDT 0.6B V2](https://huggingface.co/nvidia/parakeet-tdt-0.6b-v2) open-sourced by NVIDIA and hosted on Novita AI (https://novita.ai/templates-library/105929).
144
+
145
+ This server is free to use and provides high-quality English transcription capabilities, supporting both plain text and SRT subtitle formats.
146
+ """)
147
+
148
+ with gr.Tab("Transcribe to text"):
149
+ with gr.Row():
150
+ with gr.Column():
151
+ audio_input_text = gr.Audio(
152
+ label="Upload Audio File",
153
+ type="filepath",
154
+ sources=["upload", "microphone"]
155
+ )
156
+ transcribe_text_btn = gr.Button("Transcribe to Text", variant="primary")
157
+
158
+ with gr.Column():
159
+ text_output = gr.Textbox(
160
+ label="Transcription Result",
161
+ lines=10,
162
+ placeholder="Transcribed text will appear here..."
163
+ )
164
+
165
+ transcribe_text_btn.click(
166
+ fn=transcribe_to_text,
167
+ inputs=[audio_input_text],
168
+ outputs=[text_output]
169
+ )
170
+
171
+ with gr.Tab("Transcribe to SRT Subtitles"):
172
+ with gr.Row():
173
+ with gr.Column():
174
+ audio_input_srt = gr.Audio(
175
+ label="Upload Audio File",
176
+ type="filepath",
177
+ sources=["upload", "microphone"]
178
+ )
179
+ transcribe_srt_btn = gr.Button("Transcribe to SRT", variant="primary")
180
+
181
+ with gr.Column():
182
+ srt_output = gr.Textbox(
183
+ label="SRT Subtitles",
184
+ lines=15,
185
+ placeholder="SRT formatted subtitles will appear here..."
186
+ )
187
+
188
+ transcribe_srt_btn.click(
189
+ fn=transcribe_to_srt,
190
+ inputs=[audio_input_srt],
191
+ outputs=[srt_output]
192
+ )
193
+
194
+ if __name__ == "__main__":
195
+ # Launch with MCP server enabled
196
+ try:
197
+ demo.launch(
198
+ mcp_server=True,
199
+ share=False,
200
+ server_name=CONFIG["server"]["host"],
201
+ server_port=CONFIG["server"]["port"],
202
+ )
203
+ except Exception as e:
204
+ print(f"Error launching server: {e}")
config.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Configuration settings for the ASR (Automatic Speech Recognition) MCP Server.
3
+ """
4
+ import os
5
+
6
+ # API Configuration
7
+ API_CONFIG = {
8
+ "base_url": os.getenv("API_BASE_URL", ""),
9
+ "endpoints": {
10
+ "transcribe": "/transcribe",
11
+ "transcribe_srt": "/transcribe/srt"
12
+ },
13
+ "timeout": 300.0,
14
+ "max_retries": None
15
+ }
16
+
17
+ # Server Configuration
18
+ SERVER_CONFIG = {
19
+ "host": "0.0.0.0",
20
+ "port": 7860,
21
+ }
22
+
23
+ # Export the configuration
24
+ CONFIG = {
25
+ "api": API_CONFIG,
26
+ "server": SERVER_CONFIG
27
+ }
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio[mcp]>=5.31.0
2
+ httpx>=0.25.0