forked from whitphx/streamlit-webrtc-article-tutorial-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interview_chat.py
64 lines (51 loc) · 1.93 KB
/
interview_chat.py
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
from langchain. chat_models import ChatOpenAI
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import (
HumanMessage,
)
from langchain.chains import ConversationChain, LLMChain
import openai
from typing import Any, Dict, List
import streamlit as st
import os
import requests
import av
from generate_questions import generate_questions
from services import text_to_speech
import speech_recognition as sr
#これもAsyncにできる?
class SentenceCallbackHandler(BaseCallbackHandler):
""" Sentence Callback Handler """
def __init__(self, state_obj, model='tts-1', voice='alloy', response_format="opus") -> None:
self.state_obj= state_obj
self.sentence = ''
self.model = model
self.voice = voice
self.response_format = response_format
def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
"""Run on new LLM token. Only available when streaming is enabled."""
self.sentence += str(token)
#一文になったら非同期でself.sentenceをtext_to_speechに(下は仮)
if token in ['.','?','!']:
#print(self.sentence)
text_to_speech(self.state_obj, self.sentence, self.model, self.voice, self.response_format)
self.sentence = ''
def on_llm_end(self, response, **kwargs: Any) -> None:
"""Run on LLM end. Only available when streaming is enabled."""
st.write(response)
self.state_obj.set_is_finished(True)
profile = {
'model': 'tts-1',
'voice': 'alloy',
'response_format': "opus",
}
def generate_response(prompt, user_input, state, state_obj):
handler = SentenceCallbackHandler(state_obj, **profile)
llm = ChatOpenAI(streaming=True, temperature=0.9, callbacks=[handler])
conversation = LLMChain(
llm=llm,
prompt=prompt,
memory=state['memory']
)
res = conversation.predict(input=user_input)
return res