Spaces:
Runtime error
Runtime error
Update app.py
#1
by CenterFor - opened
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
#
|
| 2 |
-
# Hey Michael v0.
|
| 3 |
-
#
|
| 4 |
#
|
| 5 |
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import time
|
| 8 |
from langchain.chat_models import ChatOpenAI
|
|
@@ -12,14 +13,36 @@ from langchain.prompts.chat import (
|
|
| 12 |
HumanMessagePromptTemplate,
|
| 13 |
)
|
| 14 |
from langchain.schema import HumanMessage, SystemMessage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Initialize the chat model
|
| 17 |
-
chat = ChatOpenAI(model="gpt-4-0613", temperature=0.0)
|
| 18 |
#chat = ChatOpenAI(model="gpt-4", temperature=0.0)
|
| 19 |
-
|
| 20 |
|
| 21 |
# Define the system message
|
| 22 |
-
system_message_content = "You are Michael of Nebadon, also known as Jesus of Nazareth. Your answers are exclusively based on the Urantia Book. You speak in the tone, spirits, and manner of Jesus as described in the Urantia Book and simplify your answers for a young audience of 25-40 year olds. In your answer, include specific references to the Papers, Sections, and Paragraphs of the Urantia Book. Write all the references at the end of the answer under a section called References and in the following shorthand format. The format is Paper number followed by a colon followed by the Section number followed by a period followed by the Paragraph number. For example, Paper 1, Section 2, Paragraph 3 is written as 1:2.3"
|
|
|
|
| 23 |
|
| 24 |
# Create templates
|
| 25 |
system_template = system_message_content
|
|
@@ -32,14 +55,99 @@ chat_prompt = ChatPromptTemplate.from_messages(
|
|
| 32 |
[system_message_prompt, human_message_prompt]
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 36 |
chatbot = gr.Chatbot()
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
clear = gr.ClearButton([msg, chatbot])
|
| 41 |
|
| 42 |
def user(user_message, history):
|
|
|
|
|
|
|
| 43 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
| 44 |
|
| 45 |
def bot(history):
|
|
@@ -48,16 +156,33 @@ with gr.Blocks() as demo:
|
|
| 48 |
response = chat(messages)
|
| 49 |
bot_message = response.content # assuming response is an AIMessage
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
history[-1][1] = ""
|
| 52 |
for character in bot_message:
|
| 53 |
history[-1][1] += character
|
| 54 |
time.sleep(0.05)
|
| 55 |
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 58 |
bot, chatbot, chatbot
|
| 59 |
)
|
| 60 |
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
|
|
|
|
|
|
|
| 61 |
|
| 62 |
demo.queue()
|
| 63 |
demo.launch()
|
|
|
|
| 1 |
#
|
| 2 |
+
# Hey Michael v0.21
|
| 3 |
+
# Aug 31, 2023
|
| 4 |
#
|
| 5 |
|
| 6 |
+
import tweepy
|
| 7 |
import gradio as gr
|
| 8 |
import time
|
| 9 |
from langchain.chat_models import ChatOpenAI
|
|
|
|
| 13 |
HumanMessagePromptTemplate,
|
| 14 |
)
|
| 15 |
from langchain.schema import HumanMessage, SystemMessage
|
| 16 |
+
import requests # To make API calls to Airtable
|
| 17 |
+
import smtplib
|
| 18 |
+
from email.mime.text import MIMEText
|
| 19 |
+
from email.mime.multipart import MIMEMultipart
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Twitter API credentials
|
| 23 |
+
|
| 24 |
+
#OAUTH 2
|
| 25 |
+
consumer_key = "rAWMBjvs7TGrhjHs1orrXSA1c"
|
| 26 |
+
consumer_secret = "IGZFVc9NsM7sSjU7gWkE1LTejQ5tlternCoOk9NTuZjYkrFm7V"
|
| 27 |
+
access_token = "1697549536330121216-TXUC7Ol6ngSU6SSrJAleDqhPr6WQjy"
|
| 28 |
+
access_token_secret = "FmrPfO9ZgM7Qxxx7tHECXQjuwCXhlnGfsC329CoALdQzC"
|
| 29 |
+
bearer_token = "AAAAAAAAAAAAAAAAAAAAAFXepgEAAAAAWiZmxbxBFWY11Bjv0qy2ksG1mCU%3D2g91CFJv5OuSR1AGmnHOgOdGqvzlkBVGXdR0j8gOjfodzvNVMy"
|
| 30 |
+
|
| 31 |
+
client = tweepy.Client(
|
| 32 |
+
bearer_token=bearer_token,
|
| 33 |
+
access_token=access_token,
|
| 34 |
+
access_token_secret=access_token_secret,
|
| 35 |
+
consumer_key=consumer_key,
|
| 36 |
+
consumer_secret=consumer_secret)
|
| 37 |
|
| 38 |
# Initialize the chat model
|
| 39 |
+
#chat = ChatOpenAI(model="gpt-4-0613", temperature=0.0)
|
| 40 |
#chat = ChatOpenAI(model="gpt-4", temperature=0.0)
|
| 41 |
+
chat = ChatOpenAI(model="gpt-3.5-turbo-16k", temperature=0.25)
|
| 42 |
|
| 43 |
# Define the system message
|
| 44 |
+
#system_message_content = "You are Michael of Nebadon, also known as Jesus of Nazareth. Your answers are exclusively based on the Urantia Book. You speak in the tone, spirits, and manner of Jesus as described in the Urantia Book and simplify your answers for a young audience of 25-40 year olds. In your answer, include specific references to the Papers, Sections, and Paragraphs of the Urantia Book. Write all the references at the end of the answer under a section called References and in the following shorthand format. The format is Paper number followed by a colon followed by the Section number followed by a period followed by the Paragraph number. For example, Paper 1, Section 2, Paragraph 3 is written as 1:2.3"
|
| 45 |
+
system_message_content = "You are Michael of Nebadon, also known as Jesus of Nazareth. Your answers are exclusively based on the Urantia Book. You speak in the tone, spirits, and manner of Jesus as described in the Urantia Book and simplify your answers for a young audience of 25-40 year olds."
|
| 46 |
|
| 47 |
# Create templates
|
| 48 |
system_template = system_message_content
|
|
|
|
| 55 |
[system_message_prompt, human_message_prompt]
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Initialize an empty list to store the chat history
|
| 59 |
+
chat_history = []
|
| 60 |
+
|
| 61 |
+
def send_to_airtable(question, answer):
|
| 62 |
+
# Replace with your Airtable API URL and API Key
|
| 63 |
+
url = "https://api.airtable.com/v0/appcQmrdF76EbsPB3/sessions"
|
| 64 |
+
headers = {"Authorization": "Bearer keyyKWDuDslzJCO9v"}
|
| 65 |
+
data = {
|
| 66 |
+
"fields": {"question": question, "answer": answer}
|
| 67 |
+
}
|
| 68 |
+
requests.post(url, headers=headers, json=data)
|
| 69 |
+
|
| 70 |
+
def send_email(email, history_str):
|
| 71 |
+
if not email:
|
| 72 |
+
print("No email address provided. Cannot send email.")
|
| 73 |
+
return
|
| 74 |
+
|
| 75 |
+
# Email server setup
|
| 76 |
+
smtp_server = "smtp.gmail.com"
|
| 77 |
+
smtp_port = 587 # For TLS
|
| 78 |
+
smtp_username = "contact@thecenterforunity.org"
|
| 79 |
+
smtp_password = "xwsdewhkfqszeenx"
|
| 80 |
+
|
| 81 |
+
# Email content
|
| 82 |
+
subject = "👉 A chatbot session from AskJesus"
|
| 83 |
+
#body = "Someone that cares about you share their AskJesus chatbot session with you:\n\n" + history_str + "\n\n Click here to try the AskJesus chatbot."
|
| 84 |
+
|
| 85 |
+
html_start = "<html><body><p>Someone that cares about you shared their AskJesus chatbot session with you:</p><p>"
|
| 86 |
+
html_end = '</p><p>Click <a href="https://www.thecenterforunity.org/ask-jesus-chatbot">here</a> to try the AskJesus chatbot.</p></body></html>'
|
| 87 |
+
|
| 88 |
+
# Replace newline characters with HTML line breaks
|
| 89 |
+
history_html = history_str.replace("\n", "<br/>")
|
| 90 |
+
|
| 91 |
+
# Combine everything
|
| 92 |
+
body = html_start + history_html + html_end
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Create email
|
| 96 |
+
msg = MIMEMultipart()
|
| 97 |
+
msg["From"] = smtp_username
|
| 98 |
+
msg["To"] = email
|
| 99 |
+
msg["Subject"] = subject
|
| 100 |
+
msg.attach(MIMEText(body, "html"))
|
| 101 |
+
|
| 102 |
+
# Send email
|
| 103 |
+
try:
|
| 104 |
+
server = smtplib.SMTP(smtp_server, smtp_port)
|
| 105 |
+
server.starttls()
|
| 106 |
+
server.login(smtp_username, smtp_password)
|
| 107 |
+
server.sendmail(smtp_username, email, msg.as_string())
|
| 108 |
+
server.quit()
|
| 109 |
+
print("Email sent successfully.")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"Failed to send email. Error: {e}")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def tweet_last_qa():
|
| 115 |
+
if len(chat_history) < 1:
|
| 116 |
+
print ("No Q&A to tweet.")
|
| 117 |
+
return
|
| 118 |
+
|
| 119 |
+
last_qa = chat_history[-1]
|
| 120 |
+
tweet_content = f"Question: {last_qa['Question']}\nAskJesus chatbot: {last_qa['AskJesus chatbot']}"
|
| 121 |
+
tweet_link = f"Click <a href=https://www.thecenterforunity.org/ask-jesus-chatbot>{last_qa['Question']}</a> to try the AskJesus chatbot."
|
| 122 |
+
tweet_link = f"Someone just asked the AskJesus chatbot {last_qa['Question']}.\nWhat would you ask?\n#AskJesus #TheCenterforUnity #JesusBeforeChristianity (https://www.thecenterforunity.org/ask-jesus-chatbot)"
|
| 123 |
+
|
| 124 |
+
#print (tweet_link)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
try:
|
| 128 |
+
client.create_tweet(text=tweet_link)
|
| 129 |
+
print (f"Successfully tweeted the last Q&A. {tweet_link}")
|
| 130 |
+
return
|
| 131 |
+
except Exception as e:
|
| 132 |
+
print (f"Failed to tweet. Error: {e}")
|
| 133 |
+
return
|
| 134 |
+
|
| 135 |
+
|
| 136 |
with gr.Blocks() as demo:
|
| 137 |
+
|
| 138 |
+
# Gradio UI
|
| 139 |
chatbot = gr.Chatbot()
|
| 140 |
+
msg = gr.Textbox(label="Question")
|
| 141 |
+
email_input = gr.Textbox(label="Email Address")
|
| 142 |
+
send_email_button = gr.Button(value="Email this session")
|
| 143 |
+
tweet_button = gr.Button(value="Tweet the last Q&A")
|
| 144 |
+
|
| 145 |
|
| 146 |
clear = gr.ClearButton([msg, chatbot])
|
| 147 |
|
| 148 |
def user(user_message, history):
|
| 149 |
+
# Append the user's message to the chat history
|
| 150 |
+
chat_history.append({"Question": user_message, "AskJesus chatbot": None})
|
| 151 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
| 152 |
|
| 153 |
def bot(history):
|
|
|
|
| 156 |
response = chat(messages)
|
| 157 |
bot_message = response.content # assuming response is an AIMessage
|
| 158 |
|
| 159 |
+
# Update the chat history with the bot's message
|
| 160 |
+
chat_history[-1]["AskJesus chatbot"] = bot_message
|
| 161 |
+
|
| 162 |
history[-1][1] = ""
|
| 163 |
for character in bot_message:
|
| 164 |
history[-1][1] += character
|
| 165 |
time.sleep(0.05)
|
| 166 |
yield history
|
| 167 |
+
|
| 168 |
+
# Send the question and answer to Airtable
|
| 169 |
+
send_to_airtable(history[-1][0], history[-1][1])
|
| 170 |
+
|
| 171 |
+
def send_session(email):
|
| 172 |
+
if not email:
|
| 173 |
+
print("No email address provided. Cannot send email.")
|
| 174 |
+
return
|
| 175 |
+
|
| 176 |
+
# Convert the chat history to a string format
|
| 177 |
+
history_str = "\n".join([f"Question: {entry['Question']}\nAskJesus chatbot: {entry['AskJesus chatbot']}" for entry in chat_history])
|
| 178 |
+
send_email(email, history_str)
|
| 179 |
|
| 180 |
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 181 |
bot, chatbot, chatbot
|
| 182 |
)
|
| 183 |
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
|
| 184 |
+
send_email_button.click(send_session, [email_input])
|
| 185 |
+
tweet_button.click(tweet_last_qa)
|
| 186 |
|
| 187 |
demo.queue()
|
| 188 |
demo.launch()
|