dlaima commited on
Commit
41c8606
·
verified ·
1 Parent(s): d6046fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -29
app.py CHANGED
@@ -1,14 +1,14 @@
1
  # Import the required libraries
2
- import os
3
- import requests
4
- import json
5
  from dotenv import load_dotenv, find_dotenv
6
  import gradio as gr
7
 
8
  # Load environment variables from .env file
9
  load_dotenv(find_dotenv())
10
- hf_api_key = os.getenv('HF_API_KEY')
11
- API_URL = os.getenv('HF_API_NER_BASE')
12
 
13
  # Define the `get_completion` function to interact with the Hugging Face API
14
  def get_completion(inputs, parameters=None, endpoint_url=None):
@@ -22,43 +22,28 @@ def get_completion(inputs, parameters=None, endpoint_url=None):
22
  try:
23
  response = requests.post(endpoint_url, headers=headers, data=json.dumps(data))
24
  response.raise_for_status()
25
- return response.json()
26
  except requests.exceptions.RequestException as e:
27
  print(f"Error: {e}")
28
- return []
29
 
30
  # Function to perform Named Entity Recognition (NER)
31
- def ner(input_text):
32
- # Call the Hugging Face API
33
- output = get_completion(input_text, parameters=None, endpoint_url=API_URL)
34
-
35
- # Check if the API response is valid
36
- if not output or not isinstance(output, list):
37
- return {"text": input_text, "entities": []}
38
-
39
- # Format the output for HighlightedText
40
- entities = [
41
- (entity["start"], entity["end"], entity["entity_group"])
42
- for entity in output if "start" in entity and "end" in entity and "entity_group" in entity
43
- ]
44
- return {"text": input_text, "entities": entities}
45
 
46
  # Create a Gradio interface
47
  iface = gr.Interface(
48
  fn=ner,
49
- inputs=gr.Textbox(label="Text to find entities", lines=2),
50
- outputs=gr.HighlightedText(label="Text with entities"),
51
  title="NER with dslim/bert-base-NER",
52
  description="Find entities using the `dslim/bert-base-NER` model under the hood!",
53
  allow_flagging="never",
54
- examples=[
55
- "My name is Michela and I live in Italy.",
56
- "My name is Andrew and I work at HuggingFace.",
57
- "Barack Obama was the president of the United States."
58
- ]
59
  )
60
 
61
- # Launch the app
62
  iface.launch()
63
 
64
 
@@ -66,3 +51,4 @@ iface.launch()
66
 
67
 
68
 
 
 
1
  # Import the required libraries
2
+ import os # Provides a way of using operating system-dependent functionality
3
+ import requests # For making HTTP requests to the API
4
+ import json # For handling JSON data
5
  from dotenv import load_dotenv, find_dotenv
6
  import gradio as gr
7
 
8
  # Load environment variables from .env file
9
  load_dotenv(find_dotenv())
10
+ hf_api_key = os.getenv('HF_API_KEY') # Hugging Face API key
11
+ API_URL = os.getenv('HF_API_NER_BASE') # Endpoint for the NER model
12
 
13
  # Define the `get_completion` function to interact with the Hugging Face API
14
  def get_completion(inputs, parameters=None, endpoint_url=None):
 
22
  try:
23
  response = requests.post(endpoint_url, headers=headers, data=json.dumps(data))
24
  response.raise_for_status()
25
+ return response.json() # Return the API's JSON response
26
  except requests.exceptions.RequestException as e:
27
  print(f"Error: {e}")
28
+ return [{"entity": "Error", "word": "Error", "score": 0}]
29
 
30
  # Function to perform Named Entity Recognition (NER)
31
+ def ner(input):
32
+ output = get_completion(input, parameters=None, endpoint_url=API_URL)
33
+ return {"text": input, "entities": output}
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # Create a Gradio interface
36
  iface = gr.Interface(
37
  fn=ner,
38
+ inputs=[gr.Textbox(label="Text to find entities", lines=2)],
39
+ outputs=[gr.HighlightedText(label="Text with entities")],
40
  title="NER with dslim/bert-base-NER",
41
  description="Find entities using the `dslim/bert-base-NER` model under the hood!",
42
  allow_flagging="never",
43
+ examples=["My name is Michela and I live in Italy", "My name is Andrew and work at HuggingFace"]
 
 
 
 
44
  )
45
 
46
+ # Launch the app (this will allow you to test locally before uploading to Hugging Face)
47
  iface.launch()
48
 
49
 
 
51
 
52
 
53
 
54
+