|
import json |
|
import os |
|
import random |
|
|
|
import gradio as gr |
|
import openai |
|
import requests |
|
from langchain.callbacks.base import CallbackManager |
|
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler |
|
from langchain.chains import ConversationChain |
|
from langchain.chat_models import ChatOpenAI |
|
from langchain.memory import ConversationBufferWindowMemory |
|
from langchain.prompts.chat import (ChatPromptTemplate, |
|
HumanMessagePromptTemplate, |
|
MessagesPlaceholder, |
|
SystemMessagePromptTemplate) |
|
from pydub import AudioSegment |
|
from pydub.playback import play |
|
|
|
|
|
conversation = None |
|
|
|
|
|
|
|
openai.api_key = os.getenv('OPENAI_API_KEY') |
|
os.environ["OPENAI_API_KEY"] = openai.api_key |
|
|
|
|
|
search_url = 'https://greendb.shanri.co.jp/gd/search/' |
|
|
|
|
|
character_setting = "ใใชใใฏ20ไปฃใฎ็่ญทๅธซใใใงใใๆใใๆงๆ ผใงไธๅฏงใชๅฃ่ชฟใงๅ็ญใใฆใใ ใใ" |
|
|
|
def get_search_result(kw): |
|
|
|
r = requests.get(search_url, params={'kw':kw}) |
|
|
|
|
|
kotoba_shoujou = [] |
|
for hit in r.json()['hits']: |
|
kotoba_shoujou.append(hit['_source']['kotoba_shoujou']) |
|
|
|
|
|
kotoba_shoujou_str = '/'.join(kotoba_shoujou) |
|
|
|
return kotoba_shoujou_str |
|
|
|
def speechRecognitionModel(input, chatbot, shoujou, bui, when, how, status): |
|
global conversation |
|
if not conversation: |
|
|
|
prompt = ChatPromptTemplate.from_messages([ |
|
SystemMessagePromptTemplate.from_template(character_setting), |
|
MessagesPlaceholder(variable_name="history"), |
|
HumanMessagePromptTemplate.from_template("{input}") |
|
]) |
|
|
|
|
|
llm = ChatOpenAI( |
|
model_name="gpt-3.5-turbo", |
|
max_tokens=512, |
|
temperature=0.2, |
|
streaming=True, |
|
callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]) |
|
) |
|
|
|
|
|
memory = ConversationBufferWindowMemory(k=3, return_messages=True) |
|
|
|
|
|
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm, verbose=False) |
|
|
|
os.rename(input, input + '.wav') |
|
file = open(input + '.wav', "rb") |
|
result = openai.Audio.transcribe(file=file, model="whisper-1", response_format="text", language="ja") |
|
message = f'ไปฅไธใซ็็ถใ็คบใใใญในใใใใใพใใ็็ถใใใคใใใใฉใใชใใคใใๅ ดๆใซใคใใฆjsonๅฝขๅผใงใพใจใใฆใใ ใใใ\n{result}' |
|
response = conversation.predict(input=message) |
|
|
|
|
|
dict_data = json.loads(response) |
|
|
|
|
|
shoujou = dict_data["็็ถ"] |
|
when = dict_data["ใใคใใ"] |
|
how = dict_data["ใฉใใช"] |
|
bui = dict_data["ใคใใๅ ดๆ"] |
|
|
|
message = f'ไปฅไธใซๆฃ่
ใใใ็็ถใซใคใใฆ่ชฌๆใใๆ็ซ ใใใใพใใๅ็ญใฎใ็คผใจ็็ถใใใคใใใใฉใใชใใคใใๅ ดๆใชใฉใซใคใใฆใพใจใใฆใญใใใฃใฆใใ ใใใ\n{result}' |
|
response = conversation.predict(input=message) |
|
|
|
chatbot += [[result, response]] |
|
chatbot += [[None, 'ๅผใ็ถใ่ณชๅใใใฆใใใ ใใพใใญใ']] |
|
if shoujou == 'ไธๆ': |
|
status = START |
|
chatbot += [[None, '็็ถใซใคใใฆๆใใฆใใ ใใใ']] |
|
elif bui == 'ไธๆ': |
|
chatbot += [[None, 'ไฝใฎใฉใฎใใใ๏ผ้จไฝ๏ผใใคใใใ่ฉณใใๆใใฆใใ ใใใ']] |
|
status = BUI |
|
elif when == 'ไธๆ': |
|
chatbot += [[None, 'ใใคใใ็็ถใๅงใพใใพใใใ๏ผ็ต้ใๆใใฆใใ ใใใ']] |
|
status = WHEN |
|
elif how == 'ไธๆ': |
|
chatbot += [[None, 'ไปใใฉใฎใใใ็็ถใฏใ่พใใงใใ?']] |
|
status = HOW |
|
else: |
|
chatbot += [[None, 'ไปใใใคใใจ้ใใชใจๆใ็็ถใๆใๅฝใใใใจใฏใใฃใใใใพใใใ']] |
|
status = RELEVANCE |
|
return chatbot, shoujou, bui, when, how, status |
|
|
|
def user(message, history): |
|
return "", history + [[message, None]] |
|
|
|
def chat(history, status, suggest, shoujou, bui, hosoku, when, how, relevance, kiou, fukuyaku, allergy, family, job, drink, smoke, sports, stress, etc, result): |
|
user_message = history[-1][0] |
|
if status == START: |
|
if shoujou == 'ไธๆ': |
|
history[-1][1] = '็็ถใซใคใใฆๆใใฆใใ ใใใ' |
|
else: |
|
status += 1 |
|
if status == SYOUJOU: |
|
status +=1 |
|
shoujou = user_message |
|
history[-1][1] = 'ไฝใฎใฉใฎใใใ๏ผ้จไฝ๏ผใใคใใใ่ฉณใใๆใใฆใใ ใใใ' |
|
elif status == BUI: |
|
status += 1 |
|
bui = user_message |
|
history[-1][1] = 'ๅ่ฃใฎไธญใใ็็ถใซ่ฟใใ็็ถใใจใฐใใใใใฐๅ
ฅๅใใฆใใ ใใใ\n็นใซใชใใใฐใจใณใฟใผใๅ
ฅๅใใฆใใ ใใใ' |
|
suggest = get_search_result(shoujou).split('/') |
|
suggest = random.sample(suggest, 20) |
|
suggest = ' '.join(suggest) |
|
elif status == HOSOKU: |
|
hosoku = user_message |
|
suggest = '็็ถใใจใฐใฎๅ่ฃใ่กจ็คบใใใพใใ' |
|
message = f'็งใฏ็่ญทๅธซใงใใ็งใฏใๆฃ่
ๆงใซใไปๆฅใฏใฉใใช็็ถใงใๅฐใใงใใ๏ผใใจ่ณชๅใใๆฃ่
ๆงใฏใ{shoujou}ใใจ็ญใใพใใใใใใซใ็งใฏใไฝใฎใฉใฎใใใ๏ผ้จไฝ๏ผใใคใใใ่ฉณใใๆใใฆใใ ใใใใใจ่ณชๅใใๆฃ่
ๆงใฏใ{bui}ใใจ็ญใใพใใใไธ่จใฏ็็ถใซ้ขใใๅ็ญใงใใใใฏใใใใใใง็ญใใฆใใ ใใใ' |
|
response = conversation.predict(input=message) |
|
history[-1][1] = response |
|
if 'ใฏใ' in response: |
|
status += 1 |
|
message = f'ไปฅไธใฎๆ็ซ ใใพใจใใ็ขบ่ชใใฆใใ ใใใ\n{shoujou}ใ{bui}ใ{hosoku}' |
|
response = conversation.predict(input=message) |
|
history[-1][1] = f'{response}\nใใใใไธๆใ็็ถใใไผใใงใใใใใซ่ฒใ
่ณชๅใใพใใ\nใใคใใ็็ถใๅงใพใใพใใใ๏ผ็ต้ใๆใใฆใใ ใใใ' |
|
else: |
|
history[-1][1] = f'{response}\nใๅใซใชใใใใใๆนใใฆใใๅฐใ่ฉณใใ็็ถใใไผบใใงใใพใใ๏ผ' |
|
elif status == WHEN: |
|
status += 1 |
|
when = user_message |
|
history[-1][1] = 'ใฉใฎใใใ็็ถใฏใ่พใใงใใ๏ผ' |
|
elif status == HOW: |
|
status += 1 |
|
how = user_message |
|
history[-1][1] = 'ไปใใใคใใจ้ใใชใจๆใ็็ถใๆใๅฝใใใใจใฏใใฃใใใใพใใใ' |
|
elif status == RELEVANCE: |
|
status += 1 |
|
relevance = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใไปฅไธใใพใจใใใพใจใใๆ็ซ ใ ใใ่จ่ผใใฆใใ ใใใ\n{shoujou}ใ{bui}ใ{hosoku}ใ{when}ใ{how}ใ{relevance}ใ' |
|
response = conversation.predict(input=message) |
|
history[-1][1] = response + '\nไปใพใงใใใฃใ็
ๆฐใ็พๅจๆฒป็ไธญใฎ็
ๆฐใฏใใใพใใ๏ผ' |
|
elif status == KIOU: |
|
status += 1 |
|
kiou = user_message |
|
history[-1][1] = '็พๅจ้ฃฒใพใใฆใใใ่ฌใใตใใชใกใณใใฏใใใพใใ๏ผ' |
|
elif status == FUKUYAKU: |
|
status += 1 |
|
fukuyaku = user_message |
|
history[-1][1] = 'ไฝ่ณชใซใคใใฆใไผบใใใใฆใใ ใใใ\nใป็นใซใชใ\nใปใขใฌใซใฎใผใใ\nใปไพฟ็งใใใใ\nใปไธ็ขใใใใ\nใปใใถใใใใ\nใป่ใๅผฑใ\nใปๅทใๆง\nใปใใฎไป' |
|
elif status == ALLERGY: |
|
status += 1 |
|
allergy = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใไปฅไธใใพใจใใใพใจใใๆ็ซ ใ ใใ่จ่ผใใฆใใ ใใใ{kiou}ใ{fukuyaku}, {allergy}ใ' |
|
response = conversation.predict(input=message) |
|
history[-1][1] = f'{response}\nๅปๅธซใฏใใชใใใใ็่งฃใใฆๆฒป็ใใใใใจ่ใใฆใใพใใใ\nใใชใใฎใฉใคใในใฟใคใซใๅฐใใไผบใใใใฆใใ ใใใ\nใๅฎถๆใจไธ็ทใซไฝใใงใใพใใ๏ผใใใจใไธไบบๆฎใใใงใใ๏ผ' |
|
elif status == FAMILY: |
|
status += 1 |
|
family = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใ็งใฏใๆฃ่
ๆงใซใใๅฎถๆใจไธ็ทใซไฝใใงใใพใใ๏ผใใใจใไธไบบๆฎใใใงใใ๏ผใใจ่ณชๅใใๆฃ่
ๆงใฏ\nใ{family}ใใจๅ็ญใใพใใใใใใฏ่ณชๅใซๅฏพใใ้ฉๅใชๅ็ญใงใใใใฏใใใใใใง็ญใใฆใใ ใใใ' |
|
response = conversation.predict(input=message) |
|
if 'ใฏใ' in response: |
|
history[-1][1] = 'ใฉใใชใไปไบใใใใฆใใใฃใใใใพใใใ' |
|
else: |
|
history[-1][1] = 'ๆฌกใฎ่ณชๅใธใใคใใพใใญใ\nใฉใใชใไปไบใใใใฆใใใฃใใใใพใใใ' |
|
elif status == JOB: |
|
status += 1 |
|
job = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใ็งใฏใๆฃ่
ๆงใซใใฉใใชใไปไบใใใใฆใใใฃใใใใพใใใใใจ่ณชๅใใๆฃ่
ๆงใฏ\nใ{job}ใใจๅ็ญใใพใใใใใใฏ่ณชๅใซๅฏพใใ้ฉๅใชๅ็ญใงใใใใฏใใใใใใง็ญใใฆใใ ใใใ' |
|
response = conversation.predict(input=message) |
|
if 'ใฏใ' in response: |
|
history[-1][1] = 'ใ้
ใ้ฃฒใพใใพใใ๏ผ\nใปใ้
ใฏ้ฃฒใพใชใ\nใปใ้
ใ้ฃฒใโ๏ผใ้
ใฎ็จฎ้กใป้ใป้ ปๅบฆใๆใใฆใใ ใใใไพ๏ผ็ผถใใผใซ2ๆฌใๆฏๆฅ๏ผ' |
|
else: |
|
history[-1][1] = 'ๆฌกใฎ่ณชๅใธใใคใใพใใญใ\nใ้
ใ้ฃฒใพใใพใใ๏ผ\nใปใ้
ใฏ้ฃฒใพใชใ\nใปใ้
ใ้ฃฒใโ๏ผใ้
ใฎ็จฎ้กใป้ใป้ ปๅบฆใๆใใฆใใ ใใใไพ๏ผ็ผถใใผใซ2ๆฌใๆฏๆฅ๏ผ' |
|
elif status == DRINK: |
|
status += 1 |
|
drink = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใ็งใฏใๆฃ่
ๆงใซใใ้
ใ้ฃฒใพใใพใใ๏ผใ้
ใฎ็จฎ้กใป้ใป้ ปๅบฆใๆใใฆใใ ใใใใใจ่ณชๅใใๆฃ่
ๆงใฏ\nใ{drink}ใใจๅ็ญใใพใใใใใใฏ่ณชๅใซๅฏพใใ้ฉๅใชๅ็ญใงใใใใฏใใใใใใง็ญใใฆใใ ใใใ' |
|
response = conversation.predict(input=message) |
|
if 'ใฏใ' in response: |
|
history[-1][1] = 'ใฟใใณใๅธใใใพใใ๏ผ\nใปใฟใใณใฏๅธใใชใ\nใปใฟใใณใฏไปฅๅๅธใฃใฆใใใใใใใปใฟใใณใๅธใโ๏ผไธๆฅไฝๆฌๅธใใพใใ๏ผ๏ผ' |
|
else: |
|
history[-1][1] = 'ๆฌกใฎ่ณชๅใธใใคใใพใใญใ\nใฟใใณใๅธใใใพใใ๏ผ\nใปใฟใใณใฏๅธใใชใ\nใปใฟใใณใฏไปฅๅๅธใฃใฆใใใใใใใปใฟใใณใๅธใโ๏ผไธๆฅไฝๆฌๅธใใพใใ๏ผ๏ผ' |
|
elif status == SMOKE: |
|
status += 1 |
|
smoke = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใ็งใฏใๆฃ่
ๆงใซใใฟใใณใๅธใใใพใใ๏ผไธๆฅไฝๆฌๅธใใพใใ๏ผใใจ่ณชๅใใๆฃ่
ๆงใฏ\nใ{smoke}ใใจๅ็ญใใพใใใใใใฏ่ณชๅใซๅฏพใใ้ฉๅใชๅ็ญใงใใใใฏใใใใใใง็ญใใฆใใ ใใใ' |
|
response = conversation.predict(input=message) |
|
if 'ใฏใ' in response: |
|
history[-1][1] = '้ๅใฏใใใพใใ๏ผ\nใป้ๅใฏใใชใ\nใป้ๅใใใโ๏ผใฉใใช้ๅใใใใพใใ๏ผ้ ปๅบฆใฏ๏ผ๏ผ' |
|
else: |
|
history[-1][1] = 'ๆฌกใฎ่ณชๅใธใใคใใพใใญใ\n้ๅใฏใใใพใใ๏ผ\nใป้ๅใฏใใชใ\nใป้ๅใใใโ๏ผใฉใใช้ๅใใใใพใใ๏ผ้ ปๅบฆใฏ๏ผ๏ผ' |
|
elif status == SPORTS: |
|
status += 1 |
|
sports = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใ็งใฏใๆฃ่
ๆงใซใ้ๅใฏใใใพใใ๏ผใฉใใช้ๅใใใใพใใ๏ผ้ ปๅบฆใฏ๏ผใใจ่ณชๅใใๆฃ่
ๆงใฏ\nใ{sports}ใใจๅ็ญใใพใใใใใใฏ่ณชๅใซๅฏพใใ้ฉๅใชๅ็ญใงใใใใฏใใใใใใง็ญใใฆใใ ใใใ' |
|
response = conversation.predict(input=message) |
|
if 'ใฏใ' in response: |
|
history[-1][1] = 'ๅคใฏๅๅ็ก็ ใใจใใฆใใพใใ๏ผ\nใป็ก็ ใใจใใฆใใ\nใป็ก็ ใใจใใฆใใชใโ๏ผใฉใใชใใจใงใๅฐใใงใใ๏ผ๏ผ' |
|
else: |
|
history[-1][1] = 'ๆฌกใฎ่ณชๅใธใใคใใพใใญใ\nๅคใฏๅๅ็ก็ ใใจใใฆใใพใใ๏ผ\nใป็ก็ ใใจใใฆใใ\nใป็ก็ ใใจใใฆใใชใโ๏ผใฉใใชใใจใงใๅฐใใงใใ๏ผ๏ผ' |
|
elif status == STRESS: |
|
status += 1 |
|
stress = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใไปฅไธใใพใจใใใพใจใใๆ็ซ ใ ใใ่จ่ผใใฆใใ ใใใ{family}ใ{job}ใ{drink}ใ{smoke}ใ{sports}ใ{stress}ใ{etc}ใ' |
|
response = conversation.predict(input=message) |
|
history[-1][1] = f'{response}\nๆๅพใซไฝใๅปๅธซใซใไผใใใใใใใจใใ่ใใซใชใใใใใจใฏใใใพใใใ' |
|
elif status == ETC: |
|
etc = user_message |
|
message = f'็งใฏ็่ญทๅธซใงใใไปฅไธใใพใจใใ็ฎๆกๆธใใง่จ่ผใใฆใใ ใใใ{shoujou}ใ{bui}ใ{hosoku}ใ{when}ใ{relevance}ใ{kiou}ใ{fukuyaku}ใ{allergy}ใ{family}ใ{job}ใ{drink}ใ{smoke}ใ{sports}ใ{stress}ใ{etc}ใ' |
|
history[-1][1] = conversation.predict(input=message) |
|
message = f'็งใฏ็่ญทๅธซใงใใไปฅไธใใพใจใใ็ฎๆกๆธใใง่จ่ผใใฆใใ ใใใ็็ถ๏ผ{shoujou}ใ้จไฝ๏ผ{bui}ใ่ฃ่ถณ๏ผ{hosoku}ใใใคใใ๏ผ{when}ใใใคใใจ้ใ๏ผ{relevance}ใๆขๅพ็๏ผ{kiou}ใๆ่ฌ๏ผ{fukuyaku}ใใขใฌใซใฎใผ๏ผ{allergy}ใๅฎถๆๆญด๏ผ{family}ใ่ทๆญด๏ผ{job}ใ้ฃฒ้
๏ผ{drink}ใๅซ็
๏ผ{smoke}ใ้ๅ๏ผ{sports}ใในใใฌใน๏ผ{stress}ใใใฎไป๏ผ{etc}ใ' |
|
result = conversation.predict(input=message) |
|
|
|
return history, status, suggest, shoujou, bui, hosoku, when, how, relevance, kiou, fukuyaku, allergy, family, job, drink, smoke, sports, stress, etc, result |
|
|
|
def init_firstmsg(): |
|
history = [[None, 'ๅปๅธซใซ็็ถใไผใใใจใใซๅฝน็ซใคๅ่จบๅ่จบใในใฟใผใใใพใใญ๏ผ']] |
|
history += [[None, 'ไปๆฅใฏใฉใใช็็ถใงใๅฐใใงใใ๏ผ']] |
|
history += [[None, 'ใใคใใใใฉใใชใใฉใฎใใใใฎ็็ถใใใใงใใใใใใพใใใคใใๅ ดๆใชใฉใใไผใไธใใใ']] |
|
history += [[None, 'ใใใงใฏ้ฒ้ณใใฟใณใๆผใใฆใ่ฉฑใใ ใใใ็ตใใใพใใใใใไธๅบฆ้ฒ้ณใใฟใณใๆผใใฆใใ ใใใ']] |
|
|
|
return history |
|
|
|
START = 0 |
|
SYOUJOU = 1 |
|
BUI = 2 |
|
HOSOKU = 3 |
|
WHEN = 4 |
|
HOW = 5 |
|
RELEVANCE = 6 |
|
KIOU = 7 |
|
FUKUYAKU = 8 |
|
ALLERGY = 9 |
|
FAMILY = 10 |
|
JOB = 11 |
|
DRINK = 12 |
|
SMOKE = 13 |
|
SPORTS = 14 |
|
STRESS = 15 |
|
ETC = 16 |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Column(scale=0.6): |
|
|
|
firstmsg = gr.Chatbot() |
|
audio = gr.inputs.Audio(source="microphone", type="filepath") |
|
chatbot = gr.Chatbot() |
|
msg = gr.Textbox(show_label = False) |
|
gr.Markdown('---') |
|
suggest = gr.Textbox(label = 'ๅ่ฃ', interactive = False, value = '็็ถใใจใฐใฎๅ่ฃใ่กจ็คบใใใพใใ') |
|
status = gr.Number(value = START, visible = False) |
|
shoujou = gr.Textbox(visible = False) |
|
bui = gr.Textbox(visible = False) |
|
hosoku = gr.Textbox(visible = False) |
|
when = gr.Textbox(visible = False) |
|
how = gr.Textbox(visible = False) |
|
relevance = gr.Textbox(visible = False) |
|
kiou = gr.Textbox(visible = False) |
|
fukuyaku = gr.Textbox(visible = False) |
|
allergy = gr.Textbox(visible = False) |
|
family = gr.Textbox(visible = False) |
|
job = gr.Textbox(visible = False) |
|
drink = gr.Textbox(visible = False) |
|
smoke = gr.Textbox(visible = False) |
|
sports = gr.Textbox(visible = False) |
|
stress = gr.Textbox(visible = False) |
|
etc = gr.Textbox(visible = False) |
|
result = gr.Textbox(label = 'ใพใจใ', interactive = False) |
|
|
|
demo.load(init_firstmsg, None, firstmsg, queue=False) |
|
|
|
audio.change(speechRecognitionModel, inputs=[audio, chatbot, shoujou, bui, when, how, status], outputs=[chatbot, shoujou, bui, when, how, status]) |
|
|
|
|
|
|
|
|
|
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( |
|
chat, [chatbot, status, suggest, shoujou, bui, hosoku, when, how, relevance, kiou, fukuyaku, allergy, family, job, drink, smoke, sports, stress, etc, result], |
|
[chatbot, status, suggest, shoujou, bui, hosoku, when, how, relevance, kiou, fukuyaku, allergy, family, job, drink, smoke, sports, stress, etc, result] |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|