File size: 3,532 Bytes
a97d86f
946dfa0
 
 
 
a97d86f
 
 
 
 
 
 
 
 
 
a5280a3
9a07faa
 
 
946dfa0
 
4e929f4
 
946dfa0
 
c645a12
946dfa0
 
a5280a3
 
 
 
 
 
9a07faa
 
 
 
 
 
260e2bc
a5280a3
8cbd484
9a07faa
 
a5280a3
9d529cc
8197eb2
 
084626b
 
 
 
a5280a3
9a07faa
a97d86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5280a3
 
07b5bcb
a97d86f
 
07b5bcb
 
a97d86f
 
 
 
 
 
 
 
 
 
 
 
07b5bcb
 
a97d86f
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Deploying AI Voice Chatbot Gradio App."""

import os
import subprocess

from gradio import Audio, Interface, Textbox

from utils import (TextGenerationPipeline, from_en_translation,
                   html_audio_autoplay, stt, to_en_translation, tts,
                   tts_to_bytesio)

max_answer_length = 100
desired_language = "en"
response_generator_pipe = TextGenerationPipeline(max_length=max_answer_length)

from google.cloud import aiplatform
import vertexai
from vertexai.preview.language_models import ChatModel, InputOutputTextPair

# Imports the Google Cloud client library

# credential_path = "GOOGLE_APPLICATION_CREDENTIALS=~/santa/didso.json"
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

# GOOGLE_APPLICATION_CREDENTIALS = os.environ['GOOGLE_APPLICATION_CREDENTIALS']
# subprocess.call(f"wget --no-check-certificate -O {GOOGLE_APPLICATION_CREDENTIALS}", shell=True)


PROJECT_ID = "youtube-live-checker-245701"
LOCATION = "us-central1"
CHATMODEL ="chat-bison@001"

vertexai.init(project=PROJECT_ID, location=LOCATION)
chat_model = ChatModel.from_pretrained(CHATMODEL)
parameters = {
    "temperature": 0.2,
    "max_output_tokens": 256,
    "top_p": 0.8,
    "top_k": 40
}

chat_session = chat_model.start_chat(
    context="""Develop an AI chatbot that embodies the spirit of Santa Claus and engages in delightful conversations suitable for 10-year-old children. The chatbot should exude joy and curiosity, discussing holiday traditions, inquiring about the child's preferred pastimes, and sharing enchanting tales about the North Pole. However, it's important that the AI Santa consistently employs gender-neutral language when addressing the user and refrains from making any commitments or assurances concerning gifts. The primary goal is to craft a heartwarming and enjoyable conversational experience that captures the magic of the holiday season, while avoiding any reference to gift expectations. Do not call the user by the name until the user reveals his/her name.""",
)

def response_generator_santa(text: str) -> str:
    answer = chat_session.send_message(text)
    print("Answer output : ")
    print(answer)
    answer_str = str(answer)
    print(answer_str)
    
    return answer_str


def main(audio: object):
    """Calls functions for deploying gradio app.

    It responds both verbally and in text
    by taking voice input from user.

    Args:
        audio (object): recorded speech of user

    Returns:
        tuple containing

        - user_speech_text (str) : recognized speech
        - bot_response_de (str) : translated answer of bot
        - bot_response_en (str) : bot's original answer
        - html (object) : autoplayer for bot's speech
    """
    user_speech_text = stt(audio, desired_language)
    # bot_response_en = response_generator_pipe(user_speech_text)
    bot_response_en = response_generator_santa(user_speech_text)
    bot_voice = tts(bot_response_en, desired_language)
    bot_voice_bytes = tts_to_bytesio(bot_voice)
    html = html_audio_autoplay(bot_voice_bytes)
    # return user_speech_text, bot_response_de, bot_response_en, html
    return user_speech_text, bot_response_en, html


Interface(
    fn=main,
    inputs=[
        Audio(
            source="microphone",
            type="filepath",
        ),
    ],
    outputs=[
        Textbox(label="You said: "),
        # Textbox(label="Santa said: "),
        Textbox(label="Santa said (English): "),
        "html",
    ],
    live=True,
    allow_flagging="never",
).launch()