diff --git a/.gitignore b/.gitignore index 7fa2022..e81a1db 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea venv .venv -*.db \ No newline at end of file +*.db +src/inputs/*.pdf \ No newline at end of file diff --git a/api/db/repositories.py b/api/db/repositories.py index 6608718..7686510 100644 --- a/api/db/repositories.py +++ b/api/db/repositories.py @@ -16,4 +16,10 @@ def create_form(session: Session, form: FormSubmission) -> FormSubmission: session.add(form) session.commit() session.refresh(form) - return form \ No newline at end of file + return form + +def get_all_templates(session: Session, limit: int = 100, offset: int = 0) -> list[Template]: + return session.exec(select(Template).offset(offset).limit(limit)).all() + +def get_form(session: Session, submission_id: int) -> FormSubmission | None: + return session.get(FormSubmission, submission_id) \ No newline at end of file diff --git a/api/main.py b/api/main.py index d0b8c79..0a7d8e7 100644 --- a/api/main.py +++ b/api/main.py @@ -1,7 +1,25 @@ -from fastapi import FastAPI +from fastapi import FastAPI, Request +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import JSONResponse from api.routes import templates, forms +from api.errors.base import AppError +from typing import Union app = FastAPI() +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_methods=["*"], + allow_headers=["*"], +) + +@app.exception_handler(AppError) +def app_error_handler(request: Request, exc: AppError): + return JSONResponse( + status_code=exc.status_code, + content={"detail": exc.message} + ) + app.include_router(templates.router) app.include_router(forms.router) \ No newline at end of file diff --git a/api/routes/forms.py b/api/routes/forms.py index f3430ed..3491d4e 100644 --- a/api/routes/forms.py +++ b/api/routes/forms.py @@ -1,25 +1,82 @@ +import os from fastapi import APIRouter, Depends +from fastapi.responses import FileResponse from sqlmodel import Session from api.deps import get_db from api.schemas.forms import FormFill, FormFillResponse -from api.db.repositories import create_form, get_template +from api.db.repositories import create_form, get_template, get_form from api.db.models import FormSubmission from api.errors.base import AppError from src.controller import Controller router = APIRouter(prefix="/forms", tags=["forms"]) + @router.post("/fill", response_model=FormFillResponse) def fill_form(form: FormFill, db: Session = Depends(get_db)): - if not get_template(db, form.template_id): + # Single DB query (fixes issue #149 - redundant query) + template = get_template(db, form.template_id) + if not template: raise AppError("Template not found", status_code=404) - fetched_template = get_template(db, form.template_id) + try: + controller = Controller() + # FileManipulator.fill_form expects fields as a list of key strings + fields_list = list(template.fields.keys()) if isinstance(template.fields, dict) else template.fields + path = controller.fill_form( + user_input=form.input_text, + fields=fields_list, + pdf_form_path=template.pdf_path + ) + except ConnectionError: + raise AppError( + "Could not connect to Ollama. Make sure ollama serve is running.", + status_code=503 + ) + except Exception as e: + raise AppError(f"PDF filling failed: {str(e)}", status_code=500) + + # Guard: controller returned None instead of a file path + if not path: + raise AppError( + "PDF generation failed — no output file was produced. " + "Check that the PDF template is a valid fillable form and Ollama is running.", + status_code=500 + ) - controller = Controller() - path = controller.fill_form(user_input=form.input_text, fields=fetched_template.fields, pdf_form_path=fetched_template.pdf_path) + if not os.path.exists(path): + raise AppError( + f"PDF was generated but file not found at: {path}", + status_code=500 + ) - submission = FormSubmission(**form.model_dump(), output_pdf_path=path) + submission = FormSubmission( + **form.model_dump(), + output_pdf_path=path + ) return create_form(db, submission) +@router.get("/{submission_id}", response_model=FormFillResponse) +def get_submission(submission_id: int, db: Session = Depends(get_db)): + submission = get_form(db, submission_id) + if not submission: + raise AppError("Submission not found", status_code=404) + return submission + + +@router.get("/download/{submission_id}") +def download_filled_pdf(submission_id: int, db: Session = Depends(get_db)): + submission = get_form(db, submission_id) + if not submission: + raise AppError("Submission not found", status_code=404) + + file_path = submission.output_pdf_path + if not os.path.exists(file_path): + raise AppError("PDF file not found on server", status_code=404) + + return FileResponse( + path=file_path, + media_type="application/pdf", + filename=os.path.basename(file_path) + ) \ No newline at end of file diff --git a/api/routes/templates.py b/api/routes/templates.py index 5c2281b..9419ae6 100644 --- a/api/routes/templates.py +++ b/api/routes/templates.py @@ -1,16 +1,89 @@ -from fastapi import APIRouter, Depends +import os +import shutil +import uuid +from fastapi import APIRouter, Depends, UploadFile, File, Form from sqlmodel import Session from api.deps import get_db -from api.schemas.templates import TemplateCreate, TemplateResponse -from api.db.repositories import create_template +from api.schemas.templates import TemplateResponse +from api.db.repositories import create_template, get_all_templates from api.db.models import Template -from src.controller import Controller +from api.errors.base import AppError router = APIRouter(prefix="/templates", tags=["templates"]) +# Save directly into src/inputs/ — stable location, won't get wiped +TEMPLATES_DIR = os.path.join("src", "inputs") +os.makedirs(TEMPLATES_DIR, exist_ok=True) + + @router.post("/create", response_model=TemplateResponse) -def create(template: TemplateCreate, db: Session = Depends(get_db)): - controller = Controller() - template_path = controller.create_template(template.pdf_path) - tpl = Template(**template.model_dump(exclude={"pdf_path"}), pdf_path=template_path) - return create_template(db, tpl) \ No newline at end of file +async def create( + name: str = Form(...), + file: UploadFile = File(...), + db: Session = Depends(get_db) +): + # Validate PDF + if not file.filename.endswith(".pdf"): + raise AppError("Only PDF files are allowed", status_code=400) + + # Save uploaded file with unique name into src/inputs/ + unique_name = f"{uuid.uuid4().hex}_{file.filename}" + save_path = os.path.join(TEMPLATES_DIR, unique_name) + + with open(save_path, "wb") as f: + shutil.copyfileobj(file.file, f) + + # Extract fields using commonforms + pypdf + # Store as simple list of field name strings — what Filler expects + try: + from commonforms import prepare_form + from pypdf import PdfReader + + # Read real field names directly from original PDF + # Use /T (internal name) as both key and label + # Real names like "JobTitle", "Phone Number" are already human-readable + reader = PdfReader(save_path) + raw_fields = reader.get_fields() or {} + + fields = {} + for internal_name, field_data in raw_fields.items(): + # Use /TU tooltip if available, otherwise prettify /T name + label = None + if isinstance(field_data, dict): + label = field_data.get("/TU") + if not label: + # Prettify: "JobTitle" → "Job Title", "DATE7_af_date" → "Date" + import re + label = re.sub(r'([a-z])([A-Z])', r'\1 \2', internal_name) + label = re.sub(r'_af_.*$', '', label) # strip "_af_date" suffix + label = label.replace('_', ' ').strip().title() + fields[internal_name] = label + + except Exception as e: + print(f"Field extraction failed: {e}") + fields = [] + + # Save to DB + tpl = Template(name=name, pdf_path=save_path, fields=fields) + return create_template(db, tpl) + + +@router.get("", response_model=list[TemplateResponse]) +def list_templates( + limit: int = 100, + offset: int = 0, + db: Session = Depends(get_db) +): + return get_all_templates(db, limit=limit, offset=offset) + + +@router.get("/{template_id}", response_model=TemplateResponse) +def get_template_by_id( + template_id: int, + db: Session = Depends(get_db) +): + from api.db.repositories import get_template + tpl = get_template(db, template_id) + if not tpl: + raise AppError("Template not found", status_code=404) + return tpl \ No newline at end of file diff --git a/docs/SETUP.md b/docs/SETUP.md new file mode 100644 index 0000000..cf47642 --- /dev/null +++ b/docs/SETUP.md @@ -0,0 +1,228 @@ +# 🔥 FireForm — Setup & Usage Guide + +This guide covers how to install, run, and use FireForm locally on Windows, Linux, and macOS. + +--- + +## 📋 Prerequisites + +| Tool | Version | Purpose | +|------|---------|---------| +| Python | 3.11+ | Backend runtime | +| Ollama | 0.17.7+ | Local LLM server | +| Mistral 7B | latest | AI extraction model | +| Git | any | Clone the repository | + +--- + +## 🪟 Windows + +### 1. Clone the repository +```cmd +git clone https://github.com/fireform-core/FireForm.git +cd FireForm +``` + +### 2. Create and activate virtual environment +```cmd +python -m venv venv +venv\Scripts\activate +``` + +### 3. Install dependencies +```cmd +pip install -r requirements.txt +``` + +### 4. Install and start Ollama +Download Ollama from https://ollama.com/download/windows + +Then pull the Mistral model: +```cmd +ollama pull mistral +ollama serve +``` + +> Ollama runs on `http://localhost:11434` by default. Keep this terminal open. + +### 5. Initialize the database +```cmd +python -m api.db.init_db +``` + +### 6. Start the API server +```cmd +uvicorn api.main:app --reload +``` + +API is now running at `http://127.0.0.1:8000` + +### 7. Start the frontend +Open a new terminal: +```cmd +cd frontend +python -m http.server 3000 +``` + +Open `http://localhost:3000` in your browser. + +--- + + +## 🍎 macOS + +### 1. Clone and enter the repository +```bash +git clone https://github.com/fireform-core/FireForm.git +cd FireForm +``` + +### 2. Create and activate virtual environment +```bash +python3 -m venv venv +source venv/bin/activate +``` + +### 3. Install dependencies +```bash +pip install -r requirements.txt +``` + +### 4. Install and start Ollama +Download from https://ollama.com/download/mac or: +```bash +brew install ollama +ollama pull mistral +ollama serve & +``` + +### 5. Initialize the database +```bash +python -m api.db.init_db +``` + +### 6. Start the API server +```bash +uvicorn api.main:app --reload +``` + +### 7. Start the frontend +```bash +cd frontend +python3 -m http.server 3000 +``` + +--- + +## 🖥️ Using the Frontend + +Once everything is running, open `http://localhost:3000` in your browser. + +### Step 1 — Upload a PDF template +- Click **"Choose File"** and select any fillable PDF form +- Enter a name for the template +- Click **"Upload Template"** + +FireForm will automatically extract all form field names and their human-readable labels. + +### Step 2 — Fill the form +- Select your uploaded template from the dropdown +- In the text box, describe the incident or enter the information in natural language: + +``` +Employee name is John Smith. Employee ID is EMP-2024-789. +Job title is Firefighter Paramedic. Location is Station 12 Sacramento. +Department is Emergency Medical Services. Supervisor is Captain Rodriguez. +Phone number is 916-555-0147. +``` + +- Click **"Fill Form"** + +FireForm sends one request to Ollama (Mistral) which extracts all fields at once and returns structured JSON. + +### Step 3 — Download the filled PDF +- Click **"Download PDF"** to save the completed form + +--- + +## 🤖 How AI Extraction Works + +FireForm uses a **batch extraction** approach: + +``` +Traditional approach (slow): FireForm approach (fast): + Field 1 → Ollama call All fields → 1 Ollama call + Field 2 → Ollama call Mistral returns JSON with all values + Field 3 → Ollama call Parse → fill PDF + ...N calls total 1 call total (O(1)) +``` + +Field names are automatically read from the PDF's annotations and converted to human-readable labels before being sent to Mistral — so the model understands what each field means regardless of internal PDF naming conventions like `textbox_0_0`. + +**Example extraction:** +```json +{ + "NAME/SID": "John Smith", + "JobTitle": "Firefighter Paramedic", + "Department": "Emergency Medical Services", + "Phone Number": "916-555-0147", + "email": null +} +``` + +--- + +## 🧪 Running Tests + +```bash +python -m pytest tests/ -v +``` + +Expected output: **52 passed** + +See [TESTING.md](TESTING.md) for full test coverage details. + +--- + +## 🔧 Environment Variables + +| Variable | Default | Description | +|----------|---------|-------------| +| `OLLAMA_HOST` | `http://localhost:11434` | Ollama server URL | + +To use a remote Ollama instance: +```bash +export OLLAMA_HOST=http://your-server:11434 # Linux/Mac +set OLLAMA_HOST=http://your-server:11434 # Windows +``` + +--- + +## 🐳 Docker (Coming Soon) + +Docker support is in progress. See [docker.md](docker.md) for current status. + +--- + +## ❓ Troubleshooting + +**`Form data requires python-multipart`** +```bash +pip install python-multipart +``` + +**`ModuleNotFoundError: No module named 'pypdf'`** +```bash +pip install pypdf +``` + +**`Could not connect to Ollama`** +- Make sure `ollama serve` is running +- Check Ollama is on port 11434: `curl http://localhost:11434` + +**`NameError: name 'Union' is not defined`** +- Pull latest changes: `git pull origin main` +- This bug is fixed in the current version + +**Tests fail with `ModuleNotFoundError: No module named 'api'`** +- Use `python -m pytest` instead of `pytest` \ No newline at end of file diff --git a/docs/demo/filled_form_output.pdf b/docs/demo/filled_form_output.pdf new file mode 100644 index 0000000..6587e43 Binary files /dev/null and b/docs/demo/filled_form_output.pdf differ diff --git a/docs/demo/frontend_ui.png b/docs/demo/frontend_ui.png new file mode 100644 index 0000000..856c696 Binary files /dev/null and b/docs/demo/frontend_ui.png differ diff --git a/docs/demo/frontend_ui02.png b/docs/demo/frontend_ui02.png new file mode 100644 index 0000000..ca84a72 Binary files /dev/null and b/docs/demo/frontend_ui02.png differ diff --git a/docs/frontend.md b/docs/frontend.md new file mode 100644 index 0000000..22d2b55 --- /dev/null +++ b/docs/frontend.md @@ -0,0 +1,218 @@ +# Frontend UI Guide + +This guide explains how to set up and use the FireForm browser-based frontend interface. + +## Overview + +The FireForm frontend is a single-page web application (`frontend/index.html`) that provides a user-friendly interface for non-technical first responders to: + +- Upload and save fillable PDF form templates +- Describe incidents in plain language +- Auto-fill forms using local AI (Mistral via Ollama) +- Download completed PDF forms instantly + +> [!IMPORTANT] +> The frontend communicates with the FastAPI backend at `http://127.0.0.1:8000`. Make sure both Ollama and the API server are running before opening the frontend. + +--- + +## Prerequisites + +Before running the frontend, ensure the following are set up: + +> [!IMPORTANT] +> Complete the database setup described in [db.md](db.md) first. + +1. **Ollama** installed and running — [https://ollama.com/download](https://ollama.com/download) +2. **Mistral model** pulled: + ```bash + ollama pull mistral + ``` +3. **Dependencies** installed: + ```bash + pip install -r requirements.txt + ``` + +--- + +## Running the Frontend + +### Step 1 — Start Ollama + +In a terminal, run: + +```bash +ollama serve +``` + +> [!TIP] +> Leave this terminal open. Ollama must stay running for AI extraction to work. + +### Step 2 — Initialize the Database + +```bash +python -m api.db.init_db +``` + +### Step 3 — Start the API Server + +In a new terminal, from the project root: + +```bash +uvicorn api.main:app --reload +``` + +If successful, you will see: +`INFO: Uvicorn running on http://127.0.0.1:8000` + +### Step 4 — Open the Frontend + +Open `frontend/index.html` directly in your browser by double-clicking it, or navigate to it in your file explorer. + +> [!NOTE] +> No additional server is required for the frontend. It is a static HTML file that communicates directly with the FastAPI backend. + +--- + +## Using the Frontend + +The interface guides you through 4 steps: + +### Step 1 — Upload a Template + +1. Click **"Click to upload"** or drag and drop a fillable PDF form +2. Enter a name for the template (e.g. `Cal Fire Incident Report`) +3. Click **"SAVE TEMPLATE →"** + +The template is saved to the database and will appear in the **Saved Templates** list. + +> [!TIP] +> Any fillable PDF form works. The system automatically detects all form fields. + +### Step 2 — Select a Template + +Click any saved template from the **Saved Templates** list in the sidebar. The selected template will be highlighted in red. + +### Step 3 — Describe the Incident + +Type or paste a plain-language description of the incident in the text area. For best results, include all relevant details that match your form's fields. + +**Example for an employee form:** +``` +The employee's name is John Smith. His employee ID is EMP-2024-789. +His job title is Firefighter Paramedic. His location is Station 12, +Sacramento. His department is Emergency Medical Services. His supervisor +is Captain Jane Rodriguez. His phone number is 916-555-0147. +His email is jsmith@calfire.ca.gov. +``` + +**Example for an incident report form:** +``` +Officer Hernandez responding to a structure fire at 742 Evergreen Terrace. +Two occupants evacuated safely. Minor smoke inhalation treated on scene +by EMS. Unit 7 on scene at 14:32, cleared at 16:45. +Handed off to Deputy Martinez. +``` + +### Step 4 — Fill and Download + +Click **"⚡ FILL FORM"**. The system will: + +1. Send the description to Mistral (running locally via Ollama) +2. Extract all relevant field values +3. Fill the PDF template automatically +4. Provide a **"⬇ Download PDF"** button + +> [!NOTE] +> Processing time depends on your hardware. Typically 10–30 seconds with Mistral on a standard machine. + +--- + +## API Endpoints + +The frontend uses the following API endpoints: + +| Method | Endpoint | Description | +|--------|----------|-------------| +| `POST` | `/templates/create` | Upload a new PDF template | +| `GET` | `/templates` | List all saved templates | +| `GET` | `/templates/{id}` | Get a specific template | +| `POST` | `/forms/fill` | Fill a form with incident text | +| `GET` | `/forms/{id}` | Get a submission record | +| `GET` | `/forms/download/{id}` | Download a filled PDF | + +For full API documentation, visit [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) while the server is running. + +--- + +## API Status Indicator + +The top-right corner of the frontend shows the API connection status: + +- 🟢 **api online** — Backend is reachable, ready to use +- 🔴 **api offline** — Backend is not running, check uvicorn + +--- + +## Troubleshooting + +### "api offline" shown in the top bar + +The FastAPI server is not running. Start it with: +```bash +uvicorn api.main:app --reload +``` + +### Form fills with null or incorrect values + +This happens when the incident description does not contain information matching the PDF form fields. Ensure your description includes the specific data your form requires (names, dates, locations, etc.). + +See [Issue #113](https://github.com/fireform-core/FireForm/issues/113) for context on matching input to templates. + +### "Could not connect to Ollama" error + +Ollama is not running. Start it with: +```bash +ollama serve +``` + +Then verify Mistral is available: +```bash +ollama list +``` + +If Mistral is not listed, pull it: +```bash +ollama pull mistral +``` + +### Port conflict on 11434 + +Something else is using Ollama's port. On Linux/Mac: +```bash +sudo lsof -i :11434 +``` +On Windows: +```cmd +netstat -ano | findstr :11434 +``` + +--- + +## Privacy + +> [!IMPORTANT] +> FireForm is designed to be fully private. All AI processing happens locally via Ollama. No incident data, form content, or personal information is ever sent to external servers. + +--- + +## Docker Usage + +To run the full stack including the frontend API via Docker: + +```bash +chmod +x container-init.sh +./container-init.sh +``` + +See [docker.md](docker.md) for full Docker setup instructions. diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 0000000..a3b0083 --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,467 @@ + + + + + +FireForm — Report Once, File Everywhere + + + + +
+
+ + + +
+ + +
+
+
UN Digital Public Good · GSoC 2026
+

REPORT
ONCE.

+

Describe any incident in plain language. FireForm uses a locally-running AI to extract every relevant detail and auto-fill all required agency forms — instantly and privately.

+
+ +
+
+
1
+
Upload Template
Any fillable PDF form
+
+
+
2
+
Select Template
Choose from saved forms
+
+
+
3
+
Describe Incident
Plain language report
+
+
+
4
+
Download PDF
All fields auto-filled
+
+
+ +
+
← Select a template from the sidebar
+
+ Incident Description * + 0 chars +
+ +
+ +
Runs via Ollama locally.
No data leaves your machine.
+
+
+
+
+
Mistral is extracting data and filling your form...
+
+
+
+
✓ FORM FILLED SUCCESSFULLY
+ ⬇ Download PDF +
+
+
+
+
+
+ +
+
+
Session History
+
0 submissions
+
+
+
No submissions yet this session.
+
+
+
+
+ + + + \ No newline at end of file diff --git a/src/llm.py b/src/llm.py index 70937f9..71e2adf 100644 --- a/src/llm.py +++ b/src/llm.py @@ -5,10 +5,14 @@ class LLM: def __init__(self, transcript_text=None, target_fields=None, json=None): + """ + target_fields: dict or list containing the template field names to extract + (dict format: {"field_name": "human_label"}, list format: ["field_name1", "field_name2"]) + """ if json is None: json = {} self._transcript_text = transcript_text # str - self._target_fields = target_fields # List, contains the template field. + self._target_fields = target_fields # dict or list self._json = json # dictionary def type_check_all(self): @@ -17,64 +21,182 @@ def type_check_all(self): f"ERROR in LLM() attributes ->\ Transcript must be text. Input:\n\ttranscript_text: {self._transcript_text}" ) - elif type(self._target_fields) is not list: + if not isinstance(self._target_fields, (list, dict)): raise TypeError( f"ERROR in LLM() attributes ->\ - Target fields must be a list. Input:\n\ttarget_fields: {self._target_fields}" + Target fields must be a list or dict. Input:\n\ttarget_fields: {self._target_fields}" ) - def build_prompt(self, current_field): + def build_batch_prompt(self) -> str: + """ + Build a single prompt that extracts ALL fields at once. + Sends human-readable labels as context so Mistral understands + what each internal field name means. + Fixes Issue #196 — reduces N Ollama calls to 1. + """ + if isinstance(self._target_fields, dict): + fields_lines = "\n".join( + f' "{k}": null // {v if v and v != k else k}' + for k, v in self._target_fields.items() + ) + else: + fields_lines = "\n".join( + f' "{f}": null' + for f in self._target_fields + ) + + prompt = f"""You are filling out an official form. Extract values from the transcript below. + +FORM FIELDS (each line: "internal_key": null // visible label on form): +{{ +{fields_lines} +}} + +RULES: +1. Return ONLY a valid JSON object — no explanation, no markdown, no extra text +2. Use the visible label (after //) to understand what each field means +3. Fill each key with the matching value from the transcript +4. If a value is not found in the transcript, use null +5. Never invent or guess values not present in the transcript +6. For multiple values (e.g. multiple victims), use a semicolon-separated string: "Name1; Name2" +7. Distinguish roles carefully: Officer/Employee is NOT the same as Victim or Suspect + +TRANSCRIPT: +{self._transcript_text} + +JSON:""" + + return prompt + + def build_prompt(self, current_field: str) -> str: """ - This method is in charge of the prompt engineering. It creates a specific prompt for each target field. - @params: current_field -> represents the current element of the json that is being prompted. + Legacy single-field prompt — kept for backward compatibility. + Used as fallback if batch parsing fails. """ - prompt = f""" - SYSTEM PROMPT: - You are an AI assistant designed to help fillout json files with information extracted from transcribed voice recordings. - You will receive the transcription, and the name of the JSON field whose value you have to identify in the context. Return - only a single string containing the identified value for the JSON field. - If the field name is plural, and you identify more than one possible value in the text, return both separated by a ";". - If you don't identify the value in the provided text, return "-1". - --- - DATA: - Target JSON field to find in text: {current_field} - - TEXT: {self._transcript_text} - """ + field_lower = current_field.lower() + is_plural = current_field.endswith('s') and not current_field.lower().endswith('ss') + + if any(w in field_lower for w in ['officer', 'employee', 'dispatcher', 'caller', 'reporting', 'supervisor']): + role_guidance = """ +ROLE: Extract the PRIMARY OFFICER/EMPLOYEE/DISPATCHER +- This is typically the person speaking or reporting the incident +- DO NOT extract victims, witnesses, or members of the public +- Example: "Officer Smith reporting... victims are John and Jane" → extract "Smith" +""" + elif any(w in field_lower for w in ['victim', 'injured', 'affected', 'casualty', 'patient']): + role_guidance = f""" +ROLE: Extract VICTIM/AFFECTED PERSON(S) +- Focus on people who experienced harm +- Ignore officers, dispatchers, and witnesses +{'- Return ALL names separated by ";"' if is_plural else '- Return the FIRST/PRIMARY victim'} +""" + elif any(w in field_lower for w in ['location', 'address', 'street', 'place', 'where']): + role_guidance = """ +ROLE: Extract LOCATION/ADDRESS +- Extract WHERE the incident occurred +- Return only the incident location, not other addresses mentioned +""" + elif any(w in field_lower for w in ['date', 'time', 'when', 'occurred', 'reported']): + role_guidance = """ +ROLE: Extract DATE/TIME +- Extract WHEN the incident occurred +- Return in the format it appears in the text +""" + elif any(w in field_lower for w in ['phone', 'number', 'contact', 'tel']): + role_guidance = "ROLE: Extract PHONE NUMBER — return exactly as it appears in text" + elif any(w in field_lower for w in ['email', 'mail']): + role_guidance = "ROLE: Extract EMAIL ADDRESS" + elif any(w in field_lower for w in ['department', 'unit', 'division']): + role_guidance = "ROLE: Extract DEPARTMENT/UNIT name" + elif any(w in field_lower for w in ['title', 'job', 'role', 'rank', 'position']): + role_guidance = "ROLE: Extract JOB TITLE or RANK" + elif any(w in field_lower for w in ['id', 'badge', 'identifier']): + role_guidance = "ROLE: Extract ID or BADGE NUMBER" + elif any(w in field_lower for w in ['description', 'incident', 'detail', 'nature', 'summary']): + role_guidance = "ROLE: Extract a brief INCIDENT DESCRIPTION" + else: + role_guidance = f""" +ROLE: Generic extraction for field "{current_field}" +{'- Return MULTIPLE values separated by ";" if applicable' if is_plural else '- Return the PRIMARY matching value'} +""" + + prompt = f""" +SYSTEM: You are extracting specific information from an incident report transcript. + +FIELD TO EXTRACT: {current_field} +{'[SINGULAR - Extract ONE value]' if not is_plural else '[PLURAL - Extract MULTIPLE values separated by semicolon]'} + +EXTRACTION RULES: +{role_guidance} + +CRITICAL RULES: +1. Read the ENTIRE text before answering +2. Extract ONLY what belongs to this specific field +3. Return values exactly as they appear in the text +4. If not found, return: -1 + +TRANSCRIPT: +{self._transcript_text} + +ANSWER: Return ONLY the extracted value(s), nothing else.""" return prompt def main_loop(self): - # self.type_check_all() - for field in self._target_fields.keys(): - prompt = self.build_prompt(field) - # print(prompt) - # ollama_url = "http://localhost:11434/api/generate" - ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434").rstrip("/") - ollama_url = f"{ollama_host}/api/generate" - - payload = { - "model": "mistral", - "prompt": prompt, - "stream": False, # don't really know why --> look into this later. - } + """ + Single batch Ollama call — extracts ALL fields in one request. + Falls back to per-field extraction if JSON parsing fails. + Fixes Issue #196 (O(N) → O(1) LLM calls). + """ + ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434").rstrip("/") + ollama_url = f"{ollama_host}/api/generate" - try: - response = requests.post(ollama_url, json=payload) - response.raise_for_status() - except requests.exceptions.ConnectionError: - raise ConnectionError( - f"Could not connect to Ollama at {ollama_url}. " - "Please ensure Ollama is running and accessible." - ) - except requests.exceptions.HTTPError as e: - raise RuntimeError(f"Ollama returned an error: {e}") - - # parse response - json_data = response.json() - parsed_response = json_data["response"] - # print(parsed_response) - self.add_response_to_json(field, parsed_response) + # Get field keys for result mapping + if isinstance(self._target_fields, dict): + field_keys = list(self._target_fields.keys()) + else: + field_keys = list(self._target_fields) + + # ── Single batch call ───────────────────────────────────── + prompt = self.build_batch_prompt() + payload = {"model": "mistral", "prompt": prompt, "stream": False} + + try: + response = requests.post(ollama_url, json=payload) + response.raise_for_status() + except requests.exceptions.ConnectionError: + raise ConnectionError( + f"Could not connect to Ollama at {ollama_url}. " + "Please ensure Ollama is running and accessible." + ) + except requests.exceptions.HTTPError as e: + raise RuntimeError(f"Ollama returned an error: {e}") + + raw = response.json()["response"].strip() + + # Strip markdown code fences if Mistral wraps in ```json ... ``` + raw = raw.replace("```json", "").replace("```", "").strip() + + print("----------------------------------") + print("\t[LOG] Raw Mistral batch response:") + print(raw) + + # ── Parse JSON response ─────────────────────────────────── + try: + extracted = json.loads(raw) + for key in field_keys: + val = extracted.get(key) + if val and str(val).lower() not in ("null", "none", ""): + self._json[key] = val + else: + self._json[key] = None + + print("\t[LOG] Batch extraction successful.") + + except json.JSONDecodeError: + print("\t[WARN] Batch JSON parse failed — falling back to per-field extraction") + self._json = {} + self._fallback_per_field(ollama_url, field_keys) print("----------------------------------") print("\t[LOG] Resulting JSON created from the input text:") @@ -83,10 +205,36 @@ def main_loop(self): return self + def _fallback_per_field(self, ollama_url: str, field_keys: list): + """ + Legacy per-field extraction — used only when batch JSON parse fails. + """ + print("\t[LOG] Running fallback per-field extraction...") + + for field in field_keys: + if isinstance(self._target_fields, dict): + label = self._target_fields.get(field, field) + if not label or label == field: + label = field + else: + label = field + + prompt = self.build_prompt(label) + payload = {"model": "mistral", "prompt": prompt, "stream": False} + + try: + response = requests.post(ollama_url, json=payload) + response.raise_for_status() + parsed_response = response.json()["response"] + self.add_response_to_json(field, parsed_response) + except Exception as e: + print(f"\t[WARN] Failed to extract field '{field}': {e}") + self._json[field] = None + def add_response_to_json(self, field, value): """ - this method adds the following value under the specified field, - or under a new field if the field doesn't exist, to the json dict + Add extracted value under field name. + Handles plural (semicolon-separated) values. """ value = value.strip().replace('"', "") parsed_value = None @@ -94,42 +242,35 @@ def add_response_to_json(self, field, value): if value != "-1": parsed_value = value - if ";" in value: - parsed_value = self.handle_plural_values(value) + if parsed_value and ";" in parsed_value: + parsed_value = self.handle_plural_values(parsed_value) - if field in self._json.keys(): - self._json[field].append(parsed_value) + if field in self._json: + existing = self._json[field] + if isinstance(existing, list): + if isinstance(parsed_value, list): + existing.extend(parsed_value) + else: + existing.append(parsed_value) + else: + self._json[field] = [existing, parsed_value] else: self._json[field] = parsed_value - return - def handle_plural_values(self, plural_value): """ - This method handles plural values. - Takes in strings of the form 'value1; value2; value3; ...; valueN' - returns a list with the respective values -> [value1, value2, value3, ..., valueN] + Split semicolon-separated values into a list. + "Mark Smith; Jane Doe" → ["Mark Smith", "Jane Doe"] """ if ";" not in plural_value: raise ValueError( f"Value is not plural, doesn't have ; separator, Value: {plural_value}" ) - print( - f"\t[LOG]: Formating plural values for JSON, [For input {plural_value}]..." - ) - values = plural_value.split(";") - - # Remove trailing leading whitespace - for i in range(len(values)): - current = i + 1 - if current < len(values): - clean_value = values[current].lstrip() - values[current] = clean_value - + print(f"\t[LOG]: Formatting plural values for JSON, [For input {plural_value}]...") + values = [v.strip() for v in plural_value.split(";") if v.strip()] print(f"\t[LOG]: Resulting formatted list of values: {values}") - return values def get_data(self): - return self._json + return self._json \ No newline at end of file diff --git a/src/main.py b/src/main.py index 5bb632b..e07578b 100644 --- a/src/main.py +++ b/src/main.py @@ -1,5 +1,6 @@ import os # from backend import Fill +from typing import Union from commonforms import prepare_form from pypdf import PdfReader from controller import Controller