shanribot / app.py
ssatonet's picture
fix
01a4bb8
raw
history blame
17.4 kB
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
# APIใ‚ญใƒผใฎ่จญๅฎš
#os.environ["OPENAI_API_KEY"] = ""
openai.api_key = os.getenv('OPENAI_API_KEY')
os.environ["OPENAI_API_KEY"] = openai.api_key
# ๆคœ็ดขAPIURL
search_url = 'https://greendb.shanri.co.jp/gd/search/'
# ่จญๅฎšใƒ—ใƒญใƒณใƒ—ใƒˆ
character_setting = "ใ‚ใชใŸใฏ20ไปฃใฎ็œ‹่ญทๅธซใ•ใ‚“ใงใ™ใ€‚ๆ˜Žใ‚‹ใ„ๆ€งๆ ผใงไธๅฏงใชๅฃ่ชฟใงๅ›ž็ญ”ใ—ใฆใใ ใ•ใ„"
def get_search_result(kw):
# getใƒกใ‚ฝใƒƒใƒ‰ใงsearch urlใซๅฏพใ—ใฆGETใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้€ใ‚‹
r = requests.get(search_url, params={'kw':kw})
# JSONใƒฌใ‚นใƒใƒณใ‚นใ‹ใ‚‰kotoba_shoujouใ‚’ใ™ในใฆๅ–ๅพ—
kotoba_shoujou = []
for hit in r.json()['hits']:
kotoba_shoujou.append(hit['_source']['kotoba_shoujou'])
# 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])
# audio.change(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])
# demo.load(init_chatbot, None, chatbot, queue=False)
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()