AskJesusHF / app.py
TheCenterforUnity
Add token counts.
de66df6
Raw History Blame Contribute Delete
5.04 kB
#
# AskJesus HF v0.34
# Jun 28, 2024 - remove email option from Gradio app until we can fix it.
#
#
# Import the required libraries
import os
# Check if running on Hugging Face
if 'HF_HOME' in os.environ:
# Access Hugging Face environment variables
private_local_deployemnt = os.environ.get("PRIVATE_LOCAL_DEPLOYMENT") == "True"
#
# ChatGPT SYSTEM MESSAGE and Model selection
#
current_system_message = os.environ("CHAT_GPT_DEFAULT_SYSTEM_MESSAGE")
current_screening_prompt = os.environ("CHAT_GPT_SCREENING_PROMPT")
current_gpt_model = os.environ("CHAT_GPT_4O_MODEL")
chatbot_version = f"AskJesus Chatbot {os.environ('ASKJESUS_CHATBOT_VERSION')}"
running_on_hf = True
else:
# support for the .env file
from dotenv import load_dotenv
load_dotenv()
private_local_deployemnt = os.getenv("PRIVATE_LOCAL_DEPLOYMENT") == "True"
#
# ChatGPT SYSTEM MESSAGE and Model selection
#
current_system_message = os.getenv("CHAT_GPT_DEFAULT_SYSTEM_MESSAGE")
current_screening_prompt = os.getenv("CHAT_GPT_SCREENING_PROMPT")
#current_gpt_model = os.getenv("CHAT_GPT_3_5_MODEL")
current_gpt_model = os.getenv("CHAT_GPT_4O_MODEL")
#current_gpt_model = os.getenv("CHAT_GPT_4_0_TURBO_MODEL")
chatbot_version = f"AskJesus Chatbot {os.getenv('ASKJESUS_CHATBOT_VERSION')}"
running_on_hf = False
import gradio as gr
# Import the required functions
from social_sharing import send_email_mailchimp, send_to_airtable
from AskJesus_RAG import run_question
# Use RAG for the answer generation
current_RAG_option = True
# Initialize an empty list to store the chat history
chat_history = []
with gr.Blocks(title=chatbot_version) as demo:
# Gradio UI
with gr.Row():
msg = gr.Textbox(
label="Question:",
placeholder="Enter question here",
scale=4
)
with gr.Row():
chatbot = gr.Chatbot(
label=chatbot_version,
scale=8
)
''' Until the email function is fixed.
with gr.Row():
email_input = gr.Textbox(
label="Email:",
placeholder="Enter your email here",
scale=8
)
send_email_button = gr.Button(
value="Send email",
variant="primary",
scale=1
)
'''
def user(user_message, history):
# Append the user's message to the chat history
chat_history.append({"Question": user_message, "AskJesus chatbot": None})
return gr.update(value="", interactive=False), history + [[user_message, None]]
def bot(history):
# Get a chat completion from the formatted messages and the current system message
global current_system_message
# assuming response is an str
bot_message, input_tokens, output_tokens = run_question(
question=history[-1][0],
system_message=current_system_message,
screening_prompt=current_screening_prompt,
gpt_model=current_gpt_model,
use_RAG=current_RAG_option
)
# Update the chat history with the bot's message
chat_history[-1]["AskJesus chatbot"] = bot_message
history[-1][1] = bot_message
yield history
# Send the question and answer to Airtable
send_to_airtable(
history[-1][0],
history[-1][1],
current_system_message,
current_gpt_model,
current_RAG_option,
input_tokens,
output_tokens
)
''' Until the email issue is fixed
def email_session(email):
if not email:
print("No email address provided. Cannot send email.")
raise gr.Error("Please provide email.")
return
# Convert the chat history to a string format
history_str = "\n".join([f"Question: {entry['Question']}\nAskJesus chatbot: {entry['AskJesus chatbot']}\n\n" for entry in chat_history])
send_email_mailchimp(email, history_str)
def email_last_qa(email):
if not email:
print("No email address provided. Cannot send email.")
raise gr.Error("Please provide email.")
return
# Convert the last question and answer to a string format
history_str = "\n".join([f"Question: {chat_history[-1]['Question']}\nAskJesus chatbot: {chat_history[-1]['AskJesus chatbot']}\n\n"])
if send_email_mailchimp(email, history_str) == True :
gr.Info("Email sent.")
else :
raise gr.Error("Error: email not sent.")
'''
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
''' until the email issue is fixed
send_email_button.click(email_last_qa, [email_input])
'''
demo.queue()
demo.launch(show_error=True)