Spaces:
Sleeping
Sleeping
File size: 5,912 Bytes
2fedcf6 6eaf265 2fedcf6 6eaf265 2fedcf6 1567f56 2fedcf6 |
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 |
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()
|