Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
import os
|
| 4 |
+
from langchain.chat_models import ChatOpenAI
|
| 5 |
+
from langchain.prompts.chat import (
|
| 6 |
+
ChatPromptTemplate,
|
| 7 |
+
SystemMessagePromptTemplate,
|
| 8 |
+
HumanMessagePromptTemplate,
|
| 9 |
+
)
|
| 10 |
+
from langchain.schema import HumanMessage, SystemMessage
|
| 11 |
+
|
| 12 |
+
# Initialize the chat model
|
| 13 |
+
chat = ChatOpenAI(model="gpt-3.5-turbo-16k")
|
| 14 |
+
|
| 15 |
+
# Define the system message
|
| 16 |
+
system_message_content = "You are Michael of Nebadon, also known as Jesus Christ strictly as described in the Urantia Book. You speak in the tone, spirits, and manner of the Christ as described in the Urantia Book."
|
| 17 |
+
|
| 18 |
+
# Create templates
|
| 19 |
+
system_template = system_message_content
|
| 20 |
+
system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)
|
| 21 |
+
human_template = "{text}"
|
| 22 |
+
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
| 23 |
+
|
| 24 |
+
# Build a chat prompt template
|
| 25 |
+
chat_prompt = ChatPromptTemplate.from_messages(
|
| 26 |
+
[system_message_prompt, human_message_prompt]
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Initialize HuggingFaceDatasetSaver
|
| 30 |
+
HF_TOKEN = os.getenv('HF_TOKEN') # replace 'HF_TOKEN' with the name of your environment variable
|
| 31 |
+
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "CenterFor/HeyJesus_Feedback")
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
chatbot = gr.Chatbot()
|
| 35 |
+
msg = gr.Textbox()
|
| 36 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 37 |
+
|
| 38 |
+
def user(user_message, history):
|
| 39 |
+
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
| 40 |
+
|
| 41 |
+
def bot(history):
|
| 42 |
+
# Get a chat completion from the formatted messages
|
| 43 |
+
messages = chat_prompt.format_prompt(text=history[-1][0]).to_messages()
|
| 44 |
+
response = chat(messages)
|
| 45 |
+
bot_message = response.content # assuming response is an AIMessage
|
| 46 |
+
|
| 47 |
+
history[-1][1] = ""
|
| 48 |
+
for character in bot_message:
|
| 49 |
+
history[-1][1] += character
|
| 50 |
+
time.sleep(0.05)
|
| 51 |
+
yield history
|
| 52 |
+
|
| 53 |
+
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 54 |
+
bot, chatbot, chatbot
|
| 55 |
+
)
|
| 56 |
+
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
|
| 57 |
+
|
| 58 |
+
demo.allow_flagging = "manual" # allow manual flagging
|
| 59 |
+
demo.flagging_callback = hf_writer # set the flagging callback to hf_writer
|
| 60 |
+
|
| 61 |
+
demo.queue()
|
| 62 |
+
demo.launch()
|