amritn8 commited on
Commit
821e62a
·
verified ·
1 Parent(s): 5d928e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -54
app.py CHANGED
@@ -11,40 +11,56 @@ import psutil
11
  from pathlib import Path
12
  import torch
13
 
14
- st.set_page_config(
15
- page_title="LexPilot",
16
- page_icon="🛩️",
17
- layout="wide",
18
- initial_sidebar_state="expanded"
19
- )
20
 
21
- # Cache directory setup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def setup_environment():
23
- cache_dir = Path("/app/models")
24
- cache_dir.mkdir(exist_ok=True, parents=True)
 
 
 
25
  return cache_dir
26
 
27
  cache_dir = setup_environment()
28
 
29
- # Load models with caching
30
  @st.cache_resource(ttl=3600)
31
  def load_models():
32
  try:
33
- with st.spinner("🔄 Loading AI models (this may take 1-2 minutes)..."):
34
- return {
35
- 'qa': pipeline(
36
- "question-answering",
37
- model="distilbert-base-cased-distilled-squad",
38
- device=-1
39
- ),
40
- 'summarizer': pipeline(
41
- "summarization",
42
- model="sshleifer/distilbart-cnn-12-6",
43
- device=-1
44
- )
45
- }
46
  except Exception as e:
47
- st.error(f"Failed to load models: {str(e)}")
48
  st.stop()
49
 
50
  models = load_models()
@@ -60,7 +76,7 @@ def extract_text(file):
60
  doc = docx.Document(file)
61
  return "\n".join(para.text for para in doc.paragraphs if para.text)
62
  except Exception as e:
63
- st.error(f"⚠️ Error processing document: {str(e)}")
64
  return ""
65
 
66
  def generate_summary(text, max_length=150):
@@ -74,7 +90,7 @@ def generate_summary(text, max_length=150):
74
  for chunk in chunks:
75
  result = models['summarizer'](
76
  chunk,
77
- max_length=max(max_length // len(chunks), 30),
78
  min_length=30,
79
  do_sample=False
80
  )
@@ -82,18 +98,13 @@ def generate_summary(text, max_length=150):
82
  return " ".join(summaries)
83
  return models['summarizer'](text, max_length=max_length)[0]['summary_text']
84
  except Exception as e:
85
- st.error(f"Summarization failed: {str(e)}")
86
  return ""
87
 
88
- # UI
89
- st.title("🛩️ LexPilot")
90
- st.markdown(
91
- "LexPilot™ ingests text, PDF, and Word files to instantly analyze contracts. "
92
- "It delivers concise summaries and lets you ask targeted questions—giving fast, precise insights to speed up legal and procurement reviews."
93
- )
94
 
95
- with st.sidebar:
96
- st.header("Upload Document")
97
  uploaded_file = st.file_uploader("Choose PDF/DOCX", type=["pdf", "docx"])
98
  manual_text = st.text_area("Or paste text here:", height=150)
99
  context = extract_text(uploaded_file) if uploaded_file else manual_text
@@ -111,14 +122,12 @@ with tab1:
111
  question=question,
112
  context=context[:100000]
113
  )
114
- st.success(f"Answered in {time.time() - start_time:.1f}s")
115
  st.markdown(f"**Answer:** {result['answer']}")
116
  st.progress(result['score'])
117
  st.caption(f"Confidence: {result['score']:.0%}")
118
  except Exception as e:
119
- st.error(f"Question answering failed: {str(e)}")
120
- else:
121
- st.info("Upload a document or paste text to enable question answering.")
122
 
123
  with tab2:
124
  if context and len(context.strip()) > 0:
@@ -129,20 +138,10 @@ with tab2:
129
  start_time = time.time()
130
  summary = generate_summary(context, length)
131
  if summary:
132
- st.success(f"Generated in {time.time() - start_time:.1f}s")
133
  st.markdown(f"**Summary:**\n\n{summary}")
134
- else:
135
- st.info("Upload a document or paste text to enable summarization.")
136
 
137
- with st.expander("⚙️ System Status"):
138
- try:
139
- device_status = 'GPU ✅' if torch.cuda.is_available() else 'CPU ⚠️'
140
- except:
141
- device_status = 'CPU (torch not configured)'
142
- st.code(f"""
143
- Models loaded: {', '.join(models.keys())}
144
- Device: {device_status}
145
- Memory usage: {psutil.virtual_memory().percent}%
146
- CPU usage: {psutil.cpu_percent()}%
147
- Cache location: {cache_dir}
148
- """)
 
11
  from pathlib import Path
12
  import torch
13
 
14
+ # Page config with wide layout
15
+ st.set_page_config(page_title="LexPilot", layout="wide")
 
 
 
 
16
 
17
+ # Sidebar with project info
18
+ with st.sidebar:
19
+ st.title("LexPilot™")
20
+ st.markdown(
21
+ """
22
+ LexPilot™ ingests text, PDF, and Word files to instantly analyze contracts.
23
+ It delivers concise summaries and lets you ask targeted questions—
24
+ giving fast, precise insights to speed up legal and procurement reviews.
25
+ """
26
+ )
27
+ st.markdown("---")
28
+ st.write("### System Status")
29
+ try:
30
+ device_status = 'GPU ✅' if torch.cuda.is_available() else 'CPU ⚠️'
31
+ except:
32
+ device_status = 'CPU (torch not configured)'
33
+ st.text(f"Device: {device_status}")
34
+ st.text(f"Memory: {psutil.virtual_memory().percent}% used")
35
+ st.text(f"CPU: {psutil.cpu_percent()}% used")
36
+
37
+ # Setup cache directory for models
38
  def setup_environment():
39
+ cache_dir = Path(".cache/models")
40
+ try:
41
+ cache_dir.mkdir(exist_ok=True, parents=True)
42
+ except Exception as e:
43
+ st.error(f"Failed to create cache directory: {e}")
44
  return cache_dir
45
 
46
  cache_dir = setup_environment()
47
 
 
48
  @st.cache_resource(ttl=3600)
49
  def load_models():
50
  try:
51
+ qa_model = pipeline(
52
+ "question-answering",
53
+ model="distilbert-base-cased-distilled-squad",
54
+ device=-1
55
+ )
56
+ summarizer_model = pipeline(
57
+ "summarization",
58
+ model="sshleifer/distilbart-cnn-6-6",
59
+ device=-1
60
+ )
61
+ return {'qa': qa_model, 'summarizer': summarizer_model}
 
 
62
  except Exception as e:
63
+ st.error(f"Failed to load models: {e}")
64
  st.stop()
65
 
66
  models = load_models()
 
76
  doc = docx.Document(file)
77
  return "\n".join(para.text for para in doc.paragraphs if para.text)
78
  except Exception as e:
79
+ st.error(f"Error processing document: {e}")
80
  return ""
81
 
82
  def generate_summary(text, max_length=150):
 
90
  for chunk in chunks:
91
  result = models['summarizer'](
92
  chunk,
93
+ max_length=max(max_length//len(chunks), 30),
94
  min_length=30,
95
  do_sample=False
96
  )
 
98
  return " ".join(summaries)
99
  return models['summarizer'](text, max_length=max_length)[0]['summary_text']
100
  except Exception as e:
101
+ st.error(f"Summarization failed: {e}")
102
  return ""
103
 
104
+ # Main UI title
105
+ st.title("📄 LexPilot")
 
 
 
 
106
 
107
+ with st.expander("📤 Upload Document", expanded=True):
 
108
  uploaded_file = st.file_uploader("Choose PDF/DOCX", type=["pdf", "docx"])
109
  manual_text = st.text_area("Or paste text here:", height=150)
110
  context = extract_text(uploaded_file) if uploaded_file else manual_text
 
122
  question=question,
123
  context=context[:100000]
124
  )
125
+ st.success(f"Answered in {time.time()-start_time:.1f}s")
126
  st.markdown(f"**Answer:** {result['answer']}")
127
  st.progress(result['score'])
128
  st.caption(f"Confidence: {result['score']:.0%}")
129
  except Exception as e:
130
+ st.error(f"Question answering failed: {e}")
 
 
131
 
132
  with tab2:
133
  if context and len(context.strip()) > 0:
 
138
  start_time = time.time()
139
  summary = generate_summary(context, length)
140
  if summary:
141
+ st.success(f"Generated in {time.time()-start_time:.1f}s")
142
  st.markdown(f"**Summary:**\n\n{summary}")
 
 
143
 
144
+ # Show cache dir path in sidebar (optional)
145
+ with st.sidebar:
146
+ st.markdown("---")
147
+ st.write(f"Cache directory: {cache_dir}")