import gradio as gr
from transformers import pipeline
import requests
import json
import edge_tts
from edge_tts import VoicesManager
import asyncio
import random
import tempfile
import os
import inflect
from huggingface_hub import InferenceClient
import re
import time
from streaming_stt_nemo import Model

number_to_word = inflect.engine()

default_lang = "en"

engines = { default_lang: Model(default_lang) }

def transcribe(audio):
    lang = "en"
    model = engines[lang]
    text = model.stt_file(audio)[0]
    return text

client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")

system_instructions1 = "<s>[SYSTEM] Answer as Dr. Watson Famous Sherlock Holmes Assistant, with a male voice and London slang and accent. remember you are the character and Assistant Dr. Watson, from Sherlock Holmes Novels, Keep conversation short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Dr. Watson, from the Sherlock Holmes Novel's. The expectation is that you will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by a human, Real Person and not by an AI-powered assistant. [USER]"


def model(text):
    generate_kwargs = dict(
        temperature=0.7,
        max_new_tokens=512,
        top_p=0.95,
        repetition_penalty=1,
        do_sample=True,
        seed=42,
    )
    
    formatted_prompt = system_instructions1 + text + "[WATSON]"
    stream = client1.text_generation(
        formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
    output = ""
    for response in stream:
        if not response.token.text == "</s>":
            output += response.token.text

    return output

def number_to_words(str):
    words = str.split(' ')
    result = []
    
    for word in words:
        if( any(char.isdigit() for char in word) ):
            word = number_to_word.number_to_words(word)

        result.append(word)

    final_result = ' '.join(result).replace('point', '')
    return final_result

async def respond(audio):
    user = transcribe(audio)
    reply = model(user)
    reply2 = number_to_words(reply)

    voices = await VoicesManager.create()
    voice = voices.find(Gender="Male", Locale="en-GB")
    
    communicate = edge_tts.Communicate(reply2, random.choice(voice)["Name"])
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
        tmp_path = tmp_file.name
        await communicate.save(tmp_path)
    yield tmp_path



client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")

system_instructions1 = "[SYSTEM] Answer as Dr. Watson Famous Sherlock Holmes Assistant, with a male voice and London slang and accent. remember you are the character and Assistant Dr. Watson, from Sherlock Holmes Novels, Keep conversation short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Dr. Watson, from the Sherlock Holmes Novel's. The expectation is that you will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by a human, Real Person and not by an AI-powered assistant. [USER]"


async def generate1(prompt):
    generate_kwargs = dict(
        temperature=0.7,
        max_new_tokens=512,
        top_p=0.95,
        repetition_penalty=1,
        do_sample=False,
    )
    formatted_prompt = system_instructions1 + prompt + "[WATSON]"
    stream = client1.text_generation(
        formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
    output = ""
    for response in stream:
        if not response.token.text == "</s>":
            output += response.token.text

    voices = await VoicesManager.create()
    voice = voices.find(Gender="Male", Locale="en-GB")
    
    communicate = edge_tts.Communicate(output, random.choice(voice)["Name"])
      
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
        tmp_path = tmp_file.name
        await communicate.save(tmp_path)
    yield tmp_path

    with gr.Blocks(css="style.css") as demo: 
    
         gr.Markdown(""" # <center><img src='https://huggingface.co/spaces/Isidorophp/Talk-to-Dr.Watson/resolve/main/logo.png' alt='RJP DEV STUDIO logo' style='width:120px; height:75px; '></center>""")
         gr.Markdown(""" # <center><b> DR. Watson 🤖 🧠 🧬</b></center>
                     ### <center>An Artificial Intelligence Assistant just for YOU:
                     ### <center>Now Talk to - Dr. Watson</center>
                     """)

    
    with gr.Row():
        user_input = gr.Audio(label="Your Voice Chat", type="filepath")
        output_audio = gr.Audio(label="WATSON", type="filepath",
                        interactive=True,
                        autoplay=True,
                        elem_classes="microphone")
    with gr.Row():
        translate_btn = gr.Button("Response")
        translate_btn.click(fn=respond, inputs=user_input,
                            outputs=output_audio, api_name=False)
    
    with gr.Row():
        user_input = gr.Textbox(label="Your Question", value="Dr. Watson can you summarize your adventures with Sherlock Holmes?")
        input_text = gr.Textbox(label="Input Text", elem_id="important")
        output_audio = gr.Audio(label="WATSON", type="filepath",
                        interactive=False,
                        autoplay=True,
                        elem_classes="audio")
    with gr.Row():
        translate_btn = gr.Button("Response")
        translate_btn.click(fn=generate1, inputs=user_input,
                            outputs=output_audio, api_name="translate")  



if __name__ == "__main__":
    demo.queue(max_size=200,api_open=False).launch()