gnaw05 commited on
Commit
96d8c6a
·
verified ·
1 Parent(s): ce9dc81
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -14,7 +14,6 @@ st.set_page_config(page_title="📑 Contract Analyzer", layout="wide")
14
  # ========== FUNCTIONS ==========
15
 
16
  # Tải mô hình Hugging Face từ Hub
17
- @st.cache_resource
18
  def load_inference_client():
19
  return InferenceClient(repo_id="HuggingFaceH4/zephyr-7b-beta") # Mô hình Zephyr
20
 
@@ -23,19 +22,23 @@ inference_client = load_inference_client()
23
  def extract_text_from_pdf(uploaded_file):
24
  try:
25
  with pdfplumber.open(uploaded_file) as pdf:
26
- return "\n".join(page.extract_text() or "" for page in pdf.pages)
27
- except:
28
- try:
29
- reader = PyPDF4.PdfFileReader(uploaded_file)
30
- return "\n".join([reader.getPage(i).extractText() for i in range(reader.numPages)])
31
- except Exception as e:
32
- st.error(f"Error reading PDF: {e}")
33
- return ""
34
 
35
  def load_text(file):
36
  if not file:
37
  return ""
38
  try:
 
 
 
 
 
39
  ext = file.name.split('.')[-1].lower()
40
  if ext == 'txt':
41
  return StringIO(file.getvalue().decode("utf-8")).read()
@@ -79,7 +82,11 @@ def query_zephyr_model(text1, text2, question):
79
  prompt = f"Compare the following two contracts and answer the question:\nText 1: {text1}\nText 2: {text2}\nQuestion: {question}"
80
  try:
81
  result = inference_client(inputs=prompt)
82
- return result['generated_text']
 
 
 
 
83
  except Exception as e:
84
  st.error(f"Error querying the model: {e}")
85
  return None
@@ -129,16 +136,15 @@ def main():
129
  user_question = st.text_input("Enter your question about the contracts:")
130
 
131
  if user_question and st.button("Analyze Question"):
132
- col = st.columns(1)
133
-
134
- with col:
135
- st.subheader("Answer from Document")
136
- with st.spinner("Analyzing..."):
137
- try:
138
- pred = query_zephyr_model(text1, text2, user_question)
139
  st.success(pred)
140
- except Exception as e:
141
- st.error(f"Failed on Document: {e}")
 
 
142
 
143
  if __name__ == "__main__":
144
  main()
 
14
  # ========== FUNCTIONS ==========
15
 
16
  # Tải mô hình Hugging Face từ Hub
 
17
  def load_inference_client():
18
  return InferenceClient(repo_id="HuggingFaceH4/zephyr-7b-beta") # Mô hình Zephyr
19
 
 
22
  def extract_text_from_pdf(uploaded_file):
23
  try:
24
  with pdfplumber.open(uploaded_file) as pdf:
25
+ text = "\n".join(page.extract_text() or "" for page in pdf.pages)
26
+ if not text.strip():
27
+ raise ValueError("No extractable text found in the PDF")
28
+ return text
29
+ except Exception as e:
30
+ st.error(f"Error reading PDF: {e}")
31
+ return ""
 
32
 
33
  def load_text(file):
34
  if not file:
35
  return ""
36
  try:
37
+ # Check file size (e.g., limit to 10MB)
38
+ if file.size > 10 * 1024 * 1024: # 10MB
39
+ st.warning("File is too large. Please upload a smaller file.")
40
+ return ""
41
+
42
  ext = file.name.split('.')[-1].lower()
43
  if ext == 'txt':
44
  return StringIO(file.getvalue().decode("utf-8")).read()
 
82
  prompt = f"Compare the following two contracts and answer the question:\nText 1: {text1}\nText 2: {text2}\nQuestion: {question}"
83
  try:
84
  result = inference_client(inputs=prompt)
85
+ if 'generated_text' in result:
86
+ return result['generated_text']
87
+ else:
88
+ st.error("No generated text found in the response.")
89
+ return None
90
  except Exception as e:
91
  st.error(f"Error querying the model: {e}")
92
  return None
 
136
  user_question = st.text_input("Enter your question about the contracts:")
137
 
138
  if user_question and st.button("Analyze Question"):
139
+ with st.spinner("Analyzing..."):
140
+ try:
141
+ pred = query_zephyr_model(text1, text2, user_question)
142
+ if pred:
 
 
 
143
  st.success(pred)
144
+ else:
145
+ st.error("Failed to get a valid answer from the model.")
146
+ except Exception as e:
147
+ st.error(f"Failed on Document: {e}")
148
 
149
  if __name__ == "__main__":
150
  main()