Spaces:
Build error
Build error
up
Browse files
app.py
CHANGED
|
@@ -6,12 +6,20 @@ import docx2txt
|
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
import difflib
|
| 9 |
-
from
|
| 10 |
|
| 11 |
-
# ========== CONFIG ==========
|
| 12 |
st.set_page_config(page_title="📑 Contract Analyzer", layout="wide")
|
| 13 |
|
| 14 |
# ========== FUNCTIONS ==========
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def extract_text_from_pdf(uploaded_file):
|
| 16 |
try:
|
| 17 |
with pdfplumber.open(uploaded_file) as pdf:
|
|
@@ -66,7 +74,17 @@ def compute_similarity(text1, text2):
|
|
| 66 |
except:
|
| 67 |
return difflib.SequenceMatcher(None, text1, text2).ratio() * 100
|
| 68 |
|
| 69 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def main():
|
| 71 |
st.title("📑 Contract Analyzer")
|
| 72 |
st.markdown("Upload two contracts, compare them, and ask any question!")
|
|
@@ -116,8 +134,8 @@ def main():
|
|
| 116 |
st.subheader("Answer from Document 1")
|
| 117 |
with st.spinner("Analyzing..."):
|
| 118 |
try:
|
| 119 |
-
pred1 =
|
| 120 |
-
st.success(pred1
|
| 121 |
except Exception as e:
|
| 122 |
st.error(f"Failed on Document 1: {e}")
|
| 123 |
|
|
@@ -125,8 +143,8 @@ def main():
|
|
| 125 |
st.subheader("Answer from Document 2")
|
| 126 |
with st.spinner("Analyzing..."):
|
| 127 |
try:
|
| 128 |
-
pred2 =
|
| 129 |
-
st.success(pred2
|
| 130 |
except Exception as e:
|
| 131 |
st.error(f"Failed on Document 2: {e}")
|
| 132 |
|
|
|
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
import difflib
|
| 9 |
+
from huggingface_hub import InferenceApi # Import Hugging Face API
|
| 10 |
|
| 11 |
+
# ========== CONFIG ==========
|
| 12 |
st.set_page_config(page_title="📑 Contract Analyzer", layout="wide")
|
| 13 |
|
| 14 |
# ========== FUNCTIONS ==========
|
| 15 |
+
|
| 16 |
+
# Tải mô hình Hugging Face từ Hub
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_inference_api():
|
| 19 |
+
return InferenceApi(repo_id="HuggingFaceH4/zephyr-7b-beta") # Mô hình Zephyr
|
| 20 |
+
|
| 21 |
+
inference_api = load_inference_api()
|
| 22 |
+
|
| 23 |
def extract_text_from_pdf(uploaded_file):
|
| 24 |
try:
|
| 25 |
with pdfplumber.open(uploaded_file) as pdf:
|
|
|
|
| 74 |
except:
|
| 75 |
return difflib.SequenceMatcher(None, text1, text2).ratio() * 100
|
| 76 |
|
| 77 |
+
# Hàm truy vấn Zephyr từ Hugging Face
|
| 78 |
+
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_api(inputs=prompt)
|
| 82 |
+
return result['generated_text']
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.error(f"Error querying the model: {e}")
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
# ========== MAIN ==========
|
| 88 |
def main():
|
| 89 |
st.title("📑 Contract Analyzer")
|
| 90 |
st.markdown("Upload two contracts, compare them, and ask any question!")
|
|
|
|
| 134 |
st.subheader("Answer from Document 1")
|
| 135 |
with st.spinner("Analyzing..."):
|
| 136 |
try:
|
| 137 |
+
pred1 = query_zephyr_model(text1, text2, user_question)
|
| 138 |
+
st.success(pred1)
|
| 139 |
except Exception as e:
|
| 140 |
st.error(f"Failed on Document 1: {e}")
|
| 141 |
|
|
|
|
| 143 |
st.subheader("Answer from Document 2")
|
| 144 |
with st.spinner("Analyzing..."):
|
| 145 |
try:
|
| 146 |
+
pred2 = query_zephyr_model(text1, text2, user_question)
|
| 147 |
+
st.success(pred2)
|
| 148 |
except Exception as e:
|
| 149 |
st.error(f"Failed on Document 2: {e}")
|
| 150 |
|