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_text} """ 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 )