Import python module in flutter using starflut - python

I am trying to develop a Flutter app for audio fingerprints processing. I am using Starflut for python integration. Here is simple example:
//python file for using from dart
def tt(a,b) :
print (a, b)
return 666,777
g1 = 123
def yy(a,b,z) :
print(a,b,z)
return {'jack': 4098, 'sape': 4139}
class Multiply :
def __init__(self):
pass
def multiply(self, a,b):
print("multiply....",a,b)
return a * b
//dart code which uses python
void _initStarCore() async {
StarCoreFactory starcore = await Starflut.getFactory();
StarServiceClass service = await starcore.initSimple("test", "123", 0, 0, []);
srvGroup = await service["_ServiceGroup"];
bool isAndroid = await Starflut.isAndroid();
if (isAndroid == true) {
String libraryDir = await Starflut.getNativeLibraryDir();
String docPath = await Starflut.getDocumentPath();
if (libraryDir.indexOf("arm64") > 0) {
Starflut.unzipFromAssets("lib-dynload-arm64.zip", docPath, true);
} else if (libraryDir.indexOf("x86_64") > 0) {
Starflut.unzipFromAssets("lib-dynload-x86_64.zip", docPath, true);
} else if (libraryDir.indexOf("arm") > 0) {
Starflut.unzipFromAssets("lib-dynload-armeabi.zip", docPath, true);
} else {
Starflut.unzipFromAssets("lib-dynload-x86.zip", docPath, true);
}
await Starflut.copyFileFromAssets("python3.6.zip",
"flutter_assets/starfiles", null);
}
await srvGroup.initRaw("python36", service);
String resPath = await Starflut.getResourcePath();
srvGroup.loadRawModule("python", "",
resPath + "/flutter_assets/starfiles/" + "testpy.py", false);
dynamic python = await service.importRawContext("python", "", false, "");
StarObjectClass retobj = await python.call("tt", ["hello ", "world"]);
print(await retobj[0]);
print(await retobj[1]);
print(await python["g1"]);
StarObjectClass yy = await python.call("yy", ["hello ", "world", 123]);
print(await yy.call("__len__",[]));
StarObjectClass multiply = await service.importRawContext("python", "Multiply", true, "");
StarObjectClass multiply_inst = await multiply.newObject(["", "", 33, 44]);
print(await multiply_inst.getString());
print(await multiply_inst.call("multiply", [11, 22]));
await srvGroup.clearService();
await starcore.moduleExit();
}
Now I need to import python library Dejavu for audio fingerprinting, but I do not know how to do that. There is nothing about it in starflut documentation or issues in their repository.
Has somebody faced same problem? Or maybe somebody has any suggestion how i can try to solve it?
Sorry for mistakes, hope the text is understandable:) English is not my native language.

Did you read the repo readme and installation readme file?
If not, try this:
In your command prompt:
pip install PyDejavu
In the module where you need to import Dejavu:
from dejavu import Dejavu

Have you tried chaquopy plugin for flutter, as it supports 90% of the python packages that you can use and integrate inside your flutter app.

Related

Process items concurrently and write the results sequentially?

The following Python code (from Read in parallel and write sequentially?) processes (reads) the data for ID 0 to N. There readers run in parallel and it writes the result in the order of ID. How to implement it in C# efficiently?
async def reader(q, id_to_data, data_ready): # Multiple readers are run in parallel
while True:
id = await q.get()
data = await read_async(id)
id_to_data[id] = data
data_ready.set()
async def main():
q = asyncio.Queue()
for id in range(1000):
await q.put(id)
id_to_data = {}
data_ready = asyncio.Event()
readers = [asyncio.create_task(reader(q, id_to_data, data_ready))
for _ in range(3)]
for id in range(1000):
while True:
# wait for the current ID to appear before writing
if id in id_to_data:
data = id_to_data.pop(id)
await data.write_async(f'{id}.csv')
break
# move on to the next ID
else:
# wait for new data and try again
await data_ready.wait()
data_ready.clear()
for r in readers:
r.cancel()
C#:
void main() {
List<int> ids = get_IDs();
ProcessAllIDs(ids);
}
async Task ProcessAllIDs(List<int> ids) {
// A low efficient implementation
for (var id in ids) {
var result = await Process1(id); // Can be run concurrently
await Process2(result); // Need to be run sequentially
}
}
A simple way would be to create a List of Tasks and then await in them in sequence:
var list = new List<Task<int>> { Calculate(2), Calculate(7), Calculate(19) };
foreach(var task in list)
{
Console.WriteLine(await task);
}
static async Task<int> Calculate(int x)
{
await Task.Delay(200);
return x*x;
}
This will start 3 Tasks that calculate stuff simultaneously and then print the results in order.
So for your example, it would look like this:
async Task main() {
List<int> ids = get_IDs();
await ProcessAllIDs(ids);
}
async Task ProcessAllIDs(List<int> ids) {
var tasks = ids.Select(id => Process1(id)).ToList();
for (var t in tasks) {
await Process2(await t); // Need to be run sequentially
}
}

How to transcribe the recording for speech recognization

After downloading and uploading files related to the mozilla deeepspeech, I started using google colab. I am using mozilla/deepspeech for speech recognization. The code shown below is for recording my audio. After recording the audio, I want to use a function/method to transcribe the recording into text. Everything compiles, but the text does not come out correctly. Any thoughts in my code?
"""
To write this piece of code I took inspiration/code from a lot of places.
It was late night, so I'm not sure how much I created or just copied o.O
Here are some of the possible references:
https://blog.addpipe.com/recording-audio-in-the-browser-using-pure-html5-and-minimal-javascript/
https://stackoverflow.com/a/18650249
https://hacks.mozilla.org/2014/06/easy-audio-capture-with-the-mediarecorder-api/
https://air.ghost.io/recording-to-an-audio-file-using-html5-and-js/
https://stackoverflow.com/a/49019356
"""
from google.colab.output import eval_js
from base64 import b64decode
from scipy.io.wavfile import read as wav_read
import io
import ffmpeg
AUDIO_HTML = """
<script>
var my_div = document.createElement("DIV");
var my_p = document.createElement("P");
var my_btn = document.createElement("BUTTON");
var t = document.createTextNode("Press to start recording");
my_btn.appendChild(t);
//my_p.appendChild(my_btn);
my_div.appendChild(my_btn);
document.body.appendChild(my_div);
var base64data = 0;
var reader;
var recorder, gumStream;
var recordButton = my_btn;
var handleSuccess = function(stream) {
gumStream = stream;
var options = {
//bitsPerSecond: 8000, //chrome seems to ignore, always 48k
mimeType : 'audio/webm;codecs=opus'
//mimeType : 'audio/webm;codecs=pcm'
};
//recorder = new MediaRecorder(stream, options);
recorder = new MediaRecorder(stream);
recorder.ondataavailable = function(e) {
var url = URL.createObjectURL(e.data);
var preview = document.createElement('audio');
preview.controls = true;
preview.src = url;
document.body.appendChild(preview);
reader = new FileReader();
reader.readAsDataURL(e.data);
reader.onloadend = function() {
base64data = reader.result;
//console.log("Inside FileReader:" + base64data);
}
};
recorder.start();
};
recordButton.innerText = "Recording... press to stop";
navigator.mediaDevices.getUserMedia({audio: true}).then(handleSuccess);
function toggleRecording() {
if (recorder && recorder.state == "recording") {
recorder.stop();
gumStream.getAudioTracks()[0].stop();
recordButton.innerText = "Saving the recording... pls wait!"
}
}
// https://stackoverflow.com/a/951057
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var data = new Promise(resolve=>{
//recordButton.addEventListener("click", toggleRecording);
recordButton.onclick = ()=>{
toggleRecording()
sleep(2000).then(() => {
// wait 2000ms for the data to be available...
// ideally this should use something like await...
//console.log("Inside data:" + base64data)
resolve(base64data.toString())
});
}
});
</script>
"""
def get_audio():
display(HTML(AUDIO_HTML))
data = eval_js("data")
binary = b64decode(data.split(',')[1])
process = (ffmpeg
.input('pipe:0')
.output('pipe:1', format='wav')
.run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True, quiet=True, overwrite_output=True)
)
output, err = process.communicate(input=binary)
riff_chunk_size = len(output) - 8
# Break up the chunk size into four bytes, held in b.
q = riff_chunk_size
b = []
for i in range(4):
q, r = divmod(q, 256)
b.append(r)
# Replace bytes 4:8 in proc.stdout with the actual size of the RIFF chunk.
riff = output[:4] + bytes(b) + output[8:]
sr, audio = wav_read(io.BytesIO(riff))
return audio, sr
audio, sr = get_audio()
def recordingTranscribe(audio):
data16 = np.frombuffer(audio)
return model.stt(data16)
recordingTranscribe(audio)
Try this
It is perfect!
note-required python 3.6 or below...
import speech_recognition as sr
def takeCommand():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio=r.listen(source)
try:
statement=r.recognize_google(audio,language='en-in')
print(f"user said:{statement}\n")
except Exception as e:
#speak("Sorry, please say that again")
print('Sorry, please say that again')
return "None"
return statement
if __name__=='__main__':
statement = takeCommand().lower()
print('detecting.....')
print(statement)

Discord bot not updating status

I am new at python so sry if question is stupid but i hope u guys will help me.
Bot not updating status every 5 sec(i put more time like 5 min and it didn't work too). It shows number of servers and not changing to second status.
from discord.ext import commands, tasks
from itertools import cycle
#tasks.loop( seconds = 12 )
async def changeStatus():
status = cycle( [f' on { len(client.guilds) } servers', '~help'] )
await client.change_presence( activity = discord.Activity( type = discord.ActivityType.playing, name = next(status) ) )
#client.event
async def on_ready():
print( 'bot connected' )
changeStatus.start()
Since
status = cycle( [f' on { len(client.guilds) } servers', '~help'] )
is called everytime you call your function, it will be reinterpreted, which means that the next() function always return the first element. To fix this, you will need a different approach. For example, create a global iteration-variable and declare you cycle-list as only a list.
iterationPosition = 0
#tasks.loop( seconds = 12 )
async def changeStatus():
status = [f' on { len(client.guilds) } servers', '~help']
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=status[iterationPosition]))
iterationPosition = 0 if (iterationPosition == len(status) - 1) else (iterationPosition + 1)
You will need to keep track if you've reached the end of your list. This is done by the last line of code.
It is not changing the status as you got a Presence Update rate-limit of 5/60secs per-session. These rate-limits are dynamic so don't get too close to them
status = cycle( [f' on { len(client.guilds) } servers', '~help'] ) This line should be outside of the changeStatus function as whenever it is called, it makes [f' on { len(client.guilds) } servers the first item.

How can i rewrite the subscriber in RXJS with rxpy

I don't know how to change the Subscriber of RXJS to the form of Python, which is not effective.
Typescript USES the GRPC stream form and plugs in the RXJS Subscriber. Python doesn't know how to write it.
RXJS code
import * as grpc from 'grpc';
import { Observable, Subscriber } from 'rxjs';
export function subscribeToWorkspace(
input: WorkspaceSubscriptionInput,
): Observable<EventPayload> {
return new Observable<EventPayload>((observer: Subscriber<EventPayload>) => {
log(`opening stream for subscription ${input.toString()}`);
const stream: grpc.ClientReadableStream<EventPayload> =
client.subscribeToWorkspace(input, metadata);
stream.on('data', (eventPayload: EventPayload) => {
if (observer.closed) {
return;
}
observer.next(eventPayload);
});
stream.on('error', (err) => observer.error(err));
stream.on('end', () => observer.complete());
return () => {
log(`closing stream for subscription ${input.toString()}`);
stream.cancel();
};
});
}
Python code
from rx import of
def subscribe_to_workspace(cls, subsinput: WorkspaceSubscriptionInput) -> Observable:
return of(cls.client.SubscribeToWorkspace(subsinput))
Rxpy does not work. What should Python do
# pip install rx
from rx import Observable, Observer
def get_strings(observer):
observer.on_next("Ram")
observer.on_next("Mohan")
observer.on_next("Shyam")
observer.on_completed()
class PrintObserver(Observer):
def on_next(self, value):
print("Received {0}".format(value))
def on_completed(self):
print("Finished")
def on_error(self, error):
print("Error: {0}".format(error))
source = Observable.create(get_strings)
source.subscribe(PrintObserver())

Tchannel server code not working. (Python and nodejs)

I have just started learning Uber's Tchannel. I'm trying to run the code from tchannel documentation in python and nodejs. In both the cases I am not able to connect the client to the server.
This is how my code looks like in nodejs, which i followed from http://tchannel-node.readthedocs.org/en/latest/GUIDE/:
var TChannel = require('tchannel');
var myLocalIp = require('my-local-ip');
var rootChannel = TChannel();
rootChannel.listen(0,myLocalIp());
rootChannel.on('listening', function onListen() {
console.log('got a server', rootChannel.address());
});
var TChannelThrift = rootChannel.TChannelAsThrift;
var keyChan = rootChannel.makeSubChannel({
serviceName: process.env.USER || 'keyvalue'
});
var fs = require('fs');
var keyThrift = TChannelThrift({
source: fs.readFileSync('./keyvalue.thrift', 'utf8')
});
var ctx = {
store: {}
};
keyThrift.register(keyChan, 'KeyValue::get_v1', ctx, get);
keyThrift.register(keyChan, 'KeyValue::put_v1', ctx, put);
function get(context, req, head, body, cb) {
cb(null, {
ok: true,
body: {
value: context.store[body.key]
}
});
}
function put(context, req, head, body, cb) {
context.store[body.key] = body.value;
cb(null, {
ok: true,
body: null
});
}
When i run this code i get this error:
node sever.js
assert.js:93
throw new assert.AssertionError({
^
AssertionError: every field must be marked optional, required, or have a default value on GetResult including "value" in strict mode
at ThriftStruct.link (/home/bhaskar/node_modules/thriftrw/struct.js:154:13)
at Thrift.link (/home/bhaskar/node_modules/thriftrw/thrift.js:199:32)
at new Thrift (/home/bhaskar/node_modules/thriftrw/thrift.js:69:10)
at new TChannelAsThrift (/home/bhaskar/node_modules/tchannel/as/thrift.js:46:17)
at TChannelAsThrift (/home/bhaskar/node_modules/tchannel/as/thrift.js:38:16)
at Object.<anonymous> (/home/bhaskar/uber/tchannel/thrift/sever.js:16:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Similar, I have also tried the same thing in python by following http://tchannel.readthedocs.org/projects/tchannel-python/en/latest/guide.html link.
code in python looks like this:
from __future__ import absolute_import
from tornado import ioloop
from tornado import gen
from service import KeyValue
from tchannel import TChannel
tchannel = TChannel('keyvalue-server')
values={}
#tchannel.thrift.register(KeyValue)
def getValue(request):
key = request.body.key
value = values.get(key)
if value is None:
raise KeyValue.NotFoundError(key)
return value
#tchannel.thrift.register(KeyValue)
def setValue(request):
key = request.body.key
value = request.body.value
values[key] = value
def run():
tchannel.listen()
print('Listening on %s' % tchannel.hostport)
if __name__ == '__main__':
run()
ioloop.IOLoop.current().start()
when i run this with python server.py command i get ` Listening on my-local-ip:58092
`
but when i try to connect it with the client using tcurl as:
tcurl -p localhost:58092 -t ~/keyvalue/thrift/service.thrift service KeyValue::setValue -3 '{"key": "hello", "value": "world"}'
I get this:
assert.js:93
throw new assert.AssertionError({
^
AssertionError: every field must be marked optional, required, or have a default value on NotFoundError including "key" in strict mode
at ThriftException.link (/usr/lib/node_modules/tcurl/node_modules/thriftrw/struct.js:154:13)
at Thrift.link (/usr/lib/node_modules/tcurl/node_modules/thriftrw/thrift.js:199:32)
at new Thrift (/usr/lib/node_modules/tcurl/node_modules/thriftrw/thrift.js:69:10)
at new TChannelAsThrift (/usr/lib/node_modules/tcurl/node_modules/tchannel/as/thrift.js:46:17)
at asThrift (/usr/lib/node_modules/tcurl/index.js:324:18)
at onIdentified (/usr/lib/node_modules/tcurl/index.js:278:13)
at finish (/usr/lib/node_modules/tcurl/node_modules/tchannel/peer.js:266:9)
at Array.onIdentified [as 1] (/usr/lib/node_modules/tcurl/node_modules/tchannel/peer.js:257:9)
at DefinedEvent.emit (/usr/lib/node_modules/tcurl/node_modules/tchannel/lib/event_emitter.js:90:25)
at TChannelConnection.onOutIdentified (/usr/lib/node_modules/tcurl/node_modules/tchannel/connection.js:383:26)
Can anyone tell me what is the mistake?
for the node example, the thrift file in the guide needs to be updated. try to use the following (i just added a required keyword for every field):
struct GetResult {
1: required string value
}
service KeyValue {
GetResult get_v1(
1: required string key
)
void put_v1(
1: required string key,
2: required string value
)
}

Categories