Spaces:
Sleeping
Sleeping
File size: 9,394 Bytes
d8bc601 9bf9961 d8bc601 eacdb45 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 687900d d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 9bf9961 d8bc601 |
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
import gradio as gr
import requests
import json
import uuid
import concurrent.futures
from requests.exceptions import ChunkedEncodingError
import os
from dotenv import load_dotenv
load_dotenv()
# Define the endpoints
host = os.getenv("BACKEND_URL")
default_endpoint = f"{host}/chat/default"
memory_endpoint = f"{host}/chat/memory"
history_endpoint = f"{host}/view_history"
tarot_cards = [
"The Fool",
"The Magician",
"The High Priestess",
"The Empress",
"The Emperor",
"The Hierophant",
"The Lovers",
"The Chariot",
"Strength",
"The Hermit",
"Wheel of Fortune",
"Justice",
"The Hanged Man",
"Death",
"Temperance",
"The Devil",
"The Tower",
"The Star",
"The Moon",
"The Sun",
"Judgement",
"The World",
"Ace of Cups",
"Two of Cups",
"Three of Cups",
"Four of Cups",
"Five of Cups",
"Six of Cups",
"Seven of Cups",
"Eight of Cups",
"Nine of Cups",
"Ten of Cups",
"Page of Cups",
"Knight of Cups",
"Queen of Cups",
"King of Cups",
"Ace of Pentacles",
"Two of Pentacles",
"Three of Pentacles",
"Four of Pentacles",
"Five of Pentacles",
"Six of Pentacles",
"Seven of Pentacles",
"Eight of Pentacles",
"Nine of Pentacles",
"Ten of Pentacles",
"Page of Pentacles",
"Knight of Pentacles",
"Queen of Pentacles",
"King of Pentacles",
"Ace of Swords",
"Two of Swords",
"Three of Swords",
"Four of Swords",
"Five of Swords",
"Six of Swords",
"Seven of Swords",
"Eight of Swords",
"Nine of Swords",
"Ten of Swords",
"Page of Swords",
"Knight of Swords",
"Queen of Swords",
"King of Swords",
"Ace of Wands",
"Two of Wands",
"Three of Wands",
"Four of Wands",
"Five of Wands",
"Six of Wands",
"Seven of Wands",
"Eight of Wands",
"Nine of Wands",
"Ten of Wands",
"Page of Wands",
"Knight of Wands",
"Queen of Wands",
"King of Wands",
]
# Define the request payload structure
class ChatRequest:
def __init__(self, session_id, messages, model_id, temperature, seer_name, seer_personality):
self.session_id = session_id
self.messages = messages
self.model_id = model_id
self.temperature = temperature
self.seer_name = seer_name
self.seer_personality = seer_personality
class ChatRequestWithMemory(ChatRequest):
def __init__(self, session_id, messages, model_id, temperature, seer_name, seer_personality, summary_threshold):
super().__init__(session_id, messages, model_id, temperature, seer_name, seer_personality)
self.summary_threshold = summary_threshold
def compare_chatbots(session_id, messages, model_id, temperature, seer_name, seer_personality, summary_threshold, tarot_card):
# Convert messages list to a single string
# Prepare the payloads
print("tarot_card", tarot_card)
payload_default = json.dumps({
"session_id": session_id + "_default",
"messages": messages,
"model_id": model_id,
"temperature": temperature,
"tarot_card": tarot_card,
"seer_name": seer_name,
"seer_personality": seer_personality,
})
payload_memory = json.dumps({
"session_id": session_id + "_memory",
"messages": messages,
"model_id": model_id,
"temperature": temperature,
"seer_name": seer_name,
"seer_personality": seer_personality,
"tarot_card": tarot_card,
"summary_threshold": summary_threshold,
})
headers = {
'Content-Type': 'application/json'
}
def call_endpoint(url, payload):
try:
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code == 200:
try:
return response.text
except requests.exceptions.JSONDecodeError:
return "Error: Response is not valid JSON"
else:
return f"Error: {response.status_code} - {response.text}"
except ChunkedEncodingError:
return "Error: Response ended prematurely"
with concurrent.futures.ThreadPoolExecutor() as executor:
future_default = executor.submit(call_endpoint, default_endpoint, payload_default)
future_memory = executor.submit(call_endpoint, memory_endpoint, payload_memory)
response_default_text = future_default.result()
response_memory_text = future_memory.result()
return response_default_text, response_memory_text
# Function to handle chat interaction
def chat_interaction(session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, chat_history_default, chat_history_memory, tarot_card):
response_default, response_memory = compare_chatbots(session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, tarot_card)
chat_history_default.append((message, response_default))
chat_history_memory.append((message, response_memory))
message = ""
tarot_card = []
return message, chat_history_default, chat_history_memory, tarot_card
# Function to reload session ID and clear chat history
def reload_session_and_clear_chat():
new_session_id = str(uuid.uuid4())
new_session_id_memory = f"{new_session_id}_memory"
return new_session_id, new_session_id_memory, [], []
# Function to load chat history
def load_chat_history(session_id):
try:
response = requests.get(f"{history_endpoint}?session_id={session_id}")
if response.status_code == 200:
return response.json()
else:
return {"error": f"Error: {response.status_code} - {response.text}"}
except Exception as e:
return {"error": str(e)}
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Chatbot Comparison")
with gr.Row():
with gr.Column():
gr.Markdown("## Default Chatbot")
chatbot_default = gr.Chatbot(elem_id="chatbot_default")
with gr.Column():
gr.Markdown("## Memory Chatbot")
chatbot_memory = gr.Chatbot(elem_id="chatbot_memory")
with gr.Row():
message = gr.Textbox(label="Message", show_label=False, scale=3)
submit_button = gr.Button("Submit", scale=1, variant="primary")
session_id_default = str(uuid.uuid4())
model_id_choices = [
"openthai-llama3.1-70b",
"llama-3.1-8b-instant",
"llama-3.1-70b-versatile",
"typhoon-v1.5-instruct",
"typhoon-v1.5x-70b-instruct",
"gemma2-9b-it",
]
with gr.Accordion("Settings", open=False):
reload_button = gr.Button("Reload Session", scale=1, variant="secondary")
session_id = gr.Textbox(label="Session ID", value=session_id_default)
model_id = gr.Dropdown(label="Model ID", choices=model_id_choices, value=model_id_choices[0])
temperature = gr.Slider(0, 1, step=0.1, label="Temperature", value=0.5)
seer_name = gr.Textbox(label="Seer Name", value="แม่หมอแพตตี้")
seer_personality = gr.Textbox(label="Seer Personality", value="You are a friend who is always ready to help.")
tarot_card = gr.Dropdown(label="Tarot Card", value=[], choices=tarot_cards, multiselect=True)
summary_threshold = gr.Number(label="Summary Threshold", value=7)
with gr.Accordion("View History of Memory Chatbot", open=False):
session_id_memory = gr.Textbox(label="Session ID", value=f"{session_id_default}_memory")
load_history_button = gr.Button("Load Chat History", scale=1, variant="secondary") # New button
chat_history_json = gr.JSON(label="Chat History") # New JSON field
submit_button.click(
lambda session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, chatbot_default, chatbot_memory, tarot_card: chat_interaction(
session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, chatbot_default, chatbot_memory, tarot_card
),
inputs=[session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, chatbot_default, chatbot_memory, tarot_card],
outputs=[message, chatbot_default, chatbot_memory, tarot_card]
)
message.submit(
lambda session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, chatbot_default, chatbot_memory, tarot_card: chat_interaction(
session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, chatbot_default, chatbot_memory, tarot_card
),
inputs=[session_id, message, model_id, temperature, seer_name, seer_personality, summary_threshold, chatbot_default, chatbot_memory, tarot_card],
outputs=[message, chatbot_default, chatbot_memory, tarot_card]
)
reload_button.click(
reload_session_and_clear_chat,
inputs=[],
outputs=[session_id, session_id_memory, chatbot_default, chatbot_memory]
)
load_history_button.click(
load_chat_history,
inputs=[session_id_memory],
outputs=[chat_history_json]
)
# Launch the interface
demo.launch(show_api=False) |