Spaces:
Runtime error
Runtime error
import gradio as gr | |
import joblib | |
def findCase(query:list[str]) -> str: | |
new_sentences = query | |
# new_sentences = [sentence.lower().replace('[^\w\s]', '') for sentence in new_sentences] | |
case_pipe = joblib.load("case.joblib") | |
predictions = case_pipe.predict_proba(new_sentences) | |
# print("Predictions for new sentences:") | |
for sentence, prob in zip(new_sentences, predictions): | |
# max_one = "" | |
# max_score = 0.0 | |
top_values = [] | |
labels = case_pipe.named_steps['model'].classes_ | |
for label, probability in zip(labels, prob): | |
# if(max_score<prob): | |
# max_score = prob | |
# max_one = label | |
if(probability<0.08):continue | |
if len(top_values) < 3: | |
top_values.append([round(probability, 4), label]) | |
else: | |
tops = [i[0] for i in top_values] | |
min_value = min(tops) | |
if probability > min_value: | |
min_index = tops.index(min_value) | |
top_values[min_index] = [round(probability, 4), label] | |
# print("----------------------------------------") | |
# print(sentence) | |
# print("----------------------------------------") | |
if(len(top_values) == 0):return "" | |
resp = "" | |
for t in top_values: | |
# print(t) | |
if(resp!=''):resp+=', ' | |
resp+=t[1] | |
# print(f"{t[1]} - {round(100 * t[0], 2)}") | |
return resp | |
def findClient(query:list[str])->str: | |
client_pipe = joblib.load("client.joblib") | |
pred = client_pipe.predict(query) | |
return pred[0] | |
def greet(query): | |
query = [sentence.lower().replace('[^\w\s]', '') for sentence in [query]] | |
return findCase(query),findClient(query) | |
def build_gui(): | |
description = """ | |
<center><strong> | |
<font size='10'>Legal Up Model</font> | |
</strong></center> | |
<br> | |
<p>Welcome to the <a href='https://github.com/meet244/Legal-Up' target='_blank'>Legal Up</a> demo!</p> | |
<p> | |
Legal Up recommends suitable lawyers to clients based on concise case descriptions using advanced algorithms, ensuring clients find the right legal expertise. | |
</p> | |
<p>Great thanks to <a href='https://huggingface.co/meet244' target='_blank'>Meet Patel</a>, the major contributor of this | |
demo! | |
</p> | |
""" # noqa | |
article = """ | |
<p style='text-align: center'> | |
Case and Client are trained on private datasets, and we are persisting in refining and iterating upon it.<br/> | |
<a href='https://github.com/meet244/Legal-Up' target='_blank'>Legal Up: Lawyer Recommendation System</a> | |
</p> | |
""" # noqa | |
with gr.Blocks(title="Legal Up Model") as demo: | |
gr.HTML(description) | |
gr.Interface( | |
fn=greet, | |
inputs=gr.Textbox(label="Legal Case"), | |
outputs=[gr.Textbox(label="Case Type"),gr.Textbox(label="Client Type")], | |
examples=[ | |
["I'm a landscaper, and a customer is suing me for negligence after their property was damaged by a tree that I planted. I'm worried about the cost of defending the lawsuit, even if I win."], | |
["A customer is alleging that they were injured by one of our products. We are strongly denying liability, but we are deeply concerned about the damage this could do to our reputation."], | |
["I am a filmmaker and I am working on a new movie. I want to make sure that I do not infringe on any copyrights in my film. Can you help me review my script and identify any potential copyright issues?","I am a US citizen who is married to a foreign national. My spouse wants to immigrate to the United States. Can you help me file a petition for my spouse and assist them with the immigration process?","I was recently injured in a medical malpractice incident. I am considering filing a lawsuit against the doctor or hospital. Can you help me assess the strength of my case and advise me on how to proceed?"] | |
], | |
cache_examples=True, | |
allow_flagging='never' | |
) | |
gr.HTML(article) | |
return demo | |
if __name__ == "__main__": | |
build_gui().launch() | |