Spaces:
Sleeping
Sleeping
Commit
Β·
dc2726e
1
Parent(s):
d20eb01
push for posterity
Browse files- app.py +10 -15
- {audios β assets/audios}/tempfile.mp3 +0 -0
- {images β assets/images}/Masahiro.png +0 -0
- {videos β assets/videos}/Masahiro.mp4 +0 -0
- {videos β assets/videos}/tempfile.mp4 +0 -0
- config/config.py +3 -5
- utilities/html_stuff.py +6 -7
- utilities/reset_memory.py +3 -3
- utilities/run_chain.py +3 -3
- utilities/update_things.py +0 -1
app.py
CHANGED
@@ -1,20 +1,14 @@
|
|
1 |
-
import io
|
2 |
import os
|
3 |
import ssl
|
4 |
-
from contextlib import closing
|
5 |
|
6 |
-
import boto3
|
7 |
import gradio as gr
|
8 |
-
import
|
9 |
-
# Pertains to Express-inator functionality
|
10 |
-
from langchain.prompts import PromptTemplate
|
11 |
|
12 |
from config.config import TALKING_HEAD_WIDTH, TOOLS_DEFAULT_LIST, NUM_WORDS_DEFAULT, FORMALITY_DEFAULT, EMOTION_DEFAULT, \
|
13 |
-
LANG_LEVEL_DEFAULT, TRANSLATE_TO_DEFAULT, LITERARY_STYLE_DEFAULT,
|
|
|
14 |
from utilities.audio import FORCE_TRANSLATE_DEFAULT, USE_GPT4_DEFAULT, transcribe, WHISPER_DETECT_LANG
|
15 |
-
from utilities.audio import POLLY_VOICE_DATA, AZURE_VOICE_DATA
|
16 |
from utilities.html_stuff import create_html_video, update_talking_head
|
17 |
-
from utilities.polly_utils import NEURAL_ENGINE
|
18 |
from utilities.reset_memory import ChatWrapper, reset_memory
|
19 |
from utilities.set_openai_api_key import set_openai_api_key
|
20 |
from utilities.update_things import update_foo, update_selected_tools, update_use_embeddings, \
|
@@ -23,8 +17,6 @@ from utilities.update_things import update_foo, update_selected_tools, update_us
|
|
23 |
# Temporarily address Wolfram Alpha SSL certificate issue
|
24 |
ssl._create_default_https_context = ssl._create_unverified_context
|
25 |
|
26 |
-
|
27 |
-
|
28 |
chat = ChatWrapper()
|
29 |
|
30 |
with gr.Blocks(css=".gradio-container {background-color: lightgray}") as block:
|
@@ -36,7 +28,7 @@ with gr.Blocks(css=".gradio-container {background-color: lightgray}") as block:
|
|
36 |
trace_chain_state = gr.State(False)
|
37 |
speak_text_state = gr.State(False)
|
38 |
talking_head_state = gr.State(True)
|
39 |
-
monologue_state = gr.State(False) # Takes the input and repeats it back to
|
40 |
force_translate_state = gr.State(FORCE_TRANSLATE_DEFAULT) #
|
41 |
memory_state = gr.State()
|
42 |
|
@@ -74,8 +66,11 @@ with gr.Blocks(css=".gradio-container {background-color: lightgray}") as block:
|
|
74 |
<p><center>Hit Enter after pasting your OpenAI API key.</center></p>
|
75 |
<i><center>If you have GPT-4 access, optionally select it in Settings tab.</center></i>""")
|
76 |
|
|
|
77 |
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...) and hit Enter",
|
78 |
-
|
|
|
|
|
79 |
|
80 |
with gr.Row():
|
81 |
with gr.Column(scale=1, min_width=TALKING_HEAD_WIDTH, visible=True):
|
@@ -86,11 +81,11 @@ with gr.Blocks(css=".gradio-container {background-color: lightgray}") as block:
|
|
86 |
my_file = gr.File(label="Upload a file", type="file", visible=False)
|
87 |
|
88 |
# tmp_file_url = "/file=" + tmp_file.value['name']
|
89 |
-
html_video = create_html_video(
|
90 |
video_html = gr.HTML(html_video)
|
91 |
|
92 |
# my_aud_file = gr.File(label="Audio file", type="file", visible=True)
|
93 |
-
tmp_aud_file = gr.File("audios/tempfile.mp3", visible=False)
|
94 |
tmp_aud_file_url = "/file=" + tmp_aud_file.value['name']
|
95 |
htm_audio = f'<audio><source src={tmp_aud_file_url} type="audio/mp3"></audio>'
|
96 |
audio_html = gr.HTML(htm_audio)
|
|
|
|
|
1 |
import os
|
2 |
import ssl
|
|
|
3 |
|
|
|
4 |
import gradio as gr
|
5 |
+
from dotenv import load_dotenv
|
|
|
|
|
6 |
|
7 |
from config.config import TALKING_HEAD_WIDTH, TOOLS_DEFAULT_LIST, NUM_WORDS_DEFAULT, FORMALITY_DEFAULT, EMOTION_DEFAULT, \
|
8 |
+
LANG_LEVEL_DEFAULT, TRANSLATE_TO_DEFAULT, LITERARY_STYLE_DEFAULT, LOOPING_TALKING_HEAD_VIDEO_PATH, TOOLS_LIST, \
|
9 |
+
MAX_WORDS
|
10 |
from utilities.audio import FORCE_TRANSLATE_DEFAULT, USE_GPT4_DEFAULT, transcribe, WHISPER_DETECT_LANG
|
|
|
11 |
from utilities.html_stuff import create_html_video, update_talking_head
|
|
|
12 |
from utilities.reset_memory import ChatWrapper, reset_memory
|
13 |
from utilities.set_openai_api_key import set_openai_api_key
|
14 |
from utilities.update_things import update_foo, update_selected_tools, update_use_embeddings, \
|
|
|
17 |
# Temporarily address Wolfram Alpha SSL certificate issue
|
18 |
ssl._create_default_https_context = ssl._create_unverified_context
|
19 |
|
|
|
|
|
20 |
chat = ChatWrapper()
|
21 |
|
22 |
with gr.Blocks(css=".gradio-container {background-color: lightgray}") as block:
|
|
|
28 |
trace_chain_state = gr.State(False)
|
29 |
speak_text_state = gr.State(False)
|
30 |
talking_head_state = gr.State(True)
|
31 |
+
monologue_state = gr.State(False) # Takes the input and repeats it back to themp4 user, optionally transforming it.
|
32 |
force_translate_state = gr.State(FORCE_TRANSLATE_DEFAULT) #
|
33 |
memory_state = gr.State()
|
34 |
|
|
|
66 |
<p><center>Hit Enter after pasting your OpenAI API key.</center></p>
|
67 |
<i><center>If you have GPT-4 access, optionally select it in Settings tab.</center></i>""")
|
68 |
|
69 |
+
load_dotenv()
|
70 |
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...) and hit Enter",
|
71 |
+
value=os.environ.get("OPENAI_API_KEY", ""),
|
72 |
+
show_label=False,
|
73 |
+
lines=1, type='password')
|
74 |
|
75 |
with gr.Row():
|
76 |
with gr.Column(scale=1, min_width=TALKING_HEAD_WIDTH, visible=True):
|
|
|
81 |
my_file = gr.File(label="Upload a file", type="file", visible=False)
|
82 |
|
83 |
# tmp_file_url = "/file=" + tmp_file.value['name']
|
84 |
+
html_video = create_html_video(LOOPING_TALKING_HEAD_VIDEO_PATH, TALKING_HEAD_WIDTH)
|
85 |
video_html = gr.HTML(html_video)
|
86 |
|
87 |
# my_aud_file = gr.File(label="Audio file", type="file", visible=True)
|
88 |
+
tmp_aud_file = gr.File("assets/audios/tempfile.mp3", visible=False)
|
89 |
tmp_aud_file_url = "/file=" + tmp_aud_file.value['name']
|
90 |
htm_audio = f'<audio><source src={tmp_aud_file_url} type="audio/mp3"></audio>'
|
91 |
audio_html = gr.HTML(htm_audio)
|
{audios β assets/audios}/tempfile.mp3
RENAMED
File without changes
|
{images β assets/images}/Masahiro.png
RENAMED
File without changes
|
{videos β assets/videos}/Masahiro.mp4
RENAMED
File without changes
|
{videos β assets/videos}/tempfile.mp4
RENAMED
File without changes
|
config/config.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
from langchain import PromptTemplate
|
4 |
|
5 |
TALKING_HEAD_WIDTH = 192
|
@@ -12,12 +10,12 @@ TOOLS_LIST = ['serpapi', 'wolfram-alpha', 'pal-math',
|
|
12 |
TOOLS_DEFAULT_LIST = ['serpapi']
|
13 |
BUG_FOUND_MSG = "Congratulations, you've found a bug in this application!"
|
14 |
# AUTH_ERR_MSG = "Please paste your OpenAI key from openai.com to use this application. It is not necessary to hit a button or key after pasting it."
|
15 |
-
|
16 |
MAX_TOKENS = 512
|
17 |
|
18 |
-
|
19 |
|
20 |
-
MAX_TALKING_HEAD_TEXT_LENGTH =
|
21 |
|
22 |
# Pertains to Express-inator functionality
|
23 |
NUM_WORDS_DEFAULT = 0
|
|
|
|
|
|
|
1 |
from langchain import PromptTemplate
|
2 |
|
3 |
TALKING_HEAD_WIDTH = 192
|
|
|
10 |
TOOLS_DEFAULT_LIST = ['serpapi']
|
11 |
BUG_FOUND_MSG = "Congratulations, you've found a bug in this application!"
|
12 |
# AUTH_ERR_MSG = "Please paste your OpenAI key from openai.com to use this application. It is not necessary to hit a button or key after pasting it."
|
13 |
+
AUTHORIZATION_ERROR_MESSAGE = "Please paste your OpenAI key from openai.com to use this application. "
|
14 |
MAX_TOKENS = 512
|
15 |
|
16 |
+
LOOPING_TALKING_HEAD_VIDEO_PATH = "assets/videos/Masahiro.mp4"
|
17 |
|
18 |
+
MAX_TALKING_HEAD_TEXT_LENGTH = 1550
|
19 |
|
20 |
# Pertains to Express-inator functionality
|
21 |
NUM_WORDS_DEFAULT = 0
|
utilities/html_stuff.py
CHANGED
@@ -4,16 +4,15 @@ from contextlib import closing
|
|
4 |
|
5 |
import boto3
|
6 |
import gradio as gr
|
7 |
-
|
8 |
import requests
|
9 |
|
10 |
-
from config.config import TALKING_HEAD_WIDTH,
|
11 |
from utilities.audio import AZURE_VOICE_DATA, POLLY_VOICE_DATA
|
12 |
from utilities.polly_utils import NEURAL_ENGINE
|
13 |
|
14 |
|
15 |
def create_html_video(file_name, width):
|
16 |
-
tmp_file = gr.File(
|
17 |
temp_file_url = "/file=" + tmp_file.value['name']
|
18 |
html_video = f'<video width={width} height={width} autoplay muted loop><source src={temp_file_url} type="video/mp4" poster="Masahiro.png"></video>'
|
19 |
return html_video
|
@@ -22,7 +21,7 @@ def update_talking_head(widget, state):
|
|
22 |
if widget:
|
23 |
state = widget
|
24 |
|
25 |
-
video_html_talking_head = create_html_video(
|
26 |
return state, video_html_talking_head
|
27 |
else:
|
28 |
# return state, create_html_video(LOOPING_TALKING_HEAD, "32")
|
@@ -60,9 +59,9 @@ def do_html_audio_speak(words_to_speak, polly_language):
|
|
60 |
# output = os.path.join("/tmp/", "speech.mp3")
|
61 |
|
62 |
try:
|
63 |
-
with open('audios/tempfile.mp3', 'wb') as f:
|
64 |
f.write(stream.read())
|
65 |
-
temp_aud_file = gr.File("audios/tempfile.mp3")
|
66 |
temp_aud_file_url = "/file=" + temp_aud_file.value['name']
|
67 |
html_audio = f'<audio autoplay><source src={temp_aud_file_url} type="audio/mp3"></audio>'
|
68 |
except IOError as error:
|
@@ -74,7 +73,7 @@ def do_html_audio_speak(words_to_speak, polly_language):
|
|
74 |
print("Could not stream audio")
|
75 |
return None, None
|
76 |
|
77 |
-
return html_audio, "audios/tempfile.mp3"
|
78 |
|
79 |
|
80 |
|
|
|
4 |
|
5 |
import boto3
|
6 |
import gradio as gr
|
|
|
7 |
import requests
|
8 |
|
9 |
+
from config.config import TALKING_HEAD_WIDTH, LOOPING_TALKING_HEAD_VIDEO_PATH
|
10 |
from utilities.audio import AZURE_VOICE_DATA, POLLY_VOICE_DATA
|
11 |
from utilities.polly_utils import NEURAL_ENGINE
|
12 |
|
13 |
|
14 |
def create_html_video(file_name, width):
|
15 |
+
tmp_file = gr.File(LOOPING_TALKING_HEAD_VIDEO_PATH, visible=False)
|
16 |
temp_file_url = "/file=" + tmp_file.value['name']
|
17 |
html_video = f'<video width={width} height={width} autoplay muted loop><source src={temp_file_url} type="video/mp4" poster="Masahiro.png"></video>'
|
18 |
return html_video
|
|
|
21 |
if widget:
|
22 |
state = widget
|
23 |
|
24 |
+
video_html_talking_head = create_html_video(LOOPING_TALKING_HEAD_VIDEO_PATH, TALKING_HEAD_WIDTH)
|
25 |
return state, video_html_talking_head
|
26 |
else:
|
27 |
# return state, create_html_video(LOOPING_TALKING_HEAD, "32")
|
|
|
59 |
# output = os.path.join("/tmp/", "speech.mp3")
|
60 |
|
61 |
try:
|
62 |
+
with open('assets/audios/tempfile.mp3', 'wb') as f:
|
63 |
f.write(stream.read())
|
64 |
+
temp_aud_file = gr.File("assets/audios/tempfile.mp3")
|
65 |
temp_aud_file_url = "/file=" + temp_aud_file.value['name']
|
66 |
html_audio = f'<audio autoplay><source src={temp_aud_file_url} type="audio/mp3"></audio>'
|
67 |
except IOError as error:
|
|
|
73 |
print("Could not stream audio")
|
74 |
return None, None
|
75 |
|
76 |
+
return html_audio, "assets/audios/tempfile.mp3"
|
77 |
|
78 |
|
79 |
|
utilities/reset_memory.py
CHANGED
@@ -4,7 +4,7 @@ from typing import Tuple, Optional
|
|
4 |
|
5 |
from langchain import ConversationChain, LLMChain
|
6 |
|
7 |
-
from config.config import MAX_TALKING_HEAD_TEXT_LENGTH,
|
8 |
from utilities.html_stuff import do_html_video_speak, create_html_video, do_html_audio_speak
|
9 |
from utilities.transform_text import transform_text
|
10 |
|
@@ -76,14 +76,14 @@ class ChatWrapper:
|
|
76 |
if len(output) <= MAX_TALKING_HEAD_TEXT_LENGTH:
|
77 |
html_video, temp_file = do_html_video_speak(output, translate_to)
|
78 |
else:
|
79 |
-
temp_file =
|
80 |
html_video = create_html_video(temp_file, TALKING_HEAD_WIDTH)
|
81 |
html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
|
82 |
else:
|
83 |
html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
|
84 |
else:
|
85 |
if talking_head:
|
86 |
-
temp_file =
|
87 |
html_video = create_html_video(temp_file, TALKING_HEAD_WIDTH)
|
88 |
else:
|
89 |
# html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
|
|
|
4 |
|
5 |
from langchain import ConversationChain, LLMChain
|
6 |
|
7 |
+
from config.config import MAX_TALKING_HEAD_TEXT_LENGTH, LOOPING_TALKING_HEAD_VIDEO_PATH, TALKING_HEAD_WIDTH
|
8 |
from utilities.html_stuff import do_html_video_speak, create_html_video, do_html_audio_speak
|
9 |
from utilities.transform_text import transform_text
|
10 |
|
|
|
76 |
if len(output) <= MAX_TALKING_HEAD_TEXT_LENGTH:
|
77 |
html_video, temp_file = do_html_video_speak(output, translate_to)
|
78 |
else:
|
79 |
+
temp_file = LOOPING_TALKING_HEAD_VIDEO_PATH
|
80 |
html_video = create_html_video(temp_file, TALKING_HEAD_WIDTH)
|
81 |
html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
|
82 |
else:
|
83 |
html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
|
84 |
else:
|
85 |
if talking_head:
|
86 |
+
temp_file = LOOPING_TALKING_HEAD_VIDEO_PATH
|
87 |
html_video = create_html_video(temp_file, TALKING_HEAD_WIDTH)
|
88 |
else:
|
89 |
# html_audio, temp_aud_file = do_html_audio_speak(output, translate_to)
|
utilities/run_chain.py
CHANGED
@@ -6,7 +6,7 @@ from io import StringIO
|
|
6 |
from openai import InvalidRequestError
|
7 |
from openai.error import RateLimitError, AuthenticationError
|
8 |
|
9 |
-
from config.config import BUG_FOUND_MSG,
|
10 |
|
11 |
|
12 |
def run_chain(chain, inp, capture_hidden_text):
|
@@ -21,7 +21,7 @@ def run_chain(chain, inp, capture_hidden_text):
|
|
21 |
try:
|
22 |
output = chain.run(input=inp)
|
23 |
except AuthenticationError as ae:
|
24 |
-
error_msg =
|
25 |
print("error_msg", error_msg)
|
26 |
except RateLimitError as rle:
|
27 |
error_msg = "\n\nRateLimitError: " + str(rle)
|
@@ -59,7 +59,7 @@ def run_chain(chain, inp, capture_hidden_text):
|
|
59 |
try:
|
60 |
output = chain.run(input=inp)
|
61 |
except AuthenticationError as ae:
|
62 |
-
output =
|
63 |
print("output", output)
|
64 |
except RateLimitError as rle:
|
65 |
output = "\n\nRateLimitError: " + str(rle)
|
|
|
6 |
from openai import InvalidRequestError
|
7 |
from openai.error import RateLimitError, AuthenticationError
|
8 |
|
9 |
+
from config.config import BUG_FOUND_MSG, AUTHORIZATION_ERROR_MESSAGE
|
10 |
|
11 |
|
12 |
def run_chain(chain, inp, capture_hidden_text):
|
|
|
21 |
try:
|
22 |
output = chain.run(input=inp)
|
23 |
except AuthenticationError as ae:
|
24 |
+
error_msg = AUTHORIZATION_ERROR_MESSAGE + str(datetime.datetime.now()) + ". " + str(ae)
|
25 |
print("error_msg", error_msg)
|
26 |
except RateLimitError as rle:
|
27 |
error_msg = "\n\nRateLimitError: " + str(rle)
|
|
|
59 |
try:
|
60 |
output = chain.run(input=inp)
|
61 |
except AuthenticationError as ae:
|
62 |
+
output = AUTHORIZATION_ERROR_MESSAGE + str(datetime.datetime.now()) + ". " + str(ae)
|
63 |
print("output", output)
|
64 |
except RateLimitError as rle:
|
65 |
output = "\n\nRateLimitError: " + str(rle)
|
utilities/update_things.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
from langchain import FAISS
|
2 |
from langchain.text_splitter import CharacterTextSplitter
|
3 |
|
4 |
-
|
5 |
from utilities.load_chain import load_chain
|
6 |
|
7 |
|
|
|
1 |
from langchain import FAISS
|
2 |
from langchain.text_splitter import CharacterTextSplitter
|
3 |
|
|
|
4 |
from utilities.load_chain import load_chain
|
5 |
|
6 |
|