Spaces:
Sleeping
Sleeping
File size: 4,171 Bytes
a5bce78 276708b a5bce78 74b416d a5bce78 74b416d a5bce78 74b416d a5bce78 cfefcb0 e8d85eb 74b416d a5bce78 74b416d 2b019a0 9ffa09a 2b019a0 9ffa09a 2b019a0 74b416d 4445a35 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
FLOWISE_URL = "https://siraflowappl.happywave-0e1819cd.uaenorth.azurecontainerapps.io/api/v1/prediction/5f2aa074-ae2e-4b06-a4a2-6f8dccbe59bb"
#0b8a2f94-f83f-4303-9007-abe55420e171" contract_review
#5f2aa074-ae2e-4b06-a4a2-6f8dccbe59bb" Contract.Auditor.2
import os
import logging
import uuid
import requests
import docx2txt
import gradio as gr
from langdetect import detect
from pdf2docx import Converter # <-- new library for PDF to DOCX
COUNTRIES = ["Jordan", "Germany", "UAE", "Turkey", "USA"]
LANGUAGES = ["Arabic", "English"]
logging.basicConfig(level=logging.INFO)
def extract_text_from_file(file_path: str) -> str:
ext = os.path.splitext(file_path)[1].lower()
try:
if ext == ".pdf":
# --- Step 1: Convert PDF to DOCX ---
temp_docx_path = file_path.replace(".pdf", "_converted.docx")
cv = Converter(file_path)
cv.convert(temp_docx_path, start=0, end=None)
cv.close()
# --- Step 2: Read the DOCX file ---
return docx2txt.process(temp_docx_path)
elif ext == ".docx":
return docx2txt.process(file_path)
elif ext == ".txt":
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
return f.read()
else:
return "[Unsupported file format.]"
except Exception as e:
logging.exception("Failed to extract text.")
return f"[Error reading file: {e}]"
def send_to_flowise(file, country: str, language: str) -> str:
if not file or not country or not language:
return "โ Please upload a contract, select a country, and choose a response language."
try:
contract_text = extract_text_from_file(file.name).strip()
if not contract_text:
return "โ ๏ธ No content extracted from the uploaded file."
prompt = f"""Please audit the following employment contract in light of {country} labor law.
Only refer to the provided law document (do not use external knowledge), but you don't have to mention the exact source of info while answering.
Is the contract compliant? Provide reasoning and highlight any issues, and suggest improvements.
Read line by line, be precise, and don't miss any details in the contract.
Look for numbers like: salary, working hours, annual vacations, and overtime, and mention the correct numbers by law.
If the salary in the contract is below or above the legal minimum wage stated in the provided law document, explicitly mention the exact legal minimum wage and how the contractโs salary compares to it.
Please respond in {language}.
<CONTRACT START>
{contract_text}
<CONTRACT END>"""
payload = {
"question": prompt,
"chatId": str(uuid.uuid4())
}
res = requests.post(FLOWISE_URL, json=payload, timeout=300)
logging.info("๐ Flowise response: %s", res.text)
if res.status_code != 200:
return f"โ Flowise error {res.status_code}: {res.text}"
return res.json().get("text", "โ ๏ธ Flowise returned no text.")
except Exception as e:
logging.exception("Flowise request failed.")
return f"โ Connection error: {e}"
# --- Gradio UI ---
with gr.Blocks(title="Contract Auditor") as demo:
gr.Markdown("## ๐ Contract Auditor")
with gr.Row():
country_dropdown = gr.Dropdown(choices=COUNTRIES, label="๐ Select Country", interactive=True)
language_dropdown = gr.Dropdown(choices=LANGUAGES, label="๐ฃ๏ธ Select Response Language", interactive=True)
file_input = gr.File(
label="๐ Upload a contract file",
file_types=[".pdf", ".docx", ".txt"],
type="filepath"
)
submit_btn = gr.Button("๐ค Audit Contract")
output = gr.Textbox(label="๐ง Audit Result", lines=12)
submit_btn.click(
fn=send_to_flowise,
inputs=[file_input, country_dropdown, language_dropdown],
outputs=output
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
)
|