TabasumDev commited on
Commit
8c156f5
Β·
verified Β·
1 Parent(s): 297372e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py CHANGED
@@ -261,3 +261,135 @@
261
  # # πŸ”₯ Run Streamlit App
262
  # if __name__ == '__main__':
263
  # main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  # # πŸ”₯ Run Streamlit App
262
  # if __name__ == '__main__':
263
  # main()
264
+
265
+ import streamlit as st
266
+ import os
267
+ import re
268
+ import torch
269
+ from transformers import AutoModelForCausalLM, AutoTokenizer
270
+ from PyPDF2 import PdfReader
271
+ from peft import get_peft_model, LoraConfig, TaskType
272
+
273
+ # βœ… Force CPU execution for Hugging Face Spaces
274
+ device = torch.device("cpu")
275
+
276
+ # πŸ”Ή Load IBM Granite Model (CPU-Compatible)
277
+ MODEL_NAME = "ibm-granite/granite-3.1-2b-instruct"
278
+
279
+ model = AutoModelForCausalLM.from_pretrained(
280
+ MODEL_NAME,
281
+ device_map="cpu", # Force CPU execution
282
+ torch_dtype=torch.float32 # Use float32 since Hugging Face runs on CPU
283
+ )
284
+
285
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
286
+
287
+ # πŸ”Ή Apply LoRA Fine-Tuning Configuration
288
+ lora_config = LoraConfig(
289
+ r=8,
290
+ lora_alpha=32,
291
+ target_modules=["q_proj", "v_proj"],
292
+ lora_dropout=0.1,
293
+ bias="none",
294
+ task_type=TaskType.CAUSAL_LM
295
+ )
296
+ model = get_peft_model(model, lora_config)
297
+ model.eval()
298
+
299
+ # πŸ›  Function to Read & Extract Text from PDFs
300
+ def read_files(file):
301
+ file_context = ""
302
+ reader = PdfReader(file)
303
+
304
+ for page in reader.pages:
305
+ text = page.extract_text()
306
+ if text:
307
+ file_context += text + "\n"
308
+
309
+ return file_context.strip()
310
+
311
+ # πŸ›  Function to Format AI Prompts
312
+ def format_prompt(system_msg, user_msg, file_context=""):
313
+ if file_context:
314
+ system_msg += f" The user has provided a contract document. Use its context to generate insights, but do not repeat or summarize the document itself."
315
+ return [
316
+ {"role": "system", "content": system_msg},
317
+ {"role": "user", "content": user_msg}
318
+ ]
319
+
320
+ # πŸ›  Function to Generate AI Responses
321
+ def generate_response(input_text, max_tokens=1000, top_p=0.9, temperature=0.7):
322
+ model_inputs = tokenizer([input_text], return_tensors="pt").to(device)
323
+
324
+ with torch.no_grad():
325
+ output = model.generate(
326
+ **model_inputs,
327
+ max_new_tokens=max_tokens,
328
+ do_sample=True,
329
+ top_p=top_p,
330
+ temperature=temperature,
331
+ num_return_sequences=1,
332
+ pad_token_id=tokenizer.eos_token_id
333
+ )
334
+
335
+ return tokenizer.decode(output[0], skip_special_tokens=True)
336
+
337
+ # πŸ›  Function to Clean AI Output
338
+ def post_process(text):
339
+ cleaned = re.sub(r'ζˆ₯+', '', text) # Remove unwanted symbols
340
+ lines = cleaned.splitlines()
341
+ unique_lines = list(dict.fromkeys([line.strip() for line in lines if line.strip()]))
342
+ return "\n".join(unique_lines)
343
+
344
+ # πŸ›  Function to Handle RAG with IBM Granite & Streamlit
345
+ def granite_simple(prompt, file):
346
+ file_context = read_files(file) if file else ""
347
+
348
+ system_message = "You are IBM Granite, a legal AI assistant specializing in contract analysis."
349
+
350
+ messages = format_prompt(system_message, prompt, file_context)
351
+ input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
352
+
353
+ response = generate_response(input_text)
354
+ return post_process(response)
355
+
356
+ # πŸ”Ή Streamlit UI
357
+ def main():
358
+ st.set_page_config(page_title="Contract Analysis AI", page_icon="πŸ“œ")
359
+
360
+ st.title("πŸ“œ AI-Powered Contract Analysis Tool")
361
+ st.write("Upload a contract document (PDF) for a detailed AI-driven legal and technical analysis.")
362
+
363
+ # πŸ”Ή Sidebar Settings
364
+ with st.sidebar:
365
+ st.header("βš™οΈ Settings")
366
+ max_tokens = st.slider("Max Tokens", 50, 1000, 250, 50)
367
+ top_p = st.slider("Top P (sampling)", 0.1, 1.0, 0.9, 0.1)
368
+ temperature = st.slider("Temperature (creativity)", 0.1, 1.0, 0.7, 0.1)
369
+
370
+ # πŸ”Ή File Upload Section
371
+ uploaded_file = st.file_uploader("πŸ“‚ Upload a contract document (PDF)", type="pdf")
372
+
373
+ if uploaded_file:
374
+ st.success("βœ… File uploaded successfully!")
375
+ st.write("Click the button below to analyze the contract.")
376
+
377
+ # Force button to always render
378
+ st.markdown('<style>div.stButton > button {display: block; width: 100%;}</style>', unsafe_allow_html=True)
379
+
380
+ if st.button("πŸ” Analyze Document"):
381
+ with st.spinner("Analyzing contract document... ⏳"):
382
+ final_answer = granite_simple(
383
+ "Perform a detailed technical analysis of the attached contract document, highlighting potential risks, legal pitfalls, compliance issues, and areas where contractual terms may lead to future disputes or operational challenges.",
384
+ uploaded_file
385
+ )
386
+
387
+ # πŸ”Ή Display Analysis Result
388
+ st.subheader("πŸ“‘ Analysis Result")
389
+ st.write(final_answer)
390
+
391
+ # πŸ”₯ Run Streamlit App
392
+ if __name__ == '__main__':
393
+ main()
394
+
395
+