CoolFace
Apppublic

HernanGro/Revision

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
app.py72 linesDownload Raw Back to root
1import gradio as gr2import requests3import PyPDF24import io5 6# -----------------------------7# Helper: PDF → text8# -----------------------------9def extract_text_from_pdf(file_obj):10    reader = PyPDF2.PdfReader(file_obj)11    text = ""12    for page in reader.pages:13        text += page.extract_text() or ""14    return text.strip()15 16# -----------------------------17# Main processing function18# -----------------------------19def process_pdf(pdf_file, api_url, api_key):20    if pdf_file is None:21        return "No PDF uploaded."22 23    # Extract text24    try:25        text = extract_text_from_pdf(pdf_file)26    except Exception as e:27        return f"Error reading PDF: {e}"28 29    if not api_url:30        return "Missing API URL."31 32    # Prepare request to external AI33    payload = {34        "text": text,35        "instruction": "Extract regulatory study data from this document and return structured JSON."36    }37 38    headers = {}39    if api_key:40        headers["Authorization"] = f"Bearer {api_key}"41 42    try:43        response = requests.post(api_url, json=payload, headers=headers, timeout=60)44        if response.status_code != 200:45            return f"API error: {response.status_code} - {response.text}"46        return response.json()47    except Exception as e:48        return f"Error contacting external AI: {e}"49 50# -----------------------------51# Gradio UI52# -----------------------------53with gr.Blocks() as demo:54    gr.Markdown("# 🧪 Regulatory Analysis App (Base Version)\nUpload a PDF and send it to an external AI for extraction.")55 56    with gr.Row():57        pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])58    59    api_url = gr.Textbox(label="External AI API URL", placeholder="https://your-endpoint.com/extract")60    api_key = gr.Textbox(label="API Key (optional)", type="password")61 62    run_button = gr.Button("Process")63 64    output = gr.JSON(label="AI Output")65 66    run_button.click(67        fn=process_pdf,68        inputs=[pdf_input, api_url, api_key],69        outputs=output70    )71 72demo.launch()