TabasumDev commited on
Commit
1a19f21
Β·
verified Β·
1 Parent(s): 80f621f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -49
app.py CHANGED
@@ -2,28 +2,21 @@
2
  # import os
3
  # import re
4
  # import torch
5
- # from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
6
  # from PyPDF2 import PdfReader
7
  # from peft import get_peft_model, LoraConfig, TaskType
8
 
9
- # # βœ… Fix CUDA Memory Fragmentation
10
- # os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
11
 
12
- # # πŸ”Ή Load IBM Granite Model with 4-bit Quantization
13
  # MODEL_NAME = "ibm-granite/granite-3.1-2b-instruct"
14
- # quant_config = BitsAndBytesConfig(load_in_4bit=True) # Use 4-bit quantization
15
-
16
- # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
-
18
- # # βœ… Ensure model initialization correctly
19
- # torch.cuda.empty_cache() # Clear GPU memory before loading model
20
 
21
  # model = AutoModelForCausalLM.from_pretrained(
22
  # MODEL_NAME,
23
- # quantization_config=quant_config,
24
- # device_map="auto", # Auto-assign layers to available GPUs/CPUs
25
- # torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32 # Use FP16 if GPU is available
26
- # ).to(device) # Move model to correct device
27
 
28
  # tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
29
 
@@ -51,7 +44,6 @@
51
 
52
  # return file_context.strip()
53
 
54
- # # πŸ›  Function to Format AI Prompts
55
  # # πŸ›  Function to Format AI Prompts
56
  # def format_prompt(system_msg, user_msg, file_context=""):
57
  # if file_context:
@@ -60,10 +52,9 @@
60
  # {"role": "system", "content": system_msg},
61
  # {"role": "user", "content": user_msg}
62
  # ]
 
63
  # # πŸ›  Function to Generate AI Responses
64
  # def generate_response(input_text, max_tokens=1000, top_p=0.9, temperature=0.7):
65
- # torch.cuda.empty_cache() # βœ… Clear GPU memory before inference
66
-
67
  # model_inputs = tokenizer([input_text], return_tensors="pt").to(device)
68
 
69
  # with torch.no_grad():
@@ -125,17 +116,6 @@
125
  # # πŸ”Ή User Input for Analysis
126
  # user_prompt = "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."
127
 
128
- # # user_prompt = st.text_area(
129
- # # "πŸ“ Describe what you want to analyze:",
130
- # # "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."
131
- # # )
132
- # # with st.empty(): # This hides the text area
133
- # # user_prompt = st.text_area(
134
- # # "πŸ“ Describe what you want to analyze:",
135
- # # "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."
136
- # # )
137
-
138
-
139
  # if st.button("πŸ” Analyze Document"):
140
  # with st.spinner("Analyzing contract document... ⏳"):
141
  # final_answer = granite_simple(user_prompt, temp_file_path)
@@ -154,6 +134,7 @@
154
 
155
 
156
 
 
157
  import streamlit as st
158
  import os
159
  import re
@@ -162,16 +143,16 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
162
  from PyPDF2 import PdfReader
163
  from peft import get_peft_model, LoraConfig, TaskType
164
 
165
- # βœ… Force CPU execution for Streamlit Cloud
166
- device = torch.device("cpu")
167
 
168
- # πŸ”Ή Load IBM Granite Model (CPU-Compatible)
169
  MODEL_NAME = "ibm-granite/granite-3.1-2b-instruct"
170
 
171
  model = AutoModelForCausalLM.from_pretrained(
172
  MODEL_NAME,
173
- device_map="cpu", # Force CPU execution
174
- torch_dtype=torch.float32 # Use float32 since Streamlit runs on CPU
175
  )
176
 
177
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
@@ -188,7 +169,7 @@ lora_config = LoraConfig(
188
  model = get_peft_model(model, lora_config)
189
  model.eval()
190
 
191
- # πŸ›  Function to Read & Extract Text from PDFs
192
  def read_files(file):
193
  file_context = ""
194
  reader = PdfReader(file)
@@ -233,13 +214,11 @@ def post_process(text):
233
  unique_lines = list(dict.fromkeys([line.strip() for line in lines if line.strip()]))
234
  return "\n".join(unique_lines)
235
 
236
- # πŸ›  Function to Handle RAG with IBM Granite & Streamlit
237
- def granite_simple(prompt, file):
238
- file_context = read_files(file) if file else ""
239
-
240
  system_message = "You are IBM Granite, a legal AI assistant specializing in contract analysis."
241
 
242
- messages = format_prompt(system_message, prompt, file_context)
243
  input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
244
 
245
  response = generate_response(input_text)
@@ -259,30 +238,26 @@ def main():
259
  top_p = st.slider("Top P (sampling)", 0.1, 1.0, 0.9, 0.1)
260
  temperature = st.slider("Temperature (creativity)", 0.1, 1.0, 0.7, 0.1)
261
 
262
- # πŸ”Ή File Upload Section
263
  uploaded_file = st.file_uploader("πŸ“‚ Upload a contract document (PDF)", type="pdf")
264
 
265
  if uploaded_file is not None:
266
- temp_file_path = "temp_uploaded_contract.pdf"
267
- with open(temp_file_path, "wb") as f:
268
- f.write(uploaded_file.getbuffer())
269
-
270
  st.success("βœ… File uploaded successfully!")
271
 
 
 
 
272
  # πŸ”Ή User Input for Analysis
273
  user_prompt = "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."
274
 
275
  if st.button("πŸ” Analyze Document"):
276
  with st.spinner("Analyzing contract document... ⏳"):
277
- final_answer = granite_simple(user_prompt, temp_file_path)
278
 
279
  # πŸ”Ή Display Analysis Result
280
  st.subheader("πŸ“‘ Analysis Result")
281
  st.write(final_answer)
282
 
283
- # πŸ”Ή Remove Temporary File
284
- os.remove(temp_file_path)
285
-
286
  # πŸ”₯ Run Streamlit App
287
  if __name__ == '__main__':
288
- main()
 
2
  # import os
3
  # import re
4
  # import torch
5
+ # from transformers import AutoModelForCausalLM, AutoTokenizer
6
  # from PyPDF2 import PdfReader
7
  # from peft import get_peft_model, LoraConfig, TaskType
8
 
9
+ # # βœ… Force CPU execution for Streamlit Cloud
10
+ # device = torch.device("cpu")
11
 
12
+ # # πŸ”Ή Load IBM Granite Model (CPU-Compatible)
13
  # MODEL_NAME = "ibm-granite/granite-3.1-2b-instruct"
 
 
 
 
 
 
14
 
15
  # model = AutoModelForCausalLM.from_pretrained(
16
  # MODEL_NAME,
17
+ # device_map="cpu", # Force CPU execution
18
+ # torch_dtype=torch.float32 # Use float32 since Streamlit runs on CPU
19
+ # )
 
20
 
21
  # tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
22
 
 
44
 
45
  # return file_context.strip()
46
 
 
47
  # # πŸ›  Function to Format AI Prompts
48
  # def format_prompt(system_msg, user_msg, file_context=""):
49
  # if file_context:
 
52
  # {"role": "system", "content": system_msg},
53
  # {"role": "user", "content": user_msg}
54
  # ]
55
+
56
  # # πŸ›  Function to Generate AI Responses
57
  # def generate_response(input_text, max_tokens=1000, top_p=0.9, temperature=0.7):
 
 
58
  # model_inputs = tokenizer([input_text], return_tensors="pt").to(device)
59
 
60
  # with torch.no_grad():
 
116
  # # πŸ”Ή User Input for Analysis
117
  # user_prompt = "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."
118
 
 
 
 
 
 
 
 
 
 
 
 
119
  # if st.button("πŸ” Analyze Document"):
120
  # with st.spinner("Analyzing contract document... ⏳"):
121
  # final_answer = granite_simple(user_prompt, temp_file_path)
 
134
 
135
 
136
 
137
+
138
  import streamlit as st
139
  import os
140
  import re
 
143
  from PyPDF2 import PdfReader
144
  from peft import get_peft_model, LoraConfig, TaskType
145
 
146
+ # βœ… Auto-detect GPU for Hugging Face Spaces
147
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
148
 
149
+ # πŸ”Ή Load IBM Granite Model (CPU/GPU Compatible)
150
  MODEL_NAME = "ibm-granite/granite-3.1-2b-instruct"
151
 
152
  model = AutoModelForCausalLM.from_pretrained(
153
  MODEL_NAME,
154
+ device_map="auto", # Auto-detect GPU if available
155
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
156
  )
157
 
158
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
169
  model = get_peft_model(model, lora_config)
170
  model.eval()
171
 
172
+ # πŸ›  Function to Read & Extract Text from PDFs (No Temp File Needed)
173
  def read_files(file):
174
  file_context = ""
175
  reader = PdfReader(file)
 
214
  unique_lines = list(dict.fromkeys([line.strip() for line in lines if line.strip()]))
215
  return "\n".join(unique_lines)
216
 
217
+ # πŸ›  Function to Handle AI Analysis (No Temp File)
218
+ def granite_simple(prompt, file_content):
 
 
219
  system_message = "You are IBM Granite, a legal AI assistant specializing in contract analysis."
220
 
221
+ messages = format_prompt(system_message, prompt, file_content)
222
  input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
223
 
224
  response = generate_response(input_text)
 
238
  top_p = st.slider("Top P (sampling)", 0.1, 1.0, 0.9, 0.1)
239
  temperature = st.slider("Temperature (creativity)", 0.1, 1.0, 0.7, 0.1)
240
 
241
+ # πŸ”Ή File Upload Section (No Temp File)
242
  uploaded_file = st.file_uploader("πŸ“‚ Upload a contract document (PDF)", type="pdf")
243
 
244
  if uploaded_file is not None:
 
 
 
 
245
  st.success("βœ… File uploaded successfully!")
246
 
247
+ # πŸ”Ή Read PDF Content (No Temp File)
248
+ file_content = read_files(uploaded_file)
249
+
250
  # πŸ”Ή User Input for Analysis
251
  user_prompt = "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."
252
 
253
  if st.button("πŸ” Analyze Document"):
254
  with st.spinner("Analyzing contract document... ⏳"):
255
+ final_answer = granite_simple(user_prompt, file_content)
256
 
257
  # πŸ”Ή Display Analysis Result
258
  st.subheader("πŸ“‘ Analysis Result")
259
  st.write(final_answer)
260
 
 
 
 
261
  # πŸ”₯ Run Streamlit App
262
  if __name__ == '__main__':
263
+ main()