import gradio as gr
from transformers import pipeline
import json
# Initialize NLP pipeline
ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
def analyze_event(text):
try:
# Process text with NER pipeline
ner_results = ner_pipeline(text)
# Group entities
entities = {
"people": [],
"organizations": [],
"locations": [],
"hashtags": [word for word in text.split() if word.startswith('#')]
}
for item in ner_results:
if item["entity"].endswith("PER"):
entities["people"].append(item["word"])
elif item["entity"].endswith("ORG"):
entities["organizations"].append(item["word"])
elif item["entity"].endswith("LOC"):
entities["locations"].append(item["word"])
# Calculate confidence
confidence = min(1.0, (
0.2 * bool(entities["people"]) +
0.2 * bool(entities["organizations"]) +
0.3 * bool(entities["locations"]) +
0.3 * bool(entities["hashtags"])
))
return {
"text": text,
"entities": entities,
"confidence": confidence,
"verification_needed": confidence < 0.6
}
except Exception as e:
return {"error": str(e)}
# Create Gradio interface with custom CSS and HTML
css = """
.container { max-width: 800px; margin: auto; padding: 20px; }
.results { padding: 20px; border: 1px solid #ddd; border-radius: 8px; margin-top: 20px; }
.confidence-high { color: #22c55e; font-weight: bold; }
.confidence-low { color: #f97316; font-weight: bold; }
.entity-section { margin: 15px 0; }
.alert-warning { background: #fff3cd; padding: 10px; border-radius: 5px; margin: 10px 0; }
.alert-success { background: #d1fae5; padding: 10px; border-radius: 5px; margin: 10px 0; }
"""
def format_results(analysis_result):
if "error" in analysis_result:
return f"
Error: {analysis_result['error']}
"
confidence_class = "confidence-high" if analysis_result["confidence"] >= 0.6 else "confidence-low"
html = f"""
Analysis Results
Confidence Score: {int(analysis_result['confidence'] * 100)}%
{f'''
⚠️ Verification Required: Low confidence score detected. Please verify the extracted information.
''' if analysis_result["verification_needed"] else ''}
👤 People Detected
{''.join(f'- {person}
' for person in analysis_result['entities']['people']) or '- None detected
'}
🏢 Organizations
{''.join(f'- {org}
' for org in analysis_result['entities']['organizations']) or '- None detected
'}
📍 Locations
{''.join(f'- {loc}
' for loc in analysis_result['entities']['locations']) or '- None detected
'}
# Hashtags
{''.join(f'- {tag}
' for tag in analysis_result['entities']['hashtags']) or '- None detected
'}
{f'''
✅ Event Validated: The extracted information meets confidence thresholds.
''' if not analysis_result["verification_needed"] else ''}
"""
return html
demo = gr.Interface(
fn=lambda text: format_results(analyze_event(text)),
inputs=[
gr.Textbox(
label="Event Text",
placeholder="Enter text to analyze (e.g., 'John from Tech Corp. is attending the meeting in Washington, DC #tech')",
lines=3
)
],
outputs=gr.HTML(),
title="DoD Event Analysis System",
description="Analyze text to extract entities, assess confidence, and identify key event information.",
css=css,
theme=gr.themes.Soft(),
examples=[
["John from Tech Corp. is attending the meeting in Washington, DC tomorrow #tech"],
["Sarah Johnson and Mike Smith from Defense Systems Inc. are conducting training in Norfolk, VA #defense #training"],
["Team meeting at headquarters with @commander_smith #briefing"]
]
)
if __name__ == "__main__":
demo.launch()