dlaima commited on
Commit
f6aeb16
·
verified ·
1 Parent(s): ea0f798

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -1,37 +1,44 @@
1
- # Import the required libraries
2
- import os # os provides a way of using operating system-dependent functionality
3
- import io # this library provides core tools for working with streams of data
4
- from IPython.display import Image, display, HTML # This is used for displaying rich content (e.g., images, HTML) in Jupyter Notebooks
5
- from PIL import Image # Python Imaging Library (PIL) is used for opening, manipulating, and saving image files
6
- import base64 # This library encodes and decodes data in base64 format
7
- import requests
8
- import json
9
- from dotenv import load_dotenv, find_dotenv
10
  import gradio as gr
11
-
12
-
13
- # Load environment variables from .env file
14
- load_dotenv(find_dotenv())
15
- hf_api_key = os.getenv('HF_API_KEY')
16
- API_URL = os.getenv('HF_API_NER_BASE')
17
-
18
- #Get completion to be defined?
19
-
20
- def ner(input):
21
- output = get_completion(input, parameters=None, endpoint_url=API_URL)
22
- return {"text": input, "entities": output}
23
-
24
- # Create a Gradio interface
25
- iface = gr.Interface(fn=ner,
26
- inputs=[gr.Textbox(label="Text to find entities", lines=2)],
27
- outputs=[gr.HighlightedText(label="Text with entities")],
28
- title="NER with dslim/bert-base-NER",
29
- description="Find entities using the `dslim/bert-base-NER` model under the hood!",
30
- allow_flagging="never",
31
- examples=["My name is Michela and I live in Italy", "My name is Andrew and work at HuggingFace"])
32
-
33
- # Launch the app (this will allow you to test locally before uploading to Hugging Face)
34
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
 
37
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load the NER pipeline from Hugging Face
5
+ ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
6
+
7
+ # Function to perform NER
8
+ def perform_ner(text):
9
+ # Run NER on the input text
10
+ results = ner_pipeline(text)
11
+
12
+ # Format results for better readability
13
+ formatted_results = [
14
+ {
15
+ "Entity": result["entity"],
16
+ "Word": result["word"],
17
+ "Score": round(result["score"], 4),
18
+ "Start": result["start"],
19
+ "End": result["end"]
20
+ }
21
+ for result in results
22
+ ]
23
+ return formatted_results
24
+
25
+ # Define the Gradio interface
26
+ title = "Named Entity Recognition (NER) App"
27
+ description = "Enter text below to extract named entities like persons, organizations, and locations using a Hugging Face model."
28
+
29
+ interface = gr.Interface(
30
+ fn=perform_ner, # The function to process input
31
+ inputs=gr.Textbox(lines=5, label="Input Text"), # Text input for the user
32
+ outputs=gr.Dataframe( # Display results as a table
33
+ headers=["Entity", "Word", "Score", "Start", "End"],
34
+ label="Recognized Entities"
35
+ ),
36
+ title=title,
37
+ description=description
38
+ )
39
+
40
+ # Launch the Gradio app
41
+ interface.launch()
42
 
43
 
44