Spaces:
Sleeping
Sleeping
File size: 20,989 Bytes
66b79c2 dc2726e 66b79c2 d20eb01 dc2726e d20eb01 66b79c2 dc2726e 66b79c2 dc2726e 66b79c2 dc2726e 66b79c2 d20eb01 66b79c2 dc2726e d20eb01 66b79c2 dc2726e 66b79c2 d20eb01 66b79c2 |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
import os
import ssl
import gradio as gr
from dotenv import load_dotenv
from config.config import TALKING_HEAD_WIDTH, TOOLS_DEFAULT_LIST, NUM_WORDS_DEFAULT, FORMALITY_DEFAULT, EMOTION_DEFAULT, \
LANG_LEVEL_DEFAULT, TRANSLATE_TO_DEFAULT, LITERARY_STYLE_DEFAULT, LOOPING_TALKING_HEAD_VIDEO_PATH, TOOLS_LIST, \
MAX_WORDS
from utilities.audio import FORCE_TRANSLATE_DEFAULT, USE_GPT4_DEFAULT, transcribe, WHISPER_DETECT_LANG
from utilities.html_stuff import create_html_video, update_talking_head
from utilities.reset_memory import ChatWrapper, reset_memory
from utilities.set_openai_api_key import set_openai_api_key
from utilities.update_things import update_foo, update_selected_tools, update_use_embeddings, \
update_embeddings
# Temporarily address Wolfram Alpha SSL certificate issue
ssl._create_default_https_context = ssl._create_unverified_context
chat = ChatWrapper()
with gr.Blocks(css=".gradio-container {background-color: lightgray}") as block:
llm_state = gr.State()
history_state = gr.State()
chain_state = gr.State()
express_chain_state = gr.State()
tools_list_state = gr.State(TOOLS_DEFAULT_LIST)
trace_chain_state = gr.State(False)
speak_text_state = gr.State(False)
talking_head_state = gr.State(True)
monologue_state = gr.State(False) # Takes the input and repeats it back to themp4 user, optionally transforming it.
force_translate_state = gr.State(FORCE_TRANSLATE_DEFAULT) #
memory_state = gr.State()
# Pertains to Express-inator functionality
num_words_state = gr.State(NUM_WORDS_DEFAULT)
formality_state = gr.State(FORMALITY_DEFAULT)
anticipation_level_state = gr.State(EMOTION_DEFAULT)
joy_level_state = gr.State(EMOTION_DEFAULT)
trust_level_state = gr.State(EMOTION_DEFAULT)
fear_level_state = gr.State(EMOTION_DEFAULT)
surprise_level_state = gr.State(EMOTION_DEFAULT)
sadness_level_state = gr.State(EMOTION_DEFAULT)
disgust_level_state = gr.State(EMOTION_DEFAULT)
anger_level_state = gr.State(EMOTION_DEFAULT)
lang_level_state = gr.State(LANG_LEVEL_DEFAULT)
translate_to_state = gr.State(TRANSLATE_TO_DEFAULT)
literary_style_state = gr.State(LITERARY_STYLE_DEFAULT)
# Pertains to WHISPER functionality
whisper_lang_state = gr.State(WHISPER_DETECT_LANG)
# Pertains to question answering functionality
embeddings_state = gr.State()
qa_chain_state = gr.State()
docsearch_state = gr.State()
use_embeddings_state = gr.State(False)
use_gpt4_state = gr.State(USE_GPT4_DEFAULT)
with gr.Tab("Chat"):
with gr.Row():
with gr.Column():
gr.HTML(
"""<b><center>GPT + WolframAlpha + Whisper</center></b>
<p><center>Hit Enter after pasting your OpenAI API key.</center></p>
<i><center>If you have GPT-4 access, optionally select it in Settings tab.</center></i>""")
load_dotenv()
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...) and hit Enter",
value=os.environ.get("OPENAI_API_KEY", ""),
show_label=False,
lines=1, type='password')
with gr.Row():
with gr.Column(scale=1, min_width=TALKING_HEAD_WIDTH, visible=True):
speak_text_cb = gr.Checkbox(label="Enable speech", value=False)
speak_text_cb.change(update_foo, inputs=[speak_text_cb, speak_text_state],
outputs=[speak_text_state])
my_file = gr.File(label="Upload a file", type="file", visible=False)
# tmp_file_url = "/file=" + tmp_file.value['name']
html_video = create_html_video(LOOPING_TALKING_HEAD_VIDEO_PATH, TALKING_HEAD_WIDTH)
video_html = gr.HTML(html_video)
# my_aud_file = gr.File(label="Audio file", type="file", visible=True)
tmp_aud_file = gr.File("assets/audios/tempfile.mp3", visible=False)
tmp_aud_file_url = "/file=" + tmp_aud_file.value['name']
htm_audio = f'<audio><source src={tmp_aud_file_url} type="audio/mp3"></audio>'
audio_html = gr.HTML(htm_audio)
with gr.Column(scale=7):
chatbot = gr.Chatbot()
with gr.Row():
message = gr.Textbox(label="What's on your mind??",
placeholder="What's the answer to life, the universe, and everything?",
lines=1)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
# UNCOMMENT TO USE WHISPER
with gr.Row():
audio_comp = gr.Microphone(source="microphone", type="filepath", label="Just say it!",
interactive=True, streaming=False)
audio_comp.change(transcribe, inputs=[audio_comp, whisper_lang_state], outputs=[message])
# TEMPORARY FOR TESTING
# with gr.Row():
# audio_comp_tb = gr.Textbox(label="Just say it!", lines=1)
# audio_comp_tb.submit(transcribe_dummy, inputs=[audio_comp_tb, whisper_lang_state], outputs=[message])
gr.Examples(
examples=["How many people live in Canada?",
"What is 2 to the 30th power?",
"If x+y=10 and x-y=4, what are x and y?",
"How much did it rain in SF today?",
"Get me information about the movie 'Avatar'",
"What are the top tech headlines in the US?",
"On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses - "
"if I remove all the pairs of sunglasses from the desk, how many purple items remain on it?"],
inputs=message
)
with gr.Tab("Settings"):
tools_checkbox_group = gr.CheckboxGroup(label="Tools:", choices=TOOLS_LIST,
value=TOOLS_DEFAULT_LIST)
tools_checkbox_group.change(update_selected_tools,
inputs=[tools_checkbox_group, tools_list_state, llm_state],
outputs=[tools_list_state, llm_state, chain_state, express_chain_state])
trace_chain_checkbox = gr.Checkbox(label="Show reasoning chain in chat bubble", value=False)
trace_chain_checkbox.change(update_foo, inputs=[trace_chain_checkbox, trace_chain_state],
outputs=[trace_chain_state])
force_translate_checkbox = gr.Checkbox(label="Force translation to selected Output Language",
value=FORCE_TRANSLATE_DEFAULT)
force_translate_checkbox.change(update_foo, inputs=[force_translate_checkbox, force_translate_state],
outputs=[force_translate_state])
# speak_text_cb = gr.Checkbox(label="Speak text from agent", value=False)
# speak_text_cb.change(update_foo, inputs=[speak_text_cb, speak_text_state],
# outputs=[speak_text_state])
talking_head_cb = gr.Checkbox(label="Show talking head", value=True)
talking_head_cb.change(update_talking_head, inputs=[talking_head_cb, talking_head_state],
outputs=[talking_head_state, video_html])
monologue_cb = gr.Checkbox(label="Babel fish mode (translate/restate what you enter, no conversational agent)",
value=False)
monologue_cb.change(update_foo, inputs=[monologue_cb, monologue_state],
outputs=[monologue_state])
use_gpt4_cb = gr.Checkbox(label="Use GPT-4 (experimental) if your OpenAI API has access to it",
value=USE_GPT4_DEFAULT)
use_gpt4_cb.change(set_openai_api_key,
inputs=[openai_api_key_textbox, use_gpt4_cb],
outputs=[chain_state, express_chain_state, llm_state, embeddings_state,
qa_chain_state, memory_state, use_gpt4_state])
reset_btn = gr.Button(value="Reset chat", variant="secondary").style(full_width=False)
reset_btn.click(reset_memory, inputs=[history_state, memory_state],
outputs=[chatbot, history_state, memory_state])
with gr.Tab("Whisper STT"):
whisper_lang_radio = gr.Radio(label="Whisper speech-to-text language:", choices=[
WHISPER_DETECT_LANG, "Arabic", "Arabic (Gulf)", "Catalan", "Chinese (Cantonese)", "Chinese (Mandarin)",
"Danish", "Dutch", "English (Australian)", "English (British)", "English (Indian)", "English (New Zealand)",
"English (South African)", "English (US)", "English (Welsh)", "Finnish", "French", "French (Canadian)",
"German", "German (Austrian)", "Georgian", "Hindi", "Icelandic", "Indonesian", "Italian", "Japanese",
"Korean", "Norwegian", "Polish",
"Portuguese (Brazilian)", "Portuguese (European)", "Romanian", "Russian", "Spanish (European)",
"Spanish (Mexican)", "Spanish (US)", "Swedish", "Turkish", "Ukrainian", "Welsh"],
value=WHISPER_DETECT_LANG)
whisper_lang_radio.change(update_foo,
inputs=[whisper_lang_radio, whisper_lang_state],
outputs=[whisper_lang_state])
with gr.Tab("Output Language"):
lang_level_radio = gr.Radio(label="Language level:", choices=[
LANG_LEVEL_DEFAULT, "1st grade", "2nd grade", "3rd grade", "4th grade", "5th grade", "6th grade",
"7th grade", "8th grade", "9th grade", "10th grade", "11th grade", "12th grade", "University"],
value=LANG_LEVEL_DEFAULT)
lang_level_radio.change(update_foo, inputs=[lang_level_radio, lang_level_state],
outputs=[lang_level_state])
translate_to_radio = gr.Radio(label="Language:", choices=[
TRANSLATE_TO_DEFAULT, "Arabic", "Arabic (Gulf)", "Catalan", "Chinese (Cantonese)", "Chinese (Mandarin)",
"Danish", "Dutch", "English (Australian)", "English (British)", "English (Indian)", "English (New Zealand)",
"English (South African)", "English (US)", "English (Welsh)", "Finnish", "French", "French (Canadian)",
"German", "German (Austrian)", "Georgian", "Hindi", "Icelandic", "Indonesian", "Italian", "Japanese",
"Korean", "Norwegian", "Polish",
"Portuguese (Brazilian)", "Portuguese (European)", "Romanian", "Russian", "Spanish (European)",
"Spanish (Mexican)", "Spanish (US)", "Swedish", "Turkish", "Ukrainian", "Welsh",
"emojis", "Gen Z slang", "how the stereotypical Karen would say it", "Klingon", "Neanderthal",
"Pirate", "Strange Planet expospeak technical talk", "Yoda"],
value=TRANSLATE_TO_DEFAULT)
translate_to_radio.change(update_foo,
inputs=[translate_to_radio, translate_to_state],
outputs=[translate_to_state])
with gr.Tab("Formality"):
formality_radio = gr.Radio(label="Formality:",
choices=[FORMALITY_DEFAULT, "Casual", "Polite", "Honorific"],
value=FORMALITY_DEFAULT)
formality_radio.change(update_foo,
inputs=[formality_radio, formality_state],
outputs=[formality_state])
with gr.Tab("Lit Style"):
literary_style_radio = gr.Radio(label="Literary style:", choices=[
LITERARY_STYLE_DEFAULT, "Prose", "Story", "Summary", "Outline", "Bullets", "Poetry", "Haiku", "Limerick",
"Rap",
"Joke", "Knock-knock", "FAQ"],
value=LITERARY_STYLE_DEFAULT)
literary_style_radio.change(update_foo,
inputs=[literary_style_radio, literary_style_state],
outputs=[literary_style_state])
with gr.Tab("Emotions"):
anticipation_level_radio = gr.Radio(label="Anticipation level:",
choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
value=EMOTION_DEFAULT)
anticipation_level_radio.change(update_foo,
inputs=[anticipation_level_radio, anticipation_level_state],
outputs=[anticipation_level_state])
joy_level_radio = gr.Radio(label="Joy level:",
choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
value=EMOTION_DEFAULT)
joy_level_radio.change(update_foo,
inputs=[joy_level_radio, joy_level_state],
outputs=[joy_level_state])
trust_level_radio = gr.Radio(label="Trust level:",
choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
value=EMOTION_DEFAULT)
trust_level_radio.change(update_foo,
inputs=[trust_level_radio, trust_level_state],
outputs=[trust_level_state])
fear_level_radio = gr.Radio(label="Fear level:",
choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
value=EMOTION_DEFAULT)
fear_level_radio.change(update_foo,
inputs=[fear_level_radio, fear_level_state],
outputs=[fear_level_state])
surprise_level_radio = gr.Radio(label="Surprise level:",
choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
value=EMOTION_DEFAULT)
surprise_level_radio.change(update_foo,
inputs=[surprise_level_radio, surprise_level_state],
outputs=[surprise_level_state])
sadness_level_radio = gr.Radio(label="Sadness level:",
choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
value=EMOTION_DEFAULT)
sadness_level_radio.change(update_foo,
inputs=[sadness_level_radio, sadness_level_state],
outputs=[sadness_level_state])
disgust_level_radio = gr.Radio(label="Disgust level:",
choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
value=EMOTION_DEFAULT)
disgust_level_radio.change(update_foo,
inputs=[disgust_level_radio, disgust_level_state],
outputs=[disgust_level_state])
anger_level_radio = gr.Radio(label="Anger level:",
choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
value=EMOTION_DEFAULT)
anger_level_radio.change(update_foo,
inputs=[anger_level_radio, anger_level_state],
outputs=[anger_level_state])
with gr.Tab("Max Words"):
num_words_slider = gr.Slider(label="Max number of words to generate (0 for don't care)",
value=NUM_WORDS_DEFAULT, minimum=0, maximum=MAX_WORDS, step=10)
num_words_slider.change(update_foo,
inputs=[num_words_slider, num_words_state],
outputs=[num_words_state])
with gr.Tab("Embeddings"):
embeddings_text_box = gr.Textbox(label="Enter text for embeddings and hit Create:",
lines=20)
with gr.Row():
use_embeddings_cb = gr.Checkbox(label="Use embeddings", value=False)
use_embeddings_cb.change(update_use_embeddings, inputs=[use_embeddings_cb, use_embeddings_state],
outputs=[use_embeddings_state])
embeddings_text_submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
embeddings_text_submit.click(update_embeddings,
inputs=[embeddings_text_box, embeddings_state, qa_chain_state],
outputs=[docsearch_state])
gr.HTML("""
<p>This application, developed by <a href='https://www.linkedin.com/in/javafxpert/'>James L. Weaver</a>,
demonstrates a conversational agent implemented with OpenAI GPT-3.5 and LangChain.
When necessary, it leverages tools for complex math, searching the internet, and accessing news and weather.
Uses talking heads from <a href='https://exh.ai/'>Ex-Human</a>.
For faster inference without waiting in queue, you may duplicate the space.
</p>""")
gr.HTML("""
<form action="https://www.paypal.com/donate" method="post" target="_blank">
<input type="hidden" name="business" value="AK8BVNALBXSPQ" />
<input type="hidden" name="no_recurring" value="0" />
<input type="hidden" name="item_name" value="Please consider helping to defray the cost of APIs such as SerpAPI and WolframAlpha that this app uses." />
<input type="hidden" name="currency_code" value="USD" />
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" title="PayPal - The safer, easier way to pay online!" alt="Donate with PayPal button" />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
</form>
""")
gr.HTML("""<center>
<a href="https://huggingface.co/spaces/JavaFXpert/Chat-GPT-LangChain?duplicate=true">
<img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
Powered by <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ</a>
</center>""")
message.submit(chat, inputs=[openai_api_key_textbox, message, history_state, chain_state, trace_chain_state,
speak_text_state, talking_head_state, monologue_state,
express_chain_state, num_words_state, formality_state,
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
lang_level_state, translate_to_state, literary_style_state,
qa_chain_state, docsearch_state, use_embeddings_state,
force_translate_state],
outputs=[chatbot, history_state, video_html, my_file, audio_html, tmp_aud_file, message])
submit.click(chat, inputs=[openai_api_key_textbox, message, history_state, chain_state, trace_chain_state,
speak_text_state, talking_head_state, monologue_state,
express_chain_state, num_words_state, formality_state,
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
lang_level_state, translate_to_state, literary_style_state,
qa_chain_state, docsearch_state, use_embeddings_state,
force_translate_state],
outputs=[chatbot, history_state, video_html, my_file, audio_html, tmp_aud_file, message])
openai_api_key_textbox.change(set_openai_api_key,
inputs=[openai_api_key_textbox, use_gpt4_state],
outputs=[chain_state, express_chain_state, llm_state, embeddings_state,
qa_chain_state, memory_state, use_gpt4_state])
openai_api_key_textbox.submit(set_openai_api_key,
inputs=[openai_api_key_textbox, use_gpt4_state],
outputs=[chain_state, express_chain_state, llm_state, embeddings_state,
qa_chain_state, memory_state, use_gpt4_state])
block.launch(debug=True)
|