File size: 17,326 Bytes
f72b0d9 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
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')
# ๆค็ดข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()
|