Spaces:
Sleeping
Sleeping
convert2docx
Browse files
app.py
CHANGED
|
@@ -1,72 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import uuid
|
| 3 |
import logging
|
|
|
|
| 4 |
import requests
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
-
|
| 7 |
from langdetect import detect
|
| 8 |
-
from
|
| 9 |
-
from multilingual_pdf2text.models.document_model.document import Document
|
| 10 |
-
|
| 11 |
-
import pdfplumber
|
| 12 |
-
import docx2txt
|
| 13 |
-
import shutil
|
| 14 |
-
import subprocess
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
tess_path = shutil.which("tesseract")
|
| 19 |
-
if tess_path:
|
| 20 |
-
print(f"✅ Tesseract found at: {tess_path}")
|
| 21 |
-
try:
|
| 22 |
-
version = subprocess.check_output(["tesseract", "--version"]).decode()
|
| 23 |
-
print(f"🧠 Tesseract version:\n{version}")
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print(f"⚠️ Tesseract found but failed to run: {e}")
|
| 26 |
-
else:
|
| 27 |
-
print("❌ Tesseract not found. Arabic PDF extraction will likely fail.")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# --- Config ---
|
| 31 |
-
FLOWISE_URL = "https://siraflowappl.happywave-0e1819cd.uaenorth.azurecontainerapps.io/api/v1/prediction/0b8a2f94-f83f-4303-9007-abe55420e171"
|
| 32 |
-
#5f2aa074-ae2e-4b06-a4a2-6f8dccbe59bb" Contract.Auditor.2
|
| 33 |
-
COUNTRIES = ["Jordan", "Germany", "UAE", "Turkey","USA"]
|
| 34 |
-
LANGUAGES = ["Arabic","English"]
|
| 35 |
|
| 36 |
logging.basicConfig(level=logging.INFO)
|
| 37 |
|
| 38 |
-
# --- File Text Extraction ---
|
| 39 |
def extract_text_from_file(file_path: str) -> str:
|
| 40 |
ext = os.path.splitext(file_path)[1].lower()
|
| 41 |
|
| 42 |
try:
|
| 43 |
if ext == ".pdf":
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if lang == "ar" or not sample:
|
| 53 |
-
arabic_doc = Document(document_path=file_path, language="ara")
|
| 54 |
-
pdf2text = PDF2Text(document=arabic_doc)
|
| 55 |
-
extracted = pdf2text.extract()
|
| 56 |
-
|
| 57 |
-
if isinstance(extracted, list):
|
| 58 |
-
if isinstance(extracted[0], dict):
|
| 59 |
-
return "\n".join(item.get("text", "") for item in extracted)
|
| 60 |
-
elif isinstance(extracted[0], str):
|
| 61 |
-
return "\n".join(extracted)
|
| 62 |
-
else:
|
| 63 |
-
return "[Error: Unexpected Arabic extractor format.]"
|
| 64 |
-
elif isinstance(extracted, str):
|
| 65 |
-
return extracted
|
| 66 |
-
else:
|
| 67 |
-
return "[Error: Unrecognized Arabic extraction output.]"
|
| 68 |
-
|
| 69 |
-
return extracted or "[Warning: No readable text found in PDF.]"
|
| 70 |
|
| 71 |
elif ext == ".docx":
|
| 72 |
return docx2txt.process(file_path)
|
|
@@ -82,7 +43,6 @@ def extract_text_from_file(file_path: str) -> str:
|
|
| 82 |
logging.exception("Failed to extract text.")
|
| 83 |
return f"[Error reading file: {e}]"
|
| 84 |
|
| 85 |
-
# --- Flowise Query ---
|
| 86 |
def send_to_flowise(file, country: str, language: str) -> str:
|
| 87 |
if not file or not country or not language:
|
| 88 |
return "❗ Please upload a contract, select a country, and choose a response language."
|
|
@@ -92,11 +52,11 @@ def send_to_flowise(file, country: str, language: str) -> str:
|
|
| 92 |
if not contract_text:
|
| 93 |
return "⚠️ No content extracted from the uploaded file."
|
| 94 |
|
| 95 |
-
prompt = f"""Please audit the following employment contract in light of {country} labor law.
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
<CONTRACT START>
|
| 101 |
{contract_text}
|
| 102 |
<CONTRACT END>"""
|
|
@@ -140,7 +100,6 @@ with gr.Blocks(title="Contract Auditor") as demo:
|
|
| 140 |
inputs=[file_input, country_dropdown, language_dropdown],
|
| 141 |
outputs=output
|
| 142 |
)
|
| 143 |
-
|
| 144 |
if __name__ == "__main__":
|
| 145 |
demo.launch(
|
| 146 |
server_name="0.0.0.0",
|
|
|
|
| 1 |
+
|
| 2 |
+
FLOWISE_URL = "https://siraflowappl.happywave-0e1819cd.uaenorth.azurecontainerapps.io/api/v1/prediction/0b8a2f94-f83f-4303-9007-abe55420e171"
|
| 3 |
+
#5f2aa074-ae2e-4b06-a4a2-6f8dccbe59bb" Contract.Auditor.2
|
| 4 |
import os
|
|
|
|
| 5 |
import logging
|
| 6 |
+
import uuid
|
| 7 |
import requests
|
| 8 |
+
import docx2txt
|
| 9 |
import gradio as gr
|
|
|
|
| 10 |
from langdetect import detect
|
| 11 |
+
from pdf2docx import Converter # <-- new library for PDF to DOCX
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
COUNTRIES = ["USA", "Germany", "UAE", "Turkey", "Jordan"]
|
| 14 |
+
LANGUAGES = ["English", "Arabic"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
| 17 |
|
|
|
|
| 18 |
def extract_text_from_file(file_path: str) -> str:
|
| 19 |
ext = os.path.splitext(file_path)[1].lower()
|
| 20 |
|
| 21 |
try:
|
| 22 |
if ext == ".pdf":
|
| 23 |
+
# --- Step 1: Convert PDF to DOCX ---
|
| 24 |
+
temp_docx_path = file_path.replace(".pdf", "_converted.docx")
|
| 25 |
+
cv = Converter(file_path)
|
| 26 |
+
cv.convert(temp_docx_path, start=0, end=None)
|
| 27 |
+
cv.close()
|
| 28 |
+
|
| 29 |
+
# --- Step 2: Read the DOCX file ---
|
| 30 |
+
return docx2txt.process(temp_docx_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
elif ext == ".docx":
|
| 33 |
return docx2txt.process(file_path)
|
|
|
|
| 43 |
logging.exception("Failed to extract text.")
|
| 44 |
return f"[Error reading file: {e}]"
|
| 45 |
|
|
|
|
| 46 |
def send_to_flowise(file, country: str, language: str) -> str:
|
| 47 |
if not file or not country or not language:
|
| 48 |
return "❗ Please upload a contract, select a country, and choose a response language."
|
|
|
|
| 52 |
if not contract_text:
|
| 53 |
return "⚠️ No content extracted from the uploaded file."
|
| 54 |
|
| 55 |
+
prompt = f"""Please audit the following employment contract in light of {country} labor law.
|
| 56 |
+
Only refer to the provided law document (do not use external knowledge). Do not mention the law NO. in your response.
|
| 57 |
+
Is the contract compliant? Provide reasoning and highlight any issues, and suggest improvements.
|
| 58 |
+
Be precise and don't miss any details in the contract.
|
| 59 |
+
Please respond in {language}.
|
| 60 |
<CONTRACT START>
|
| 61 |
{contract_text}
|
| 62 |
<CONTRACT END>"""
|
|
|
|
| 100 |
inputs=[file_input, country_dropdown, language_dropdown],
|
| 101 |
outputs=output
|
| 102 |
)
|
|
|
|
| 103 |
if __name__ == "__main__":
|
| 104 |
demo.launch(
|
| 105 |
server_name="0.0.0.0",
|