Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,40 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 7 |
-
|
| 8 |
-
# Install torch first
|
| 9 |
-
install("torch")
|
| 10 |
-
|
| 11 |
-
# Install flash_attn next
|
| 12 |
-
install("flash_attn")
|
| 13 |
-
|
| 14 |
-
# Now install other dependencies
|
| 15 |
-
install("huggingface_hub==0.22.2")
|
| 16 |
-
install("transformers")
|
| 17 |
-
install("openai")
|
| 18 |
-
install("gradio")
|
| 19 |
-
install("einops")
|
| 20 |
-
install("timm")
|
| 21 |
|
| 22 |
import gradio as gr
|
| 23 |
from huggingface_hub import InferenceClient
|
| 24 |
-
from transformers import
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
pipe = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa", trust_remote_code=True)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 33 |
|
| 34 |
-
def respond(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 36 |
for val in history:
|
| 37 |
if val[0]:
|
| 38 |
messages.append({"role": "user", "content": val[0]})
|
| 39 |
if val[1]:
|
| 40 |
messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
|
| 43 |
response = ""
|
|
@@ -49,45 +46,167 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 49 |
top_p=top_p,
|
| 50 |
):
|
| 51 |
token = message.choices[0].delta.content
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if __name__ == "__main__":
|
| 93 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.system('pip install transformers')
|
| 3 |
+
os.system('pip install datasets')
|
| 4 |
+
os.system('pip install gradio')
|
| 5 |
+
os.system('pip install minijinja')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
+
from transformers import pipeline
|
| 10 |
+
from datasets import load_dataset
|
| 11 |
|
| 12 |
+
dataset = load_dataset("ibunescu/qa_legal_dataset_train")
|
|
|
|
| 13 |
|
| 14 |
+
# Use a pipeline as a high-level helper
|
| 15 |
+
pipe = pipeline("fill-mask", model="nlpaueb/legal-bert-base-uncased")
|
| 16 |
|
| 17 |
+
"""
|
| 18 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 19 |
+
"""
|
| 20 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 21 |
|
| 22 |
+
def respond(
|
| 23 |
+
message,
|
| 24 |
+
history: list[tuple[str, str]],
|
| 25 |
+
system_message,
|
| 26 |
+
max_tokens,
|
| 27 |
+
temperature,
|
| 28 |
+
top_p,
|
| 29 |
+
):
|
| 30 |
messages = [{"role": "system", "content": system_message}]
|
| 31 |
+
|
| 32 |
for val in history:
|
| 33 |
if val[0]:
|
| 34 |
messages.append({"role": "user", "content": val[0]})
|
| 35 |
if val[1]:
|
| 36 |
messages.append({"role": "assistant", "content": val[1]})
|
| 37 |
+
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
response = ""
|
|
|
|
| 46 |
top_p=top_p,
|
| 47 |
):
|
| 48 |
token = message.choices[0].delta.content
|
| 49 |
+
if token is not None:
|
| 50 |
+
response += token
|
| 51 |
+
yield response, history + [(message, response)]
|
| 52 |
+
|
| 53 |
+
def score_argument(argument):
|
| 54 |
+
# Keywords related to legal arguments
|
| 55 |
+
merits_keywords = ["compelling", "convincing", "strong", "solid"]
|
| 56 |
+
laws_keywords = ["statute", "law", "regulation", "act"]
|
| 57 |
+
precedents_keywords = ["precedent", "case", "ruling", "decision"]
|
| 58 |
+
verdict_keywords = ["guilty", "innocent", "verdict", "judgment"]
|
| 59 |
+
|
| 60 |
+
# Initialize scores
|
| 61 |
+
merits_score = sum([1 for word in merits_keywords if word in argument.lower()])
|
| 62 |
+
laws_score = sum([1 for word in laws_keywords if word in argument.lower()])
|
| 63 |
+
precedents_score = sum([1 for word in precedents_keywords if word in argument.lower()])
|
| 64 |
+
verdict_score = sum([1 for word in verdict_keywords if word in argument.lower()])
|
| 65 |
+
length_score = len(argument.split())
|
| 66 |
+
|
| 67 |
+
# Additional evaluations for legal standards
|
| 68 |
+
merits_value = merits_score * 2 # Each keyword in merits is valued at 2 points
|
| 69 |
+
laws_value = laws_score * 3 # Each keyword in laws is valued at 3 points
|
| 70 |
+
precedents_value = precedents_score * 4 # Each keyword in precedents is valued at 4 points
|
| 71 |
+
verdict_value = verdict_score * 5 # Each keyword in verdict is valued at 5 points
|
| 72 |
+
|
| 73 |
+
# Total score: Sum of all individual scores
|
| 74 |
+
total_score = merits_value + laws_value + precedents_value + verdict_value + length_score
|
| 75 |
+
|
| 76 |
+
return total_score
|
| 77 |
+
|
| 78 |
+
def color_code(score):
|
| 79 |
+
# Green for high score, yellow for medium, red for low
|
| 80 |
+
if score > 50:
|
| 81 |
+
return "green"
|
| 82 |
+
elif score > 30:
|
| 83 |
+
return "yellow"
|
| 84 |
+
else:
|
| 85 |
+
return "red"
|
| 86 |
+
|
| 87 |
+
# Custom CSS for white background and black text for input and output boxes
|
| 88 |
+
custom_css = """
|
| 89 |
+
body {
|
| 90 |
+
background-color: #ffffff;
|
| 91 |
+
color: #000000;
|
| 92 |
+
font-family: Arial, sans-serif;
|
| 93 |
+
}
|
| 94 |
+
.gradio-container {
|
| 95 |
+
max-width: 1000px;
|
| 96 |
+
margin: 0 auto;
|
| 97 |
+
padding: 20px;
|
| 98 |
+
background-color: #ffffff;
|
| 99 |
+
border: 1px solid #e0e0e0;
|
| 100 |
+
border-radius: 8px;
|
| 101 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
| 102 |
+
}
|
| 103 |
+
.gr-button {
|
| 104 |
+
background-color: #ffffff !important;
|
| 105 |
+
border-color: #ffffff !important;
|
| 106 |
+
color: #000000 !important;
|
| 107 |
+
}
|
| 108 |
+
.gr-button:hover {
|
| 109 |
+
background-color: #ffffff !important;
|
| 110 |
+
border-color: #004085 !important;
|
| 111 |
+
}
|
| 112 |
+
.gr-input, .gr-textbox, .gr-slider, .gr-markdown, .gr-chatbox {
|
| 113 |
+
border-radius: 4px;
|
| 114 |
+
border: 1px solid #ced4da;
|
| 115 |
+
background-color: #ffffff !important;
|
| 116 |
+
color: #000000 !important;
|
| 117 |
+
}
|
| 118 |
+
.gr-input:focus, .gr-textbox:focus, .gr-slider:focus {
|
| 119 |
+
border-color: #ffffff;
|
| 120 |
+
outline: 0;
|
| 121 |
+
box-shadow: 0 0 0 0.2rem rgba(255, 255, 255, 1.0);
|
| 122 |
+
}
|
| 123 |
+
#flagging-button {
|
| 124 |
+
display: none;
|
| 125 |
+
}
|
| 126 |
+
footer {
|
| 127 |
+
display: none;
|
| 128 |
+
}
|
| 129 |
+
.chatbox .chat-container .chat-message {
|
| 130 |
+
background-color: #ffffff !important;
|
| 131 |
+
color: #000000 !important;
|
| 132 |
+
}
|
| 133 |
+
.chatbox .chat-container .chat-message-input {
|
| 134 |
+
background-color: #ffffff !important;
|
| 135 |
+
color: #000000 !important;
|
| 136 |
+
}
|
| 137 |
+
.gr-markdown {
|
| 138 |
+
background-color: #ffffff !important;
|
| 139 |
+
color: #000000 !important;
|
| 140 |
+
}
|
| 141 |
+
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3, .gr-markdown h4, .gr-markdown h5, .gr-markdown h6, .gr-markdown p, .gr-markdown ul, .gr-markdown ol, .gr-markdown li {
|
| 142 |
+
color: #000000 !important;
|
| 143 |
+
}
|
| 144 |
+
.score-box {
|
| 145 |
+
width: 60px;
|
| 146 |
+
height: 60px;
|
| 147 |
+
display: flex;
|
| 148 |
+
align-items: center;
|
| 149 |
+
justify-content: center;
|
| 150 |
+
font-size: 12px;
|
| 151 |
+
font-weight: bold;
|
| 152 |
+
color: black;
|
| 153 |
+
margin: 5px;
|
| 154 |
+
}
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
# Function to facilitate the conversation between the two chatbots
|
| 158 |
+
def chat_between_bots(system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message):
|
| 159 |
+
response1, history1 = list(respond(message, history1, system_message1, max_tokens, temperature, top_p))[-1]
|
| 160 |
+
response2, history2 = list(respond(message, history2, system_message2, max_tokens, temperature, top_p))[-1]
|
| 161 |
+
shared_history.append(f"Prosecutor: {response1}")
|
| 162 |
+
shared_history.append(f"Defense Attorney: {response2}")
|
| 163 |
+
|
| 164 |
+
# Ensure the responses are balanced by limiting the length
|
| 165 |
+
max_length = max(len(response1), len(response2))
|
| 166 |
+
response1 = response1[:max_length]
|
| 167 |
+
response2 = response2[:max_length]
|
| 168 |
+
|
| 169 |
+
# Calculate scores and scoring matrices
|
| 170 |
+
score1 = score_argument(response1)
|
| 171 |
+
score2 = score_argument(response2)
|
| 172 |
+
|
| 173 |
+
prosecutor_color = color_code(score1)
|
| 174 |
+
defense_color = color_code(score2)
|
| 175 |
+
|
| 176 |
+
prosecutor_score_color = f"<div class='score-box' style='background-color:{prosecutor_color};'>Score: {score1}</div>"
|
| 177 |
+
defense_score_color = f"<div class='score-box' style='background-color:{defense_color};'>Score: {score2}</div>"
|
| 178 |
+
|
| 179 |
+
return response1, response2, history1, history2, shared_history, f"{response1}\n\n{response2}", prosecutor_score_color, defense_score_color
|
| 180 |
+
|
| 181 |
+
# Gradio interface
|
| 182 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 183 |
+
history1 = gr.State([])
|
| 184 |
+
history2 = gr.State([])
|
| 185 |
+
shared_history = gr.State([])
|
| 186 |
+
message = gr.Textbox(label="Shared Input Box")
|
| 187 |
+
system_message1 = gr.State("You are an expert at legal Prosecution.")
|
| 188 |
+
system_message2 = gr.State("You are an expert at legal Defense.")
|
| 189 |
+
max_tokens = gr.State(512) # Adjusted to balance response length
|
| 190 |
+
temperature = gr.State(0.7)
|
| 191 |
+
top_p = gr.State(0.95)
|
| 192 |
+
|
| 193 |
+
with gr.Row():
|
| 194 |
+
with gr.Column(scale=4):
|
| 195 |
+
prosecutor_response = gr.Textbox(label="Prosecutor's Response", interactive=False)
|
| 196 |
+
with gr.Column(scale=1):
|
| 197 |
+
prosecutor_score_color = gr.HTML()
|
| 198 |
+
|
| 199 |
+
with gr.Row():
|
| 200 |
+
with gr.Column(scale=4):
|
| 201 |
+
defense_response = gr.Textbox(label="Defense Attorney's Response", interactive=False)
|
| 202 |
+
with gr.Column(scale=1):
|
| 203 |
+
defense_score_color = gr.HTML()
|
| 204 |
+
|
| 205 |
+
shared_argument = gr.Textbox(label="Shared Argument", interactive=False)
|
| 206 |
+
|
| 207 |
+
submit_btn = gr.Button("Submit")
|
| 208 |
+
|
| 209 |
+
submit_btn.click(chat_between_bots, inputs=[system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message], outputs=[prosecutor_response, defense_response, history1, history2, shared_history, shared_argument, prosecutor_score_color, defense_score_color])
|
| 210 |
+
|
| 211 |
if __name__ == "__main__":
|
| 212 |
demo.launch()
|