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