Spaces:
Sleeping

File size: 1,362 Bytes
ce1cdb9
 
 
 
 
 
78b4b7c
ce1cdb9
 
 
 
78b4b7c
 
 
 
 
 
ce1cdb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Text extraction demo: turns a claimant story or police report into structured incident fields.

The model runs on Groq, so this Space needs no GPU. Set the secret GROQ_API_KEY and the
variable GROQ_MODEL (for example openai/gpt-oss-20b) in the Space settings.
"""
import gradio as gr
import spaces

from incident_extraction.extraction import ExtractionError, extract_incident


@spaces.GPU(duration=1)
def _zerogpu_placeholder():
    """Never called. ZeroGPU Spaces must declare one GPU function to start."""
    pass


def analyse(text, source):
    if not text or not text.strip():
        raise gr.Error("Paste a claimant story or police report first.")
    try:
        incident = extract_incident(text)
    except ExtractionError as error:
        raise gr.Error(str(error))
    return {"source": source, "incident": incident.model_dump()}


demo = gr.Interface(
    fn=analyse,
    inputs=[gr.Textbox(label="Document text", lines=8),
            gr.Radio(["claimant", "police"], value="claimant", label="Source")],
    outputs=gr.JSON(label="Extracted fields"),
    title="Incident text extraction",
    description="Extracts date, time, location, vehicles, fault, incident type and impacts. "
                "Facts the text doesn't state stay empty.",
    api_name="predict",
    flagging_mode="never",
)

if __name__ == "__main__":
    demo.launch()