import os import time from uuid import uuid4 import gradio as gr from langchain.chat_models import ChatOpenAI from langchain.schema import AIMessage, HumanMessage from utils import prompts from utils import functional as F # Define all functions def start_chatbot(input_username, input_password, input_questions, id_interview): if input_username == "" or input_password == "" or input_questions == "": return ( (gr.update(),) * 2 + (gr.update(value="Invalid username. Please try again."),) + (gr.update(value="Invalid password. Please try again."),) + (gr.update(value="Invalid questions. Please try again."),) + (gr.update(),) * 5 ) print(os.environ) if ( f"AIRIS_DEMO_ACCOUNT_{input_username}" not in os.environ or os.environ[f"AIRIS_DEMO_ACCOUNT_{input_username}"] != input_password ): return ( (gr.update(),) * 2 + (gr.update(value="Invalid username. Please try again."),) + (gr.update(value="Invalid password. Please try again."),) + (gr.update(),) * 6 ) chat_openai = ChatOpenAI( model_name="gpt-3.5-turbo", temperature=0.3, ) return ( input_questions, chat_openai, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update( visible=True, value=[ [None, F.get_first_message(chat_openai, input_questions, id_interview)], ], ), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), ) def bot(chat_openai, questions, id_interview, history): history_messages = F.get_initial_messages(questions, id_interview) for message in history: if message[0] is not None: history_messages.append( HumanMessage(content=F.remove_html_tags(message[0]).strip()) ) if message[1] is not None: history_messages.append( AIMessage(content=F.remove_html_tags(message[1]).strip()) ) if prompts.END_CHATBOT_PROMPTS in F.remove_html_tags( history[-2][1] ) or prompts.END_QUESTION_PROMPTS in F.remove_html_tags(history[-2][1]): bot_message = prompts.END_CHATBOT_PROMPTS elif not history[-1][0]: bot_message = prompts.EMPTY_INPUT_MESSAGE else: bot_message = F.get_bot_message(chat_openai, history_messages) if not bot_message == prompts.END_CHATBOT_PROMPTS: check_if_end_of_message = id_interview in bot_message if check_if_end_of_message: bot_message = bot_message.replace(f'ID Interview: "{id_interview}"', "") bot_message = ( "\n".join(bot_message.split("\n")[:-1]) + "\n" + prompts.END_QUESTION_PROMPTS ) history[-1][1] = "" for character in bot_message: history[-1][1] += character time.sleep(0.005) yield history with gr.Blocks(title="AIRIS (AI Regenerative Interview Survey)") as demo: # Define all states questions = gr.State(value=None) chat_openai = gr.State(value=None) id_interview = gr.State(value=str(uuid4().hex)) # Define all components gr.Markdown( """ # AIRIS (AI Regenerative Interview Survey) AIRIS (AI Regenerative Interview Survey) is an advanced application that utilizes artificial intelligence to streamline interviews and surveys. It generates intelligent questions, analyzes real-time responses, and continuously improves based on past interviews. With data analysis tools, it provides valuable insights, making interviews more efficient and insightful. """ # noqa: E501 ) input_username = gr.Textbox( label="Username", placeholder="Input your Username here...", ).style(container=True) input_password = gr.Textbox( label="Password", placeholder="Input your Password here...", type="password", ).style(container=True) input_questions = gr.TextArea( label="Questions", placeholder="Input your questions here.." ).style(container=True) input_submit = gr.Button("Submit") chatbot_display = gr.Chatbot(label="History Messages", visible=False).style( height=600 ) message_input = gr.TextArea( label="Your Message", placeholder="Type your message here...", lines=2, visible=False, ).style(container=True) send_message = gr.Button("Send", visible=False) reset_message = gr.Button("Reset Chat", visible=False).style( full_width=False, size="sm" ) # Define all 'components' interactions send_message.click( fn=lambda user_message, history: ("", history + [[user_message, None]]), inputs=[message_input, chatbot_display], outputs=[message_input, chatbot_display], ).then( fn=bot, inputs=[chat_openai, questions, id_interview, chatbot_display], outputs=chatbot_display, ) reset_message.click( fn=lambda chat_openai, questions, id_interview: gr.update( value=[ [None, F.get_first_message(chat_openai, questions, id_interview)], ] ), inputs=[chat_openai, questions], outputs=chatbot_display, ) input_submit.click( fn=start_chatbot, inputs=[input_username, input_password, input_questions, id_interview], outputs=[ questions, chat_openai, input_username, input_password, input_questions, input_submit, chatbot_display, message_input, send_message, reset_message, ], ) demo.queue().launch()