Chris4K commited on
Commit
6e4221e
verified
1 Parent(s): 28094fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -2
app.py CHANGED
@@ -1,4 +1,124 @@
1
- from transformers.tools.base import launch_gradio_demo
 
 
 
 
 
 
2
  from ner_tool import NamedEntityRecognitionTool
3
 
4
- launch_gradio_demo(NamedEntityRecognitionTool)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import warnings
3
+
4
+ # Suppress warnings
5
+ warnings.filterwarnings("ignore")
6
+
7
+ # Import our NER Tool
8
  from ner_tool import NamedEntityRecognitionTool
9
 
10
+ # Initialize the NER Tool
11
+ ner_tool = NamedEntityRecognitionTool()
12
+
13
+ # Function to analyze text using our tool
14
+ def analyze_text(text, model, aggregation, min_score):
15
+ try:
16
+ result = ner_tool(
17
+ text=text,
18
+ model=model,
19
+ aggregation=aggregation,
20
+ min_score=float(min_score)
21
+ )
22
+ return result
23
+ except Exception as e:
24
+ return f"Error analyzing text: {str(e)}"
25
+
26
+ # Sample texts for quick testing
27
+ sample_texts = {
28
+ "Business News": """Apple Inc. CEO Tim Cook announced yesterday that the company will invest $5 billion
29
+ in new AI research centers across the United States and Europe. The first center will
30
+ open in San Francisco by December 2025, followed by additional facilities in New York,
31
+ London, and Berlin. This initiative, called 'Project Horizon', aims to compete with
32
+ Microsoft and Google in the rapidly growing artificial intelligence market.""",
33
+
34
+ "Political News": """The United Nations Security Council met in New York on Monday to discuss
35
+ the ongoing conflict in Eastern Europe. Representatives from the United States,
36
+ Russia, China, and the European Union presented their positions. Secretary-General
37
+ Ant贸nio Guterres urged all parties to return to diplomatic negotiations by July 15th.""",
38
+
39
+ "Sports News": """Manchester United defeated Liverpool 3-2 in yesterday's Premier League match
40
+ at Old Trafford. Marcus Rashford scored two goals, while Mohamed Salah managed to score
41
+ for Liverpool. The victory puts Manchester United in second place in the league standings,
42
+ just behind Manchester City.""",
43
+
44
+ "Academic Text": """According to researchers at Stanford University and MIT, the latest advancements
45
+ in quantum computing could revolutionize cryptography within the next decade. The paper,
46
+ published in the Journal of Computational Physics, suggests that Shor's algorithm implemented
47
+ on quantum systems with just 100 qubits could potentially break RSA encryption."""
48
+ }
49
+
50
+ # Create Gradio interface
51
+ with gr.Blocks(title="Named Entity Recognition Tool") as demo:
52
+ gr.Markdown("# 馃攳 Named Entity Recognition Tool")
53
+ gr.Markdown("Identify and analyze named entities in text using different models and display formats.")
54
+
55
+ with gr.Row():
56
+ with gr.Column():
57
+ # Input section
58
+ text_input = gr.Textbox(
59
+ label="Text to Analyze",
60
+ placeholder="Enter text to analyze for named entities...",
61
+ lines=10
62
+ )
63
+
64
+ # Sample texts dropdown
65
+ sample_dropdown = gr.Dropdown(
66
+ choices=list(sample_texts.keys()),
67
+ label="Or Select a Sample Text"
68
+ )
69
+
70
+ # Configuration options
71
+ with gr.Row():
72
+ with gr.Column():
73
+ model_dropdown = gr.Dropdown(
74
+ choices=list(ner_tool.available_models.keys()),
75
+ value="dslim/bert-base-NER",
76
+ label="NER Model"
77
+ )
78
+
79
+ aggregation_dropdown = gr.Dropdown(
80
+ choices=["simple", "grouped", "detailed"],
81
+ value="grouped",
82
+ label="Display Format"
83
+ )
84
+
85
+ with gr.Column():
86
+ min_score_slider = gr.Slider(
87
+ minimum=0.1,
88
+ maximum=1.0,
89
+ value=0.8,
90
+ step=0.05,
91
+ label="Minimum Confidence Score"
92
+ )
93
+
94
+ analyze_button = gr.Button("Analyze Text")
95
+
96
+ with gr.Column():
97
+ # Output section
98
+ result_output = gr.Textbox(label="Analysis Results", lines=20)
99
+
100
+ # Model info
101
+ gr.Markdown("### Available Models:")
102
+ model_info = gr.HTML(
103
+ "".join([f"<p><strong>{k}</strong>: {v}</p>" for k, v in ner_tool.available_models.items()])
104
+ )
105
+
106
+ # Set up event handlers
107
+ def load_sample(sample_name):
108
+ return sample_texts.get(sample_name, "")
109
+
110
+ sample_dropdown.change(
111
+ load_sample,
112
+ inputs=sample_dropdown,
113
+ outputs=text_input
114
+ )
115
+
116
+ analyze_button.click(
117
+ analyze_text,
118
+ inputs=[text_input, model_dropdown, aggregation_dropdown, min_score_slider],
119
+ outputs=result_output
120
+ )
121
+
122
+ # Launch the app
123
+ if __name__ == "__main__":
124
+ demo.launch()