File size: 6,763 Bytes
498a8a3
918f623
498a8a3
918f623
498a8a3
 
 
918f623
498a8a3
3f3fd40
7bfb077
 
 
498a8a3
 
 
 
 
 
 
7bfb077
 
918f623
 
 
7bfb077
 
 
 
 
 
 
 
918f623
 
 
7bfb077
918f623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfb077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bf184c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfb077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -----------------------------------------------------
# 1) MUST be the FIRST command
# -----------------------------------------------------
import streamlit as st
st.set_page_config(page_title="ClauseWise", layout="wide")

# -----------------------------------------------------
# 2) All other imports AFTER set_page_config
# -----------------------------------------------------
from pypdf import PdfReader
from docx import Document
import re
from multilingual import UI_TEXT, translate_text
from backup_features import (
    extract_entities,
    extract_clauses,
    get_risks,
    fairness_score,
    alternative_clauses
)
from transformers import T5Tokenizer, T5ForConditionalGeneration

# -----------------------------------------------------
# 3) Load model
# -----------------------------------------------------
@st.cache_resource
def load_chat_model():
    tokenizer = T5Tokenizer.from_pretrained("t5-small")
    model = T5ForConditionalGeneration.from_pretrained("t5-small")
    return tokenizer, model

tokenizer, chat_model = load_chat_model()

# -----------------------------------------------------
# 4) Chat response function
# -----------------------------------------------------
def chat_response(user_msg, lang):
    msg = user_msg.lower()

    # Store conversation
    if "history" not in st.session_state:
        st.session_state["history"] = []

    # Basic rule-based NDA responses
    if "nda" in msg:
        reply = "This NDA helps protect confidential information between parties."
    elif "duration" in msg or "how long" in msg:
        reply = "NDAs commonly last 1–5 years depending on the agreement."
    elif "penalty" in msg or "violate" in msg:
        reply = "Penalties depend on governing law and agreement provisions."
    elif "can i work" in msg or "competition" in msg:
        reply = "NDAs do NOT restrict employment. That is a Non-Compete Agreement."
    elif "termination" in msg:
        reply = "Termination clauses define when the NDA ends and obligations after that."
    else:
        reply = "I can help with NDA clauses, risks, obligations, confidentiality."

    reply += "\n\n⚠️ Educational only, not legal advice."

    return translate_text(reply, lang)

# -------------------------------
# Utility Functions
# -------------------------------
def is_nda(text):
    return "nda" in text.lower() or "non-disclosure" in text.lower()

def read_pdf(file):
    reader = PdfReader(file)
    return "\n".join(page.extract_text() for page in reader.pages)

def read_docx(file):
    doc = Document(file)
    return "\n".join(p.text for p in doc.paragraphs)

def read_txt(file):
    return file.read().decode("utf-8")

# -------------------------------
# Streamlit UI
# -------------------------------
st.set_page_config(page_title="ClauseWise", layout="wide")

# Language Selector
lang = st.sidebar.selectbox("🌐 Language", ["en", "hi", "ta", "te", "kn"])

# Title
st.title("πŸ“„ ClauseWise – Multilingual NDA Analyzer")

uploaded = st.file_uploader(UI_TEXT["upload_title"][lang], type=["pdf", "txt", "docx"])

if uploaded:
    ext = uploaded.name.split(".")[-1]

    if ext == "pdf":
        text = read_pdf(uploaded)
    elif ext == "txt":
        text = read_txt(uploaded)
    elif ext == "docx":
        text = read_docx(uploaded)
    else:
        st.error(UI_TEXT["error_not_nda"][lang])
        st.stop()

    if not is_nda(text):
        st.error(UI_TEXT["error_not_nda"][lang])
        st.stop()

    st.success(UI_TEXT["success_nda"][lang])

    tabs = st.tabs([
        UI_TEXT["tab_clauses"][lang],
        UI_TEXT["tab_risks"][lang],
        UI_TEXT["tab_fairness"][lang],
        UI_TEXT["tab_entities"][lang],
        UI_TEXT["tab_alternatives"][lang],
        UI_TEXT["tab_chat"][lang]
    ])

    # -----------------------------
# TAB 1 β€” CLAUSE SIMPLIFICATION
# -----------------------------
with tabs[0]:
    st.header(UI_TEXT["clause_simplify"][lang])

    clauses = extract_clauses(text)

    mode = st.radio(UI_TEXT["choose_mode"][lang],
                    [UI_TEXT["eli5"][lang],
                     UI_TEXT["simple"][lang],
                     UI_TEXT["pro"][lang]])

    for c in clauses:
        st.subheader(c["title"])
        if mode == UI_TEXT["eli5"][lang]:
            st.write(translate_text(c["eli5"], lang))
        elif mode == UI_TEXT["simple"][lang]:
            st.write(translate_text(c["simple"], lang))
        else:
            # βœ… FIXED SYNTAX ERROR HERE
            st.write(translate_text(c["pro"], lang))


    # -----------------------------
    # TAB 2 β€” RISKS
    # -----------------------------
    with tabs[1]:
        st.header(UI_TEXT["risk_title"][lang])
        risks = get_risks(text)
        for r in risks:
            st.error("⚠️ " + translate_text(r, lang))

    # -----------------------------
    # TAB 3 β€” FAIRNESS
    # -----------------------------
    with tabs[2]:
        st.header(UI_TEXT["fairness_title"][lang])
        score = fairness_score(text)

        st.write(f"**{UI_TEXT['your_position'][lang]}:** {score['user']}%")
        st.write(f"**{UI_TEXT['company_position'][lang]}:** {score['company']}%")

        st.progress(score["user"] / 100)

    # -----------------------------
    # TAB 4 β€” ENTITIES
    # -----------------------------
    with tabs[3]:
        st.header(UI_TEXT["entities_title"][lang])
        ents = extract_entities(text)

        st.write("**Parties:**", ents["parties"])
        st.write("**Dates:**", ents["dates"])
        st.write("**Amounts:**", ents["money"])

    # -----------------------------
    # TAB 5 β€” ALTERNATIVES
    # -----------------------------
    with tabs[4]:
        st.header(UI_TEXT["alt_title"][lang])
        alts = alternative_clauses(text)
        for a in alts:
            st.info(translate_text(a, lang))

    # -----------------------------
    # TAB 6 β€” LEGAL CHAT
    # -----------------------------
    with tabs[5]:
        st.header(UI_TEXT["chat_title"][lang])

        if "chat_history" not in st.session_state:
            st.session_state.chat_history = []

        for msg in st.session_state.chat_history:
            st.chat_message(msg["role"]).write(msg["text"])

        user_msg = st.chat_input(UI_TEXT["chat_placeholder"][lang])

        if user_msg:
            st.session_state.chat_history.append({"role": "user", "text": user_msg})
            st.chat_message("user").write(user_msg)

            bot_reply = chat_response(user_msg, lang)
            st.session_state.chat_history.append({"role": "assistant", "text": bot_reply})

            st.chat_message("assistant").write(bot_reply)

# Footer Disclaimer
st.info("⚠️ ClauseWise provides **educational legal insights only** β€” this is **NOT legal advice**.")