import glob
import sys
import os
import re
import datetime
import time
import requests
import asyncio

from google.cloud import speech_v1
from google.cloud import speech
import io
from google.cloud import storage




import io
import glob
import sys
import os
import re
import datetime
import time


from google.cloud import speech_v1
from google.cloud import speech
import io
from google.cloud import storage

from flask import Flask, render_template, request
from flask import redirect, url_for
from flask import flash
from werkzeug.utils import secure_filename
from flask import send_from_directory
from flask_jsonpify import jsonify
from flask_cors import cross_origin, CORS
from scipy.io.wavfile import read as read_wav

from flask import jsonify

app = Flask(__name__)
CORS(app)

DOMAIN_NAME = "elprod.forma2plus.com"






















GCI_JSON= '/home/ubuntu/google_cred/'
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/ubuntu/google_cred/formatrad-7ed5e4c68b97.json"
UPLOAD_AUDIO="/var/www/html/api/google_transcirption_test/"
def upload_to_bucket(blob_name, path_to_file, bucket_name):
    """ Upload data to a bucket"""

    # Explicitly use service account credentials by specifying the private key
    # file.
    client = storage_client = storage.Client.from_service_account_json(os.path.join(GCI_JSON, 'gcs_uri.json'))

    #print(buckets = list(storage_client.list_buckets())

    bucket = client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.upload_from_filename(path_to_file)

    #returns a public url
    return blob.public_url
    

def sample_long_running_recognize(local_file_path,filename,transcript_path):
    """
    Transcribe a long audio file using asynchronous speech recognition

    Args:
      local_file_path Path to local audio file, e.g. /path/audio.wav
    """

    client = speech_v1.SpeechClient()

    # local_file_path = 'resources/brooklyn_bridge.raw'

    # The language of the supplied audio
    language_code = "en-US"

    # Sample rate in Hertz of the audio data sent
    sample_rate_hertz, data=read_wav(local_file_path)
    #sample_rate_hertz = 32000
    print("33333333333")
    print(local_file_path)
    # Encoding of audio data sent. This sample sets this explicitly.
    # This field is optional for FLAC and WAV audio formats.
    storage_uri = upload_to_bucket(filename,local_file_path,'bucket_gcuri')
    #The path should be gs://<bucket_name>/<file_path_inside_bucket>.
    storage_uri = 'gs://bucket_gcuri/'+filename
    print("sotrage={}".format(storage_uri))
    encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16
    diarization_config = speech.SpeakerDiarizationConfig(
      enable_speaker_diarization=True,
      min_speaker_count=1,
      max_speaker_count=1,
      enable_automatic_punctuation=True,

    )

    config = {
        "language_code": language_code,
        "sample_rate_hertz": sample_rate_hertz,
        "encoding": encoding,
        "model" : "video",
        "diarization_config":diarization_config

    }
    with io.open(local_file_path, "rb") as f:
        content = f.read()
    audio = {"uri": storage_uri}

    operation = client.long_running_recognize(config = config, audio = audio)

    print(u"Waiting for operation to complete...")
    response = operation.result()
    os.system(f"touch {UPLOAD_AUDIO}{filename.replace('.wav','.txt')}")
    outfile = open(f"{UPLOAD_AUDIO}{filename.replace('.wav','.txt')}", 'w')
    #print(copy_line)

    text_transcript_prof = ""
    text_transcript_stag = ''
    #words_info = result.alternatives[0].words
    # word_info.speaker_tag

    text_transcript_stag = ''
    for result in response.results:
        # First alternative is the most probable result
        alternative = result.alternatives[0]
        words_info = result.alternatives[0].words

        

        print(u"Transcript: {}".format(alternative.transcript))

        for word_info in words_info:
            print("word: '{}', speaker_tag: {}".format(word_info.word, word_info.speaker_tag))

            text_transcript_stag = text_transcript_stag+ word_info.word + ' '


    print("#############")
    print(text_transcript_stag)
    # outfile.write(text_transcript_stag)

    # outfile.close()
    return text_transcript_stag


#sample_long_running_recognize("4626_10013_2023-10-18-20-51-142.wav", "4626_10013_2023-10-18-20-51-142.wav", "4626_10013_2023-10-18-20-51-142.txt")














@app.route('/transcriptionopenai', methods = ['GET', 'POST'])
def open_ai():
    #data = dict(request.form)
    print("1111111111")
    url = ''
    if request.method == 'POST':
        print("222222222222")
        filename = request.form['filename']
        UPLOAD_AUDIO = request.form['pathaudio']
        print(UPLOAD_AUDIO+filename)

        stag = sample_long_running_recognize(UPLOAD_AUDIO+filename, filename, filename.replace('.wav','.txt'))
    # file_name = url.split('/')[ len(url.split('/'))  -1 ].replace('.wav','')

    # import wget
    # wget.download(url)

    # print(f"/mnt/data/anaconda3/envs/transcriptionenv/bin/whisperx {UPLOAD_AUDIO}{file_name}.wav  --hf_token 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 --vad_filter True --diarize --min_speakers 2 --max_speakers 2 --language en")
    # os.system(f"/mnt/data/anaconda3/envs/transcriptionenv/bin/whisperx {UPLOAD_AUDIO}{file_name}.wav --hf_token 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 --vad_filter True --diarize --min_speakers 2 --max_speakers 2 --language en")
    # stag = ""
    # with open(f"{UPLOAD_AUDIO}{file_name+'.txt'}" , 'r') as file:
    #     stag = file.read().replace('\n', '')

        return stag


if __name__ == '__main__':
    context = ('server.crt', 'server.key')
    app.run(host=DOMAIN_NAME,port='5011', ssl_context=context, debug=True)
