doctorx commited on
Commit
40f1ec3
β€’
1 Parent(s): 8ed7f28

Upload 12 files

Browse files
Dockerfile ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a PyTorch runtime with CUDA
2
+ FROM pytorch/pytorch:2.4.0-cuda12.1-cudnn9-runtime
3
+
4
+ # Set environment variables
5
+ ENV PYTHONUNBUFFERED=1
6
+ ENV TRANSFORMERS_CACHE=/app/models
7
+ ENV HF_HOME=/app/models
8
+
9
+ # Set the working directory
10
+ WORKDIR /app
11
+
12
+ # Install system dependencies
13
+ RUN apt-get update && apt-get install -y --no-install-recommends \
14
+ ffmpeg \
15
+ sox \
16
+ libsndfile1 \
17
+ git \
18
+ && rm -rf /var/lib/apt/lists/*
19
+
20
+ # Upgrade pip and setuptools
21
+ RUN pip install --upgrade pip setuptools
22
+
23
+ # Copy the requirements file and install dependencies
24
+ COPY requirements.txt .
25
+ RUN pip install --no-cache-dir -r requirements.txt
26
+
27
+ # Install specific versions of additional packages
28
+ RUN pip install transformers==4.35.0 sentencepiece
29
+
30
+ # Copy application code
31
+ COPY . /app
32
+
33
+ # Generate gRPC code
34
+ #RUN python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. seamless_m4t.proto
35
+ RUN pip install git+https://github.com/huggingface/transformers.git
36
+ # Debug the transformers package and download the model
37
+ #RUN python -c "import transformers; print(transformers.__version__); print(dir(transformers))"
38
+ #RUN python -c "from transformers import AutoProcessor, AutoModel; \
39
+ # model_name = 'facebook/seamless-m4t-v2-large'; \
40
+ # AutoProcessor.from_pretrained(model_name); \
41
+ # AutoModel.from_pretrained(model_name)"
42
+
43
+ # Expose the port
44
+ EXPOSE 8080
45
+
46
+ # Command to run
47
+ CMD ["python", "server51.py"]
48
+
49
+
__pycache__/seamless_m4t_pb2.cpython-310.pyc ADDED
Binary file (1.34 kB). View file
 
__pycache__/seamless_m4t_pb2_grpc.cpython-310.pyc ADDED
Binary file (3.15 kB). View file
 
cert_config.cnf ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [req]
2
+ distinguished_name = req_distinguished_name
3
+ req_extensions = v3_req
4
+ prompt = no
5
+
6
+ [req_distinguished_name]
7
+ CN = reserve.aixblock.io
8
+
9
+ [v3_req]
10
+ subjectAltName = @alt_names
11
+
12
+ [alt_names]
13
+ DNS.1 = reserve.aixblock.io
14
+
h62_client.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import grpc
2
+ import pyaudio
3
+ import wave
4
+ import io
5
+ import threading
6
+ import queue
7
+ import seamless_m4t_pb2
8
+ import seamless_m4t_pb2_grpc
9
+
10
+ debug = False
11
+
12
+ def record_audio_to_queue(audio_queue, chunk_duration_s, sample_rate=16000, channels=1):
13
+ """
14
+ Captures audio from the sound card and pushes it to a queue in WAV format.
15
+ """
16
+ # Initialize PyAudio
17
+ p = pyaudio.PyAudio()
18
+ window=8192 #4096
19
+
20
+ # Open the audio stream
21
+ stream = p.open(format=pyaudio.paInt16,
22
+ channels=channels,
23
+ rate=sample_rate,
24
+ input=True,
25
+ frames_per_buffer=window) # Increased buffer size
26
+
27
+ if debug:
28
+ print("Recording audio... Press Ctrl+C to stop.")
29
+ try:
30
+ while True:
31
+ frames = []
32
+
33
+ # Record for the specified duration
34
+ for _ in range(0, int(sample_rate / 1024 * chunk_duration_s)):
35
+ try:
36
+ data = stream.read(window, exception_on_overflow=False) # Handle overflow
37
+ frames.append(data)
38
+ except OSError as e:
39
+ print(f"Audio buffer overflow: {e}")
40
+ break
41
+
42
+ # Write audio data to WAV format in memory
43
+ with io.BytesIO() as wav_buffer:
44
+ with wave.open(wav_buffer, 'wb') as wf:
45
+ wf.setnchannels(channels)
46
+ wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
47
+ wf.setframerate(sample_rate)
48
+ wf.writeframes(b"".join(frames))
49
+ audio_queue.put(wav_buffer.getvalue())
50
+
51
+ except KeyboardInterrupt:
52
+ print("Stopped recording.")
53
+ finally:
54
+ if stream.is_active():
55
+ stream.stop_stream()
56
+ stream.close()
57
+ p.terminate()
58
+ audio_queue.put(None) # Signal the end of recording
59
+
60
+
61
+ def send_audio_chunks_to_server(audio_queue, server_address, tgt_lang):
62
+ """
63
+ Sends audio chunks from the queue to the gRPC server and prints the transcriptions.
64
+ """
65
+ # Connect to the gRPC server
66
+ channel = grpc.insecure_channel(server_address)
67
+ stub = seamless_m4t_pb2_grpc.SeamlessM4TServiceStub(channel)
68
+
69
+ chunk_id = 0
70
+ while True:
71
+ audio_data = audio_queue.get()
72
+ if audio_data is None: # End of recording
73
+ break
74
+
75
+ try:
76
+ if debug:
77
+ print(f"Sending chunk {chunk_id} to server...")
78
+
79
+ # Create and send the request
80
+ request = seamless_m4t_pb2.SpeechToTextRequest(audio=audio_data, tgt_lang=tgt_lang)
81
+ response = stub.SpeechToText(request)
82
+
83
+ # Print the response
84
+ print(f"{response.text}")
85
+ chunk_id += 1
86
+
87
+ except grpc.RpcError as e:
88
+ print(f"gRPC Error: {e.code()} - {e.details()}")
89
+ except Exception as e:
90
+ print(f"Unexpected error: {e}")
91
+
92
+
93
+ if __name__ == "__main__":
94
+ # Parameters
95
+ chunk_duration_s = 1 # Record in 2-second chunks
96
+ server_address = "localhost:9090" # gRPC server address
97
+ tgt_lang = "eng" # Target language for transcription
98
+
99
+ # Create a queue to share audio chunks between threads
100
+ audio_queue = queue.Queue()
101
+
102
+ # Start the recording and sending threads
103
+ recorder_thread = threading.Thread(target=record_audio_to_queue, args=(audio_queue, chunk_duration_s))
104
+ sender_thread = threading.Thread(target=send_audio_chunks_to_server, args=(audio_queue, server_address, tgt_lang))
105
+
106
+ recorder_thread.start()
107
+ sender_thread.start()
108
+
109
+ # Wait for both threads to finish
110
+ recorder_thread.join()
111
+ sender_thread.join()
112
+
113
+ print("Recording and transcription completed.")
114
+
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  torchaudio
2
  grpcio
3
  grpcio-tools
4
- protobuf
 
 
1
  torchaudio
2
  grpcio
3
  grpcio-tools
4
+ protobuf
5
+
seamless_m4t.proto ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ syntax = "proto3";
2
+
3
+ package seamlessm4t;
4
+
5
+ // Define the gRPC service
6
+ service SeamlessM4TService {
7
+ // Unary RPC for speech-to-text
8
+ rpc SpeechToText(SpeechToTextRequest) returns (SpeechToTextResponse);
9
+ }
10
+
11
+ // Define the request message for speech-to-text
12
+ message SpeechToTextRequest {
13
+ bytes audio = 1; // The audio file data in bytes
14
+ string tgt_lang = 2; // The target language for transcription (e.g., "eng", "fra")
15
+ }
16
+
17
+ // Define the response message for speech-to-text
18
+ message SpeechToTextResponse {
19
+ string text = 1; // The transcribed text from the audio
20
+ }
21
+
seamless_m4t_pb2.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
3
+ # NO CHECKED-IN PROTOBUF GENCODE
4
+ # source: seamless_m4t.proto
5
+ # Protobuf Python Version: 5.28.1
6
+ """Generated protocol buffer code."""
7
+ from google.protobuf import descriptor as _descriptor
8
+ from google.protobuf import descriptor_pool as _descriptor_pool
9
+ from google.protobuf import runtime_version as _runtime_version
10
+ from google.protobuf import symbol_database as _symbol_database
11
+ from google.protobuf.internal import builder as _builder
12
+ _runtime_version.ValidateProtobufRuntimeVersion(
13
+ _runtime_version.Domain.PUBLIC,
14
+ 5,
15
+ 28,
16
+ 1,
17
+ '',
18
+ 'seamless_m4t.proto'
19
+ )
20
+ # @@protoc_insertion_point(imports)
21
+
22
+ _sym_db = _symbol_database.Default()
23
+
24
+
25
+
26
+
27
+ DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x12seamless_m4t.proto\x12\x0bseamlessm4t\"6\n\x13SpeechToTextRequest\x12\r\n\x05\x61udio\x18\x01 \x01(\x0c\x12\x10\n\x08tgt_lang\x18\x02 \x01(\t\"$\n\x14SpeechToTextResponse\x12\x0c\n\x04text\x18\x01 \x01(\t2i\n\x12SeamlessM4TService\x12S\n\x0cSpeechToText\x12 .seamlessm4t.SpeechToTextRequest\x1a!.seamlessm4t.SpeechToTextResponseb\x06proto3')
28
+
29
+ _globals = globals()
30
+ _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
31
+ _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'seamless_m4t_pb2', _globals)
32
+ if not _descriptor._USE_C_DESCRIPTORS:
33
+ DESCRIPTOR._loaded_options = None
34
+ _globals['_SPEECHTOTEXTREQUEST']._serialized_start=35
35
+ _globals['_SPEECHTOTEXTREQUEST']._serialized_end=89
36
+ _globals['_SPEECHTOTEXTRESPONSE']._serialized_start=91
37
+ _globals['_SPEECHTOTEXTRESPONSE']._serialized_end=127
38
+ _globals['_SEAMLESSM4TSERVICE']._serialized_start=129
39
+ _globals['_SEAMLESSM4TSERVICE']._serialized_end=234
40
+ # @@protoc_insertion_point(module_scope)
seamless_m4t_pb2_grpc.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2
+ """Client and server classes corresponding to protobuf-defined services."""
3
+ import grpc
4
+ import warnings
5
+
6
+ import seamless_m4t_pb2 as seamless__m4t__pb2
7
+
8
+ GRPC_GENERATED_VERSION = '1.68.0'
9
+ GRPC_VERSION = grpc.__version__
10
+ _version_not_supported = False
11
+
12
+ try:
13
+ from grpc._utilities import first_version_is_lower
14
+ _version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
15
+ except ImportError:
16
+ _version_not_supported = True
17
+
18
+ if _version_not_supported:
19
+ raise RuntimeError(
20
+ f'The grpc package installed is at version {GRPC_VERSION},'
21
+ + f' but the generated code in seamless_m4t_pb2_grpc.py depends on'
22
+ + f' grpcio>={GRPC_GENERATED_VERSION}.'
23
+ + f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
24
+ + f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
25
+ )
26
+
27
+
28
+ class SeamlessM4TServiceStub(object):
29
+ """Define the gRPC service
30
+ """
31
+
32
+ def __init__(self, channel):
33
+ """Constructor.
34
+
35
+ Args:
36
+ channel: A grpc.Channel.
37
+ """
38
+ self.SpeechToText = channel.unary_unary(
39
+ '/seamlessm4t.SeamlessM4TService/SpeechToText',
40
+ request_serializer=seamless__m4t__pb2.SpeechToTextRequest.SerializeToString,
41
+ response_deserializer=seamless__m4t__pb2.SpeechToTextResponse.FromString,
42
+ _registered_method=True)
43
+
44
+
45
+ class SeamlessM4TServiceServicer(object):
46
+ """Define the gRPC service
47
+ """
48
+
49
+ def SpeechToText(self, request, context):
50
+ """Unary RPC for speech-to-text
51
+ """
52
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
53
+ context.set_details('Method not implemented!')
54
+ raise NotImplementedError('Method not implemented!')
55
+
56
+
57
+ def add_SeamlessM4TServiceServicer_to_server(servicer, server):
58
+ rpc_method_handlers = {
59
+ 'SpeechToText': grpc.unary_unary_rpc_method_handler(
60
+ servicer.SpeechToText,
61
+ request_deserializer=seamless__m4t__pb2.SpeechToTextRequest.FromString,
62
+ response_serializer=seamless__m4t__pb2.SpeechToTextResponse.SerializeToString,
63
+ ),
64
+ }
65
+ generic_handler = grpc.method_handlers_generic_handler(
66
+ 'seamlessm4t.SeamlessM4TService', rpc_method_handlers)
67
+ server.add_generic_rpc_handlers((generic_handler,))
68
+ server.add_registered_method_handlers('seamlessm4t.SeamlessM4TService', rpc_method_handlers)
69
+
70
+
71
+ # This class is part of an EXPERIMENTAL API.
72
+ class SeamlessM4TService(object):
73
+ """Define the gRPC service
74
+ """
75
+
76
+ @staticmethod
77
+ def SpeechToText(request,
78
+ target,
79
+ options=(),
80
+ channel_credentials=None,
81
+ call_credentials=None,
82
+ insecure=False,
83
+ compression=None,
84
+ wait_for_ready=None,
85
+ timeout=None,
86
+ metadata=None):
87
+ return grpc.experimental.unary_unary(
88
+ request,
89
+ target,
90
+ '/seamlessm4t.SeamlessM4TService/SpeechToText',
91
+ seamless__m4t__pb2.SpeechToTextRequest.SerializeToString,
92
+ seamless__m4t__pb2.SpeechToTextResponse.FromString,
93
+ options,
94
+ channel_credentials,
95
+ insecure,
96
+ call_credentials,
97
+ compression,
98
+ wait_for_ready,
99
+ timeout,
100
+ metadata,
101
+ _registered_method=True)
server.crt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEwjCCAqoCE14N9UG91aZF9RVmsm6ON5SjewQwDQYJKoZIhvcNAQELBQAwHjEc
3
+ MBoGA1UEAwwTcmVzZXJ2ZS5haXhibG9jay5pbzAeFw0yND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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import torchaudio
4
+ from transformers import AutoProcessor, SeamlessM4Tv2Model
5
+ import seamless_m4t_pb2
6
+ import seamless_m4t_pb2_grpc
7
+ from concurrent import futures
8
+ import grpc
9
+ import io
10
+ # Set Hugging Face cache path
11
+ os.environ["HF_HOME"] = "/path/to/custom/huggingface_cache" # Change this to your desired path
12
+
13
+ # Load the transcription model and processor
14
+ MODEL_NAME = "facebook/seamless-m4t-v2-large"
15
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
+
17
+ print(f"Loading model {MODEL_NAME} on {device}...")
18
+ processor = AutoProcessor.from_pretrained(MODEL_NAME)
19
+ model = SeamlessM4Tv2Model.from_pretrained(MODEL_NAME).to(device)
20
+
21
+
22
+ class SeamlessM4TServicer(seamless_m4t_pb2_grpc.SeamlessM4TServiceServicer):
23
+ def SpeechToText(self, request, context):
24
+ """
25
+ Handles a unary SpeechToText request, processes the audio, and returns a transcription.
26
+ """
27
+ try:
28
+ # Load audio from the request directly into memory
29
+ print("Processing received audio in memory...")
30
+ audio_data = torch.frombuffer(request.audio, dtype=torch.float32)
31
+
32
+ # Convert the buffer to a waveform tensor
33
+ waveform, sampling_rate = torchaudio.load(io.BytesIO(request.audio), format="wav")
34
+ print(f"Loaded audio: shape={waveform.shape}, sampling_rate={sampling_rate}")
35
+
36
+ # Resample to 16 kHz if necessary
37
+ if sampling_rate != 16000:
38
+ print("Resampling audio to 16kHz...")
39
+ resampler = torchaudio.transforms.Resample(orig_freq=sampling_rate, new_freq=16000)
40
+ waveform = resampler(waveform)
41
+
42
+ # Convert stereo to mono if necessary
43
+ if waveform.shape[0] > 1:
44
+ print("Converting audio to mono...")
45
+ waveform = torch.mean(waveform, dim=0, keepdim=True)
46
+
47
+ # Prepare the input for the model
48
+ inputs = processor(
49
+ audios=waveform.squeeze().numpy(),
50
+ sampling_rate=16000,
51
+ return_tensors="pt"
52
+ ).to(device)
53
+
54
+ # Generate transcription
55
+ output_tokens = model.generate(**inputs, tgt_lang=request.tgt_lang, generate_speech=False)
56
+ transcribed_text = processor.decode(output_tokens[0].tolist()[0], skip_special_tokens=True)
57
+
58
+ print(f"Transcription result: {transcribed_text}")
59
+
60
+ # Return the response
61
+ return seamless_m4t_pb2.SpeechToTextResponse(text=transcribed_text)
62
+
63
+ except Exception as e:
64
+ print(f"Error in SpeechToText: {e}")
65
+ context.set_details(str(e))
66
+ context.set_code(grpc.StatusCode.INTERNAL)
67
+ return seamless_m4t_pb2.SpeechToTextResponse(text="Error during transcription.")
68
+
69
+
70
+ def serve():
71
+ """
72
+ Start the gRPC server and listen for client connections.
73
+ """
74
+ """
75
+ Start the gRPC server with secure communication using TLS.
76
+ """
77
+ # Load the server's certificate and private key
78
+ with open("server.crt", "rb") as cert_file, open("server.key", "rb") as key_file:
79
+ server_credentials = grpc.ssl_server_credentials(
80
+ [(key_file.read(), cert_file.read())]
81
+ )
82
+
83
+ server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
84
+ seamless_m4t_pb2_grpc.add_SeamlessM4TServiceServicer_to_server(SeamlessM4TServicer(), server)
85
+ server.add_secure_port("[::]:9080",server_credentials)
86
+ print("Server is running on port 9080...")
87
+ server.start()
88
+ server.wait_for_termination()
89
+
90
+
91
+ if __name__ == "__main__":
92
+ serve()
93
+