Spaces:
Sleeping
Sleeping
| 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"<div style='color: red'>Error: {analysis_result['error']}</div>" | |
| confidence_class = "confidence-high" if analysis_result["confidence"] >= 0.6 else "confidence-low" | |
| html = f""" | |
| <div class="results"> | |
| <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;"> | |
| <h3 style="margin: 0;">Analysis Results</h3> | |
| <div> | |
| Confidence Score: <span class="{confidence_class}">{int(analysis_result['confidence'] * 100)}%</span> | |
| </div> | |
| </div> | |
| {f''' | |
| <div class="alert-warning"> | |
| β οΈ <strong>Verification Required:</strong> Low confidence score detected. Please verify the extracted information. | |
| </div> | |
| ''' if analysis_result["verification_needed"] else ''} | |
| <div class="entity-section"> | |
| <h4>π€ People Detected</h4> | |
| <ul>{''.join(f'<li>{person}</li>' for person in analysis_result['entities']['people']) or '<li>None detected</li>'}</ul> | |
| </div> | |
| <div class="entity-section"> | |
| <h4>π’ Organizations</h4> | |
| <ul>{''.join(f'<li>{org}</li>' for org in analysis_result['entities']['organizations']) or '<li>None detected</li>'}</ul> | |
| </div> | |
| <div class="entity-section"> | |
| <h4>π Locations</h4> | |
| <ul>{''.join(f'<li>{loc}</li>' for loc in analysis_result['entities']['locations']) or '<li>None detected</li>'}</ul> | |
| </div> | |
| <div class="entity-section"> | |
| <h4># Hashtags</h4> | |
| <ul>{''.join(f'<li>{tag}</li>' for tag in analysis_result['entities']['hashtags']) or '<li>None detected</li>'}</ul> | |
| </div> | |
| {f''' | |
| <div class="alert-success"> | |
| β <strong>Event Validated:</strong> The extracted information meets confidence thresholds. | |
| </div> | |
| ''' if not analysis_result["verification_needed"] else ''} | |
| </div> | |
| """ | |
| 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() |