Spaces:
Sleeping
Sleeping
File size: 4,558 Bytes
d20eb01 dc2726e d20eb01 dc2726e d20eb01 dc2726e d20eb01 |
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 |
import datetime
from threading import Lock
from typing import Tuple, Optional
from langchain import ConversationChain, LLMChain
from config.config import MAX_TALKING_HEAD_TEXT_LENGTH, LOOPING_TALKING_HEAD_VIDEO_PATH, TALKING_HEAD_WIDTH
from utilities.html_stuff import do_html_video_speak, create_html_video, do_html_audio_speak
from utilities.transform_text import transform_text
def reset_memory(history, memory):
memory.clear()
history = []
return history, history, memory
class ChatWrapper:
def __init__(self):
self.lock = Lock()
def __call__(
self, api_key: str, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain],
trace_chain: bool, speak_text: bool, talking_head: bool, monologue: bool, express_chain: Optional[LLMChain],
num_words, formality, anticipation_level, joy_level, trust_level,
fear_level, surprise_level, sadness_level, disgust_level, anger_level,
lang_level, translate_to, literary_style, qa_chain, docsearch, use_embeddings, force_translate
):
"""Execute the chat functionality."""
self.lock.acquire()
try:
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
print("inp: " + inp)
print("trace_chain: ", trace_chain)
print("speak_text: ", speak_text)
print("talking_head: ", talking_head)
print("monologue: ", monologue)
history = history or []
# If chain is None, that is because no API key was provided.
output = "Please paste your OpenAI key from openai.com to use this app. " + str(datetime.datetime.now())
hidden_text = output
if chain:
# Set OpenAI key
import openai
openai.api_key = api_key
if not monologue:
if use_embeddings:
if inp and inp.strip() != "":
if docsearch:
docs = docsearch.similarity_search(inp)
output = str(qa_chain.run(input_documents=docs, question=inp))
else:
output, hidden_text = "Please supply some text in the the Embeddings tab.", None
else:
output, hidden_text = "What's on your mind?", None
else:
output, hidden_text = run_chain(chain, inp, capture_hidden_text=trace_chain)
else:
output, hidden_text = inp, None
output = transform_text(output, express_chain, num_words, formality, anticipation_level, joy_level,
trust_level,
fear_level, surprise_level, sadness_level, disgust_level, anger_level,
lang_level, translate_to, literary_style, force_translate)
text_to_display = output
if trace_chain:
text_to_display = hidden_text + "\n\n" + output
history.append((inp, text_to_display))
html_video, temp_file, html_audio, temp_aud_file = None, None, None, None
if speak_text:
if talking_head:
if len(output) <= MAX_TALKING_HEAD_TEXT_LENGTH:
html_video, temp_file = do_html_video_speak(output, translate_to)
else:
temp_file = LOOPING_TALKING_HEAD_VIDEO_PATH
html_video = create_html_video(temp_file, TALKING_HEAD_WIDTH)
html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
else:
html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
else:
if talking_head:
temp_file = LOOPING_TALKING_HEAD_VIDEO_PATH
html_video = create_html_video(temp_file, TALKING_HEAD_WIDTH)
else:
# html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
# html_video = create_html_video(temp_file, "128")
pass
except Exception as e:
raise e
finally:
self.lock.release()
return history, history, html_video, temp_file, html_audio, temp_aud_file, ""
# return history, history, html_audio, temp_aud_file, ""
|