forked from myreader-io/myGPTReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
208 lines (184 loc) · 8.5 KB
/
gpt.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
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
import os
import logging
import hashlib
import random
import uuid
import openai
from pathlib import Path
from llama_index import GPTSimpleVectorIndex, LLMPredictor, RssReader, SimpleDirectoryReader
from llama_index.readers.schema.base import Document
from langchain.chat_models import ChatOpenAI
from azure.cognitiveservices.speech import SpeechConfig, SpeechSynthesizer, ResultReason, CancellationReason, SpeechSynthesisOutputFormat
from azure.cognitiveservices.speech.audio import AudioOutputConfig
from app.fetch_web_post import get_urls, get_youtube_transcript, scrape_website, scrape_website_by_phantomjscloud
from app.prompt import get_prompt_template
from app.util import get_language_code, get_youtube_video_id
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
SPEECH_KEY = os.environ.get('SPEECH_KEY')
SPEECH_REGION = os.environ.get('SPEECH_REGION')
openai.api_key = OPENAI_API_KEY
llm_predictor = LLMPredictor(llm=ChatOpenAI(
temperature=0.2, model_name="gpt-3.5-turbo"))
index_cache_web_dir = Path('/tmp/myGPTReader/cache_web/')
index_cache_voice_dir = Path('/tmp/myGPTReader/voice/')
index_cache_file_dir = Path('/data/myGPTReader/file/')
if not index_cache_web_dir.is_dir():
index_cache_web_dir.mkdir(parents=True, exist_ok=True)
if not index_cache_voice_dir.is_dir():
index_cache_voice_dir.mkdir(parents=True, exist_ok=True)
if not index_cache_file_dir.is_dir():
index_cache_file_dir.mkdir(parents=True, exist_ok=True)
def get_unique_md5(urls):
urls_str = ''.join(sorted(urls))
hashed_str = hashlib.md5(urls_str.encode('utf-8')).hexdigest()
return hashed_str
def format_dialog_messages(messages):
return "\n".join(messages)
def get_document_from_youtube_id(video_id):
if video_id is None:
return None
transcript = get_youtube_transcript(video_id)
if transcript is None:
return None
return Document(transcript)
def remove_prompt_from_text(text):
return text.replace('chatGPT:', '').strip()
def get_documents_from_urls(urls):
documents = []
for url in urls['page_urls']:
document = Document(scrape_website(url))
documents.append(document)
if len(urls['rss_urls']) > 0:
rss_documents = RssReader().load_data(urls['rss_urls'])
documents = documents + rss_documents
if len(urls['phantomjscloud_urls']) > 0:
for url in urls['phantomjscloud_urls']:
document = Document(scrape_website_by_phantomjscloud(url))
documents.append(document)
if len(urls['youtube_urls']) > 0:
for url in urls['youtube_urls']:
video_id = get_youtube_video_id(url)
document = get_document_from_youtube_id(video_id)
if (document is not None):
documents.append(document)
else:
documents.append(Document(f"Can't get transcript from youtube video: {url}"))
return documents
def get_answer_from_chatGPT(messages):
dialog_messages = format_dialog_messages(messages)
logging.info('=====> Use chatGPT to answer!')
logging.info(dialog_messages)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": dialog_messages}]
)
logging.info(completion.usage)
return completion.choices[0].message.content
def get_index_from_web_cache(name):
web_cache_file = index_cache_web_dir / name
if not web_cache_file.is_file():
return None
index = GPTSimpleVectorIndex.load_from_disk(web_cache_file)
logging.info(
f"=====> Get index from web cache: {web_cache_file}")
return index
def get_index_from_file_cache(name):
file_cache_file = index_cache_file_dir / name
if not file_cache_file.is_file():
return None
index = GPTSimpleVectorIndex.load_from_disk(file_cache_file)
logging.info(
f"=====> Get index from file cache: {file_cache_file}")
return index
def get_answer_from_llama_web(messages, urls):
dialog_messages = format_dialog_messages(messages)
lang_code = get_language_code(remove_prompt_from_text(messages[-1]))
combained_urls = get_urls(urls)
logging.info(combained_urls)
index_file_name = get_unique_md5(urls)
index = get_index_from_web_cache(index_file_name)
if index is None:
logging.info(f"=====> Build index from web!")
documents = get_documents_from_urls(combained_urls)
logging.info(documents)
index = GPTSimpleVectorIndex(documents)
logging.info(
f"=====> Save index to disk path: {index_cache_web_dir / index_file_name}")
index.save_to_disk(index_cache_web_dir / index_file_name)
prompt = get_prompt_template(lang_code)
logging.info('=====> Use llama web with chatGPT to answer!')
logging.info('=====> dialog_messages')
logging.info(dialog_messages)
logging.info('=====> text_qa_template')
logging.info(prompt)
return index.query(dialog_messages, llm_predictor=llm_predictor, text_qa_template=prompt)
def get_index_name_from_file(file: str):
file_md5_with_extension = str(Path(file).relative_to(index_cache_file_dir).name)
file_md5 = file_md5_with_extension.split('.')[0]
return file_md5 + '.json'
def get_answer_from_llama_file(messages, file):
dialog_messages = format_dialog_messages(messages)
lang_code = get_language_code(remove_prompt_from_text(messages[-1]))
index_name = get_index_name_from_file(file)
index = get_index_from_file_cache(index_name)
if index is None:
logging.info(f"=====> Build index from file!")
documents = SimpleDirectoryReader(input_files=[file]).load_data()
index = GPTSimpleVectorIndex(documents)
logging.info(
f"=====> Save index to disk path: {index_cache_file_dir / index_name}")
index.save_to_disk(index_cache_file_dir / index_name)
prompt = get_prompt_template(lang_code)
logging.info('=====> Use llama file with chatGPT to answer!')
logging.info('=====> dialog_messages')
logging.info(dialog_messages)
logging.info('=====> text_qa_template')
logging.info(prompt)
return index.query(dialog_messages, llm_predictor=llm_predictor, text_qa_template=prompt)
def get_text_from_whisper(voice_file_path):
with open(voice_file_path, "rb") as f:
transcript = openai.Audio.transcribe("whisper-1", f)
return transcript.text
lang_code_voice_map = {
'zh': ['zh-CN-XiaoxiaoNeural', 'zh-CN-XiaohanNeural', 'zh-CN-YunxiNeural', 'zh-CN-YunyangNeural'],
'en': ['en-US-JennyNeural', 'en-US-RogerNeural', 'en-IN-NeerjaNeural', 'en-IN-PrabhatNeural', 'en-AU-AnnetteNeural', 'en-AU-CarlyNeural', 'en-GB-AbbiNeural', 'en-GB-AlfieNeural'],
'ja': ['ja-JP-AoiNeural', 'ja-JP-DaichiNeural'],
'de': ['de-DE-AmalaNeural', 'de-DE-BerndNeural'],
}
def convert_to_ssml(text, voice_name=None):
try:
logging.info("=====> Convert text to ssml!")
logging.info(text)
text = remove_prompt_from_text(text)
lang_code = get_language_code(text)
if voice_name is None:
voice_name = random.choice(lang_code_voice_map[lang_code])
except Exception as e:
logging.warning(f"Error: {e}. Using default voice.")
voice_name = random.choice(lang_code_voice_map['zh'])
ssml = '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="zh-CN">'
ssml += f'<voice name="{voice_name}">{text}</voice>'
ssml += '</speak>'
return ssml
def get_voice_file_from_text(text, voice_name=None):
speech_config = SpeechConfig(subscription=SPEECH_KEY, region=SPEECH_REGION)
speech_config.set_speech_synthesis_output_format(
SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3)
speech_config.speech_synthesis_language = "zh-CN"
file_name = f"{index_cache_voice_dir}{uuid.uuid4()}.mp3"
file_config = AudioOutputConfig(filename=file_name)
synthesizer = SpeechSynthesizer(
speech_config=speech_config, audio_config=file_config)
ssml = convert_to_ssml(text, voice_name)
result = synthesizer.speak_ssml_async(ssml).get()
if result.reason == ResultReason.SynthesizingAudioCompleted:
logging.info("Speech synthesized for text [{}], and the audio was saved to [{}]".format(
text, file_name))
elif result.reason == ResultReason.Canceled:
cancellation_details = result.cancellation_details
logging.info("Speech synthesis canceled: {}".format(
cancellation_details.reason))
if cancellation_details.reason == CancellationReason.Error:
logging.error("Error details: {}".format(
cancellation_details.error_details))
return file_name