|
"""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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_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_en, html |
|
|
|
|
|
Interface( |
|
fn=main, |
|
inputs=[ |
|
Audio( |
|
source="microphone", |
|
type="filepath", |
|
), |
|
], |
|
outputs=[ |
|
Textbox(label="You said: "), |
|
|
|
Textbox(label="Santa said (English): "), |
|
"html", |
|
], |
|
live=True, |
|
allow_flagging="never", |
|
).launch() |