DiamondYin commited on
Commit
ddef685
1 Parent(s): e6c45ad

update0720

Browse files
Files changed (3) hide show
  1. app.py +8 -8
  2. app_utils.py +13 -13
  3. profiles/john.txt +1 -1
app.py CHANGED
@@ -3,30 +3,30 @@ import nltk
3
  import openai
4
  import time
5
  import gradio as gr
6
- from threading import Thread
7
 
8
- from assets.char_poses_base64 import (
9
  CHAR_IDLE_HTML, CHAR_THINKING_HTML, CHAR_TALKING_HTML)
10
 
11
  from app_utils import (
12
  get_chat_history, initialize_knowledge_base,
13
  text_to_speech_gen, logging, buzz_user)
14
 
15
- global FUNC_CALL
16
  FUNC_CALL = 0
17
 
18
- global BUZZ_TIMEOUT
19
  BUZZ_TIMEOUT = 60
20
 
21
  GENERAL_RSPONSE_TRIGGERS = ["I don't understand the question.", "I don't know", "Hello, my name is", "mentioned in the context provided"]
22
- MESSAGES = [{"role": "system", "content": "You are a helpful assistant.."}]
23
 
24
- LOGGER = logging.getLogger('voice_agent')
25
  AUDIO_HTML = ''
26
 
27
  # Uncomment If this is your first Run:
28
- nltk.download('averaged_perceptron_tagger')
29
- conv_model, voice_model = initialize_knowledge_base()
30
 
31
 
32
  def idle_timer():
 
3
  import openai
4
  import time
5
  import gradio as gr
6
+ from threading import Thread #线程 用于定时器
7
 
8
+ from assets.char_poses_base64 import ( #角色动作
9
  CHAR_IDLE_HTML, CHAR_THINKING_HTML, CHAR_TALKING_HTML)
10
 
11
  from app_utils import (
12
  get_chat_history, initialize_knowledge_base,
13
  text_to_speech_gen, logging, buzz_user)
14
 
15
+ global FUNC_CALL #全局变量 用于判断角色动作
16
  FUNC_CALL = 0
17
 
18
+ global BUZZ_TIMEOUT #全局变量 用于定时器
19
  BUZZ_TIMEOUT = 60
20
 
21
  GENERAL_RSPONSE_TRIGGERS = ["I don't understand the question.", "I don't know", "Hello, my name is", "mentioned in the context provided"]
22
+ MESSAGES = [{"role": "system", "content": "You are a helpful assistant.You accompany me to practice English and engage in scene dialogue. As a hotel attendant, I am checking in. You introduce the hotel to me and recommend hotel services to me. After receiving my needs, arrange for the service personnel to work. Please remember, my English is not very good. Please have a conversation with me in simple English. After you ask questions in English, please give me some English prompts so that I know how to answer you. Let's start the conversation. You first say hello to me."}]
23
 
24
+ LOGGER = logging.getLogger('voice_agent') #日志
25
  AUDIO_HTML = ''
26
 
27
  # Uncomment If this is your first Run:
28
+ nltk.download('averaged_perceptron_tagger') #下载语料库
29
+ conv_model, voice_model = initialize_knowledge_base() #初始化知识库
30
 
31
 
32
  def idle_timer():
app_utils.py CHANGED
@@ -1,18 +1,18 @@
1
  import os
2
  import whisper
3
- from io import BytesIO
4
- import base64
5
- import boto3
6
- from pydub import AudioSegment
7
- from pydub.playback import play
8
  import logging
9
 
10
  from langchain import OpenAI
11
- from langchain.chains import RetrievalQA
12
- from langchain.vectorstores import Chroma
13
- from langchain.document_loaders import DirectoryLoader
14
- from langchain.embeddings.openai import OpenAIEmbeddings
15
- from langchain.text_splitter import CharacterTextSplitter
16
 
17
 
18
  OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
@@ -35,7 +35,7 @@ def buzz_user():
35
 
36
  def initialize_knowledge_base():
37
 
38
- loader = DirectoryLoader('profiles', glob='**/*.txt')
39
  docs = loader.load()
40
 
41
  char_text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
@@ -56,7 +56,7 @@ def initialize_knowledge_base():
56
  return conv_model, voice_model
57
 
58
 
59
- def text_to_speech_gen(answer):
60
 
61
  polly = boto3.client('polly',
62
  aws_access_key_id=AWS_ACCESS_KEY_ID,
@@ -75,7 +75,7 @@ def text_to_speech_gen(answer):
75
  return audio_html
76
 
77
 
78
- def audio_to_html(audio_bytes):
79
  audio_io = BytesIO(audio_bytes)
80
  audio_io.seek(0)
81
  audio_base64 = base64.b64encode(audio_io.read()).decode("utf-8")
 
1
  import os
2
  import whisper
3
+ from io import BytesIO # BytesIO is a class in the io module that implements an in-memory file-like object.
4
+ import base64
5
+ import boto3 # AWS Polly
6
+ from pydub import AudioSegment # AudioSegment is a class in the pydub module that can be used to manipulate audio files.
7
+ from pydub.playback import play # play is a function in the pydub.playback module that can be used to play audio files.
8
  import logging
9
 
10
  from langchain import OpenAI
11
+ from langchain.chains import RetrievalQA # RetrievalQA is a class in the langchain.chains module that can be used to build a retrieval-based question answering system.
12
+ from langchain.vectorstores import Chroma # Chroma is a class in the langchain.vectorstores module that can be used to store vectors.
13
+ from langchain.document_loaders import DirectoryLoader #
14
+ from langchain.embeddings.openai import OpenAIEmbeddings # OpenAIGPTEmbeddings
15
+ from langchain.text_splitter import CharacterTextSplitter # CharacterTextSplitter is a class in the langchain.text_splitter module that can be used to split text into chunks.
16
 
17
 
18
  OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
 
35
 
36
  def initialize_knowledge_base():
37
 
38
+ loader = DirectoryLoader('profiles', glob='**/*.txt') #文件夹加载器 profiles文件夹下的所有txt文件
39
  docs = loader.load()
40
 
41
  char_text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
 
56
  return conv_model, voice_model
57
 
58
 
59
+ def text_to_speech_gen(answer): #文字转语音
60
 
61
  polly = boto3.client('polly',
62
  aws_access_key_id=AWS_ACCESS_KEY_ID,
 
75
  return audio_html
76
 
77
 
78
+ def audio_to_html(audio_bytes): #音频转html
79
  audio_io = BytesIO(audio_bytes)
80
  audio_io.seek(0)
81
  audio_base64 = base64.b64encode(audio_io.read()).decode("utf-8")
profiles/john.txt CHANGED
@@ -3,7 +3,7 @@ you were born in 12th March 1958
3
  John is at the shop
4
  Brian is on holiday
5
  Sarah is coming home soon
6
- Sarah is net door and has left
7
  You have granddaughters, jennny and michelle, they are 7 and 10
8
  Your cousin Arthur is in Australia, he’s back next July
9
  Your neighbuor Steven passed away a few days ago
 
3
  John is at the shop
4
  Brian is on holiday
5
  Sarah is coming home soon
6
+ Sarah is net door and has left
7
  You have granddaughters, jennny and michelle, they are 7 and 10
8
  Your cousin Arthur is in Australia, he’s back next July
9
  Your neighbuor Steven passed away a few days ago