swardiantara commited on
Commit
329563f
·
1 Parent(s): 85c5100
Files changed (2) hide show
  1. app.py +95 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # --------------------------------------------------------------------------
5
+ # 1. Load your NER model from the Hub using a pipeline
6
+ # --------------------------------------------------------------------------
7
+ # Replace this with your actual model name on the Hugging Face Hub
8
+ model_name = "swardiantara/ADFLER-xlnet-base-cased"
9
+ ner_pipeline = pipeline("ner", model=model_name, aggregation_strategy="simple")
10
+
11
+ # --------------------------------------------------------------------------
12
+ # 2. Define the prediction function
13
+ # --------------------------------------------------------------------------
14
+ # This function takes raw text and returns a format that Gradio's HighlightedText component understands.
15
+ def recognize_log_events(text):
16
+ """
17
+ Performs NER on the input text and formats the output for Gradio.
18
+ """
19
+ if not text:
20
+ return {"text": "", "entities": []}
21
+
22
+ ner_results = ner_pipeline(text)
23
+
24
+ # Format the results for the HighlightedText component
25
+ # It expects a list of tuples: (word, entity_label)
26
+ # The pipeline with aggregation_strategy="simple" provides this almost directly.
27
+ entities = []
28
+ for result in ner_results:
29
+ entities.append((result['entity_group'], result['word']))
30
+
31
+ # Gradio's HighlightedText component works best with a dictionary
32
+ # containing the original text and the list of entities.
33
+ # We will return the text split by spaces and the corresponding entities.
34
+ words = text.split()
35
+ highlighted_output = []
36
+
37
+ # This is a simple way to tag words. More complex logic may be needed
38
+ # if an entity spans multiple words that are not contiguous.
39
+ # For simplicity, we create a lookup for recognized words.
40
+ entity_lookup = {entity[1].strip(): entity[0] for entity in entities}
41
+
42
+ for word in words:
43
+ label = entity_lookup.get(word)
44
+ highlighted_output.append((word, label))
45
+
46
+ return highlighted_output
47
+
48
+ # --------------------------------------------------------------------------
49
+ # 3. Create the Gradio Interface
50
+ # --------------------------------------------------------------------------
51
+ # A brief description of your project to display in the app
52
+ description = """
53
+ This demo showcases an NER model for recognizing key events in drone flight logs,
54
+ a part of my PhD research in digital forensics at ITS.
55
+ Enter a line from a drone log to see the model identify events like 'Takeoff', 'Landing', 'GPS Lock', etc.
56
+ """
57
+
58
+ # An article providing more context and links
59
+ article = """
60
+ <div style='text-align: center;'>
61
+ <p>For more details, check out the project on GitHub:</p>
62
+ <a href='https://dronenlp.github.io/documentation' target='_blank'>DroneNLP Project</a> |
63
+ <a href='https://huggingface.co/swardiantara/ADFLER-xlnet-base-cased' target='_blank'>Model Card</a> |
64
+ </div>
65
+ """
66
+ # <a href='https' target='_blank'>Research Paper (if applicable)</a>
67
+
68
+ # Example drone log entries for users to try
69
+ examples = [
70
+ ["No image transmission. RTH.; Press Brake button to cancel RTH.; No image transmission. Aircraft returning to home.; Image transmission signal weak. Adjust antennas and make sure they are perpendicular to flight direction of aircraft.; Flight mode changed to Go Home."],
71
+ ["Battery temperature is below 15 degrees Celsius. Warm up the battery temperature to above 25 degree Celsius to ensure a safe flight."],
72
+ ["Strong wireless interference. Please fly with caution. Obstacle Avoidance Disabled. Landing gear lowered. Obstacle Avoidance Disabled."],
73
+ ["Cannot switch flight mode. Turn on 'Multiple Flight Modes' to enable Atti and Sport Modes."]
74
+ ]
75
+
76
+ # The main interface
77
+ iface = gr.Interface(
78
+ fn=recognize_log_events,
79
+ inputs=gr.Textbox(
80
+ lines=5,
81
+ label="Drone Flight Log Entry",
82
+ placeholder="Paste a log entry here..."
83
+ ),
84
+ outputs=gr.HighlightedText(
85
+ label="Recognized Events",
86
+ color_map={"Event": "green"} # Customize these labels and colors!
87
+ ),
88
+ title="🚁 Drone Flight Log Event Recognizer",
89
+ description=description,
90
+ article=article,
91
+ examples=examples
92
+ )
93
+
94
+ # Launch the app!
95
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ transformers[torch]
3
+ sentencepiece