Judge-AI / app.py
darthPanda's picture
resolved bug
5f9a694
import gradio as gr
import os
import time
from pdf2image import convert_from_path
import utils
import judge
import perspective
import settlement
import uae_law
from gradio.themes.base import Base
from gradio.themes.utils import colors, fonts, sizes
from typing import Iterable
from dotenv import load_dotenv
import prompt_tracing
load_dotenv()
iteration = 0
iteration_limit = 50
class Seafoam(Base):
def __init__(
self,
*,
primary_hue: colors.Color | str = colors.emerald,
secondary_hue: colors.Color | str = colors.blue,
neutral_hue: colors.Color | str = colors.gray,
spacing_size: sizes.Size | str = sizes.spacing_md,
radius_size: sizes.Size | str = sizes.radius_md,
text_size: sizes.Size | str = sizes.text_lg,
font: fonts.Font
| str
| Iterable[fonts.Font | str] = (
fonts.GoogleFont("Quicksand"),
"ui-sans-serif",
"sans-serif",
),
font_mono: fonts.Font
| str
| Iterable[fonts.Font | str] = (
fonts.GoogleFont("IBM Plex Mono"),
"ui-monospace",
"monospace",
),
):
super().__init__(
primary_hue=primary_hue,
secondary_hue=secondary_hue,
neutral_hue=neutral_hue,
spacing_size=spacing_size,
radius_size=radius_size,
text_size=text_size,
font=font,
font_mono=font_mono,
)
js_func = """
function refresh() {
const url = new URL(window.location);
if (url.searchParams.get('__theme') !== 'light') {
url.searchParams.set('__theme', 'light');
window.location.href = url.href;
}
}
"""
seafoam = Seafoam()
print("restarted")
judge = judge.Judge()
perspective = perspective.Perspective()
settlement = settlement.Settlement()
uae_law = uae_law.Uae_law()
chat_msg = None
def print_like_dislike(x: gr.LikeData):
print(x.index, x.value, x.liked)
##################################################################
### Judgement utils
##################################################################
##################################################################
##################################################################
def add_message(history, message):
global iteration
if (judge.stage == 1 and perspective.stage == 1 and settlement.stage == 0.8 and uae_law.stage == 1):
iteration = iteration + 1
for x in message["files"]:
if prompt_tracing.tracking:
prompt_tracing.send_file_over_discord(x)
judge.lawsuit_extracted_text = utils.extract_text_from_pdf(x)
perspective.lawsuit_extracted_text = utils.extract_text_from_pdf(x)
settlement.lawsuit_extracted_text = utils.extract_text_from_pdf(x)
uae_law.lawsuit_extracted_text = utils.extract_text_from_pdf(x)
img = convert_from_path(x)[0]
img_path = os.path.join("data", "img.png")
img.save(img_path)
# history.append(((x,), None))
# history.append(((img,), None))
history.append(((img_path,), None))
# if message["text"] is not None or message["text"] != '':
if message["text"] != '' and message["text"] is not None:
history.append((message["text"], None))
# print(history)
if iteration < iteration_limit:
return history, gr.MultimodalTextbox(value=None, interactive=False), gr.Markdown(f"Credits used: {iteration}")
else:
return history, gr.MultimodalTextbox(value=None, interactive=False), gr.Markdown(f"Credits expired")
def bot(history):
# try:
# print(history)
input_text = history[-1][0]
if isinstance(input_text, tuple):
input_text = input_text[0]
if (iteration < iteration_limit):
response = judge.generate_response(input_text)
else:
response = "Your credits have ended. Please contact the JudgeAI team"
history[-1][1] = ""
for character in response:
history[-1][1] += character
time.sleep(0.001)
yield history
# # Additional message generation
if judge.additional_message != "":
# print(judge.additional_message)
additional_message = judge.additional_message
# Since the additional message should be yielded separately,
# first ensure there's a new entry in the history for it.
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
print(judge.stage)
if judge.stage == 4.9:
additional_message = judge.stage_4_9_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = judge.stage_4_9_5_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = judge.stage_5_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = judge.stage_6_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = judge.stage_7_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
# except Exception as e:
# print(e)
# print("errored")
##################################################################
### Perspective utils
##################################################################
##################################################################
##################################################################
def bot_perspective(history):
# try:
# print(history)
input_text = history[-1][0]
if isinstance(input_text, tuple):
input_text = input_text[0]
if (iteration < iteration_limit):
response = perspective.generate_response(input_text)
else:
response = "Your credits have ended. Please contact the JudgeAI team"
# response = perspective.generate_response(input_text)
history[-1][1] = ""
for character in response:
history[-1][1] += character
time.sleep(0.001)
yield history
# # Additional message generation
if perspective.additional_message != "":
additional_message = perspective.additional_message
# Since the additional message should be yielded separately,
# first ensure there's a new entry in the history for it.
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
# if judge.stage == 4.9:
additional_message = perspective.stage_2_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = perspective.stage_3_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
##################################################################
### Settlement utils
##################################################################
##################################################################
##################################################################
def bot_settlement(history):
# try:
# print(history)
input_text = history[-1][0]
if isinstance(input_text, tuple):
input_text = input_text[0]
if (iteration < iteration_limit):
response = settlement.generate_response(input_text)
else:
response = "Your credits have ended. Please contact the JudgeAI team"
# response = settlement.generate_response(input_text)
history[-1][1] = ""
for character in response:
history[-1][1] += character
time.sleep(0.001)
yield history
# # Additional message generation
# if settlement.additional_message != "":
# additional_message = settlement.additional_message
# # Since the additional message should be yielded separately,
# # first ensure there's a new entry in the history for it.
if settlement.stage == 1:
history.append([None, ""])
additional_message = settlement.stage_1_call()
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
history.append([None, ""])
additional_message = settlement.stage_2_call()
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
history.append([None, ""])
additional_message = settlement.stage_3_call()
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
##################################################################
### UAE law utils
##################################################################
##################################################################
##################################################################
def bot_uae_law(history):
# try:
# print(history)
input_text = history[-1][0]
if isinstance(input_text, tuple):
input_text = input_text[0]
if (iteration < iteration_limit):
response = uae_law.generate_response(input_text)
else:
response = "Your credits have ended. Please contact the uae_lawAI team"
# response = uae_law.generate_response(input_text)
history[-1][1] = ""
for character in response:
history[-1][1] += character
time.sleep(0.001)
yield history
# # Additional message generation
if uae_law.additional_message != "":
additional_message = uae_law.additional_message
# Since the additional message should be yielded separately,
# first ensure there's a new entry in the history for it.
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
print(uae_law.stage)
if uae_law.stage == 4.9:
additional_message = uae_law.stage_4_9_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = uae_law.stage_4_9_5_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = uae_law.stage_5_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = uae_law.stage_5_9_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = uae_law.stage_5_9_5_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = uae_law.stage_6_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
additional_message = uae_law.stage_7_call()
history.append([None, ""])
for character in additional_message:
history[-1][1] += character
time.sleep(0.001)
yield history
def get_iteration():
# return gr.Markdown(iteration)
chatbot = gr.Chatbot(
[[None, "Upload the lawsuit claim"]],
# [],
elem_id="chatbot",
bubble_full_width=False,
show_label=False,
# scale=1,
height=600#-300
)
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["file"], placeholder="Enter message or upload file...", show_label=False)
if iteration < iteration_limit:
credits_markdown = gr.Markdown(f"Credits used: {iteration}")
else:
credits_markdown = gr.Markdown(f"Credits expired")
return chatbot, chat_input, credits_markdown
def restart():
if prompt_tracing.tracking:
prompt_tracing.make_discord_trace_intro()
print("restarted")
judge.reset()
perspective.reset()
settlement.reset()
uae_law.reset()
# with gr.Blocks(theme=seafoam, js=js_func, fill_height=True) as judge_demo:
with gr.Blocks(theme=seafoam) as judge_demo:
try:
chatbot = gr.Chatbot(
[[None, "Upload the lawsuit claim"]],
# [],
elem_id="chatbot",
bubble_full_width=False,
show_label=False,
# scale=1,
height=600#-300
)
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["file"], placeholder="Enter message or upload file...", show_label=False)
credits_markdown = gr.Markdown(f"Credits used: ")
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input, credits_markdown])
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
judge_demo.load(get_iteration, None, [chatbot, chat_input, credits_markdown])
except Exception as e:
print(e)
print("errored")
with gr.Blocks(theme=seafoam) as perspective_demo:
try:
chatbot_perspective = gr.Chatbot(
[[None, "Upload the lawsuit claim"]],
# [],
elem_id="chatbot",
bubble_full_width=False,
show_label=False,
# scale=1,
height=600#-300
)
chat_input_perspective = gr.MultimodalTextbox(interactive=True, file_types=["file"], placeholder="Enter message or upload file...", show_label=False)
perspective_credits_markdown = gr.Markdown(f"Credits used: ")
chat_msg_perspective = chat_input_perspective.submit(add_message, [chatbot_perspective, chat_input_perspective], [chatbot_perspective, chat_input_perspective, perspective_credits_markdown])
bot_msg_perspective = chat_msg_perspective.then(bot_perspective, chatbot_perspective, chatbot_perspective, api_name="bot_response_perspective")
bot_msg_perspective.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input_perspective])
perspective_demo.load(get_iteration, None, [chatbot_perspective, chat_input_perspective, perspective_credits_markdown])
except Exception as e:
print(e)
print("errored")
with gr.Blocks(theme=seafoam) as settlement_demo:
try:
chatbot_settlement = gr.Chatbot(
[[None, "Upload the lawsuit claim"]],
# [],
elem_id="chatbot",
bubble_full_width=False,
show_label=False,
# scale=1,
height=600#-300
)
chat_input_settlement = gr.MultimodalTextbox(interactive=True, file_types=["file"], placeholder="Enter message or upload file...", show_label=False)
settlement_credits_markdown = gr.Markdown(f"Credits used: ")
chat_msg_settlement = chat_input_settlement.submit(add_message, [chatbot_settlement, chat_input_settlement], [chatbot_settlement, chat_input_settlement, settlement_credits_markdown])
bot_msg_settlement = chat_msg_settlement.then(bot_settlement, chatbot_settlement, chatbot_settlement, api_name="bot_response_settlement")
bot_msg_settlement.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input_settlement])
settlement_demo.load(get_iteration, None, [chatbot_settlement, chat_input_settlement, settlement_credits_markdown])
except Exception as e:
print(e)
print("errored")
with gr.Blocks(theme=seafoam) as uae_law_demo:
try:
chatbot_uae_law = gr.Chatbot(
[[None, "Upload the lawsuit claim"]],
# [],
elem_id="chatbot",
bubble_full_width=False,
show_label=False,
# scale=1,
height=600#-300
)
chat_input_uae_law = gr.MultimodalTextbox(interactive=True, file_types=["file"], placeholder="Enter message or upload file...", show_label=False)
uae_law_credits_markdown = gr.Markdown(f"Credits used: ")
chat_msg_uae_law = chat_input_uae_law.submit(add_message, [chatbot_uae_law, chat_input_uae_law], [chatbot_uae_law, chat_input_uae_law, uae_law_credits_markdown])
bot_msg_uae_law = chat_msg_uae_law.then(bot_uae_law, chatbot_uae_law, chatbot_uae_law, api_name="bot_response_uae_law")
bot_msg_uae_law.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input_uae_law])
uae_law_demo.load(get_iteration, None, [chatbot_uae_law, chat_input_uae_law, uae_law_credits_markdown])
except Exception as e:
print(e)
print("errored")
with gr.Blocks(theme=seafoam, js=js_func, fill_height=True) as demo:
gr.Image("assets//header_mini.png", show_download_button=False, show_label=False)
gr.TabbedInterface([judge_demo, perspective_demo, settlement_demo, uae_law_demo], ["Judgement", "Perspective", "Settlement", "UAE law"])
# credits_markdown
# credits_markdown = gr.Markdown(f"Credits used: {iteration}")
# chat_input.submit(get_iteration, None, credits_markdown)
demo.load(restart, None, None)
demo.launch()