dwb2023 commited on
Commit
c0a677b
Β·
verified Β·
1 Parent(s): a803819

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import json
4
+
5
+ # Initialize NLP pipeline
6
+ ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
7
+
8
+ def analyze_event(text):
9
+ try:
10
+ # Process text with NER pipeline
11
+ ner_results = ner_pipeline(text)
12
+
13
+ # Group entities
14
+ entities = {
15
+ "people": [],
16
+ "organizations": [],
17
+ "locations": [],
18
+ "hashtags": [word for word in text.split() if word.startswith('#')]
19
+ }
20
+
21
+ for item in ner_results:
22
+ if item["entity"].endswith("PER"):
23
+ entities["people"].append(item["word"])
24
+ elif item["entity"].endswith("ORG"):
25
+ entities["organizations"].append(item["word"])
26
+ elif item["entity"].endswith("LOC"):
27
+ entities["locations"].append(item["word"])
28
+
29
+ # Calculate confidence
30
+ confidence = min(1.0, (
31
+ 0.2 * bool(entities["people"]) +
32
+ 0.2 * bool(entities["organizations"]) +
33
+ 0.3 * bool(entities["locations"]) +
34
+ 0.3 * bool(entities["hashtags"])
35
+ ))
36
+
37
+ return {
38
+ "text": text,
39
+ "entities": entities,
40
+ "confidence": confidence,
41
+ "verification_needed": confidence < 0.6
42
+ }
43
+ except Exception as e:
44
+ return {"error": str(e)}
45
+
46
+ # Create Gradio interface with custom CSS and HTML
47
+ css = """
48
+ .container { max-width: 800px; margin: auto; padding: 20px; }
49
+ .results { padding: 20px; border: 1px solid #ddd; border-radius: 8px; margin-top: 20px; }
50
+ .confidence-high { color: #22c55e; font-weight: bold; }
51
+ .confidence-low { color: #f97316; font-weight: bold; }
52
+ .entity-section { margin: 15px 0; }
53
+ .alert-warning { background: #fff3cd; padding: 10px; border-radius: 5px; margin: 10px 0; }
54
+ .alert-success { background: #d1fae5; padding: 10px; border-radius: 5px; margin: 10px 0; }
55
+ """
56
+
57
+ def format_results(analysis_result):
58
+ if "error" in analysis_result:
59
+ return f"<div style='color: red'>Error: {analysis_result['error']}</div>"
60
+
61
+ confidence_class = "confidence-high" if analysis_result["confidence"] >= 0.6 else "confidence-low"
62
+
63
+ html = f"""
64
+ <div class="results">
65
+ <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
66
+ <h3 style="margin: 0;">Analysis Results</h3>
67
+ <div>
68
+ Confidence Score: <span class="{confidence_class}">{int(analysis_result['confidence'] * 100)}%</span>
69
+ </div>
70
+ </div>
71
+
72
+ {f'''
73
+ <div class="alert-warning">
74
+ ⚠️ <strong>Verification Required:</strong> Low confidence score detected. Please verify the extracted information.
75
+ </div>
76
+ ''' if analysis_result["verification_needed"] else ''}
77
+
78
+ <div class="entity-section">
79
+ <h4>πŸ‘€ People Detected</h4>
80
+ <ul>{''.join(f'<li>{person}</li>' for person in analysis_result['entities']['people']) or '<li>None detected</li>'}</ul>
81
+ </div>
82
+
83
+ <div class="entity-section">
84
+ <h4>🏒 Organizations</h4>
85
+ <ul>{''.join(f'<li>{org}</li>' for org in analysis_result['entities']['organizations']) or '<li>None detected</li>'}</ul>
86
+ </div>
87
+
88
+ <div class="entity-section">
89
+ <h4>πŸ“ Locations</h4>
90
+ <ul>{''.join(f'<li>{loc}</li>' for loc in analysis_result['entities']['locations']) or '<li>None detected</li>'}</ul>
91
+ </div>
92
+
93
+ <div class="entity-section">
94
+ <h4># Hashtags</h4>
95
+ <ul>{''.join(f'<li>{tag}</li>' for tag in analysis_result['entities']['hashtags']) or '<li>None detected</li>'}</ul>
96
+ </div>
97
+
98
+ {f'''
99
+ <div class="alert-success">
100
+ βœ… <strong>Event Validated:</strong> The extracted information meets confidence thresholds.
101
+ </div>
102
+ ''' if not analysis_result["verification_needed"] else ''}
103
+ </div>
104
+ """
105
+ return html
106
+
107
+ demo = gr.Interface(
108
+ fn=lambda text: format_results(analyze_event(text)),
109
+ inputs=[
110
+ gr.Textbox(
111
+ label="Event Text",
112
+ placeholder="Enter text to analyze (e.g., 'John from Tech Corp. is attending the meeting in Washington, DC #tech')",
113
+ lines=3
114
+ )
115
+ ],
116
+ outputs=gr.HTML(),
117
+ title="DoD Event Analysis System",
118
+ description="Analyze text to extract entities, assess confidence, and identify key event information.",
119
+ css=css,
120
+ theme=gr.themes.Soft(),
121
+ examples=[
122
+ ["John from Tech Corp. is attending the meeting in Washington, DC tomorrow #tech"],
123
+ ["Sarah Johnson and Mike Smith from Defense Systems Inc. are conducting training in Norfolk, VA #defense #training"],
124
+ ["Team meeting at headquarters with @commander_smith #briefing"]
125
+ ]
126
+ )
127
+
128
+ if __name__ == "__main__":
129
+ demo.launch()