-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
87 lines (73 loc) · 2.8 KB
/
app.py
File metadata and controls
87 lines (73 loc) · 2.8 KB
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
import json
import re
def extract_json(input_string):
"""
Extracts the first JSON code block from the input string and returns it as a Python dictionary.
Args:
input_string (str): The input string containing a JSON code block.
Returns:
dict: The parsed JSON object.
Raises:
ValueError: If no JSON code block is found or if the JSON is invalid.
"""
# Regular expression to match a JSON code block
# It looks for ```json (case-insensitive), captures everything until the next ```
pattern = re.compile(r"```json\s*(\{.*?\})\s*```", re.DOTALL | re.IGNORECASE)
match = pattern.search(input_string)
if not match:
raise ValueError("No JSON code block found in the input string.")
json_str = match.group(1)
try:
# Parse the JSON string into a Python dictionary
json_data = json.loads(json_str)
return json_data
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON format: {e}")
# Example usage:
if __name__ == "__main__":
input_text = """
Based on the retrieved data regarding fire incidents, here is the structured JSON format of findings:
```json
{
"fire_incidents": [
{
"severity": "high",
"description": "Large fire consuming a building",
"time_interval": {
"start_time": "30 seconds",
"end_time": "60 seconds"
}
},
{
"severity": "medium",
"description": "Fire with flames and smoke visible, structure outlines obscured",
"time_interval": {
"start_time": "60 seconds",
"end_time": "90 seconds"
}
},
{
"severity": "low",
"description": "Individual using a tool in a dimly lit environment, possibly related to fire response",
"time_interval": {
"start_time": "90 seconds",
"end_time": "end of video"
}
}
]
}
```
### Summary of Findings:
- The first incident is categorized as high severity, indicating a significant fire affecting a building.
- The second incident shows visible flames and smoke, with medium severity due to the ongoing danger.
- The third incident is of low severity, depicting post-fire activity involving fire response efforts.
"""
try:
extracted_json = extract_json(input_text)
# Ensure extracted_json is a dictionary and has the key 'fire_incidents'
if isinstance(extracted_json, dict) and "fire_incidents" in extracted_json:
# Append the new entry to the 'fire_incidents' list
extracted_json["fire_incidents"].append({"severity": "none"})
print(json.dumps(extracted_json, indent=2))
except ValueError as ve:
print(f"Error: {ve}")