Spaces:
Sleeping
Sleeping
Noshitha
commited on
Commit
·
3bc5722
1
Parent(s):
a24806d
Add application file
Browse files
app.py
CHANGED
|
@@ -2,27 +2,51 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
from peft import PeftModel
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
adapter_model = "Noshitha98/TinyLlama-ToS-Finetuned"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
model
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def check_clause(text):
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
prompt = f"Detect unusual or risky clauses in this contract:\n\n{text}\n\nResponse:"
|
| 18 |
-
output =
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Gradio UI
|
| 22 |
iface = gr.Interface(
|
| 23 |
fn=check_clause,
|
| 24 |
inputs=gr.Textbox(lines=8, placeholder="Paste contract clause here..."),
|
| 25 |
-
outputs=
|
|
|
|
|
|
|
| 26 |
title="Contract Clause Checker",
|
| 27 |
description="Paste contract text to detect unusual or anomalous clauses using TinyLlama-ToS-Finetuned."
|
| 28 |
)
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
from peft import PeftModel
|
| 4 |
|
| 5 |
+
# Global cache for the pipeline
|
| 6 |
+
pipe = None
|
|
|
|
| 7 |
|
| 8 |
+
def load_model():
|
| 9 |
+
"""Load the TinyLlama model with LoRA adapters (cached)."""
|
| 10 |
+
global pipe
|
| 11 |
+
if pipe is None:
|
| 12 |
+
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 13 |
+
adapter_model = "Noshitha98/TinyLlama-ToS-Finetuned"
|
| 14 |
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(base_model)
|
| 17 |
+
model = PeftModel.from_pretrained(model, adapter_model)
|
| 18 |
+
|
| 19 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 20 |
+
return pipe
|
| 21 |
|
| 22 |
def check_clause(text):
|
| 23 |
+
generator = load_model()
|
| 24 |
+
|
| 25 |
+
if not text.strip():
|
| 26 |
+
return [("⚠️ Please enter contract text.", None)]
|
| 27 |
+
|
| 28 |
prompt = f"Detect unusual or risky clauses in this contract:\n\n{text}\n\nResponse:"
|
| 29 |
+
output = generator(prompt, max_length=512, do_sample=False)[0]["generated_text"]
|
| 30 |
+
|
| 31 |
+
# Clean the response
|
| 32 |
+
response = output.split("Response:")[-1].strip()
|
| 33 |
+
|
| 34 |
+
# Simple heuristic: highlight sentences with "risk", "penalty", "terminate", "discretion"
|
| 35 |
+
risky_keywords = ["penalty", "terminate", "discretion", "risk", "sole", "modify", "liability"]
|
| 36 |
+
highlights = []
|
| 37 |
+
for sentence in text.split(". "):
|
| 38 |
+
label = "risky" if any(word.lower() in sentence.lower() for word in risky_keywords) else None
|
| 39 |
+
highlights.append((sentence.strip(), label))
|
| 40 |
+
|
| 41 |
+
return highlights
|
| 42 |
|
| 43 |
+
# Gradio UI with highlighted output
|
| 44 |
iface = gr.Interface(
|
| 45 |
fn=check_clause,
|
| 46 |
inputs=gr.Textbox(lines=8, placeholder="Paste contract clause here..."),
|
| 47 |
+
outputs=gr.HighlightedText(
|
| 48 |
+
color_map={"risky": "red"}
|
| 49 |
+
),
|
| 50 |
title="Contract Clause Checker",
|
| 51 |
description="Paste contract text to detect unusual or anomalous clauses using TinyLlama-ToS-Finetuned."
|
| 52 |
)
|