Madhana commited on
Commit
d3baca2
·
1 Parent(s): 75b469d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -11,7 +11,6 @@ Note: This is a continuation of [this](https://colab.research.google.com/drive/1
11
  # Imports
12
  """
13
 
14
- '''
15
  !pip install transformers
16
 
17
  !pip install https://huggingface.co/Madhana/en_pipeline/resolve/main/en_pipeline-any-py3-none-any.whl
@@ -20,7 +19,6 @@ Note: This is a continuation of [this](https://colab.research.google.com/drive/1
20
  !pip install spacy-transformers
21
 
22
  !pip install gradio
23
- '''
24
 
25
  import json
26
  import requests
@@ -65,31 +63,32 @@ def get_data(bot_token):
65
  text_list = [r['channel_post']['text'] for r in response_json['result']]
66
  return text_list
67
  except KeyError:
68
- print('An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.')
69
  error_list = ['An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.']
70
  return error_list
71
  except Exception as e:
72
- print('An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.') #, e
73
  error_list = ['An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.']
74
  return error_list
75
 
 
 
76
  """# Classifier"""
77
 
78
  def classify_message(bot_token):
79
- if get_data(bot_token) != ['An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.']:
80
- disaster_docs = []
81
- classifier = pipeline("sentiment-analysis", model="Madhana/disaster_msges_classifier_v1")
82
- results = []
83
- for data in get_data(bot_token):
84
- classification = classifier(data)
85
- label = classification[0]['label']
86
- results.append((data, label))
87
- if label == 'DISASTER':
88
- disaster_docs.append(data)
89
- return disaster_docs
90
- else:
91
- error_list = ['An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.']
92
- return error_list
93
 
94
  """# NER Pipeline"""
95
 
@@ -108,9 +107,6 @@ Tamil_words = ['மதனா பாலா'] # umm, that's my name in Tamil, cons
108
  nlp = spacy.load("en_pipeline")
109
  nlp.add_pipe("disaster_ner", name="disaster_ner", before='ner')
110
 
111
- entity_types = ["NAME", "STREET", "NEIGHBORHOOD", "CITY", "PHONE NUMBER","YO!"]
112
- df = pd.DataFrame(columns=["Text"] + entity_types)
113
-
114
  def create_address(row):
115
  return f"{row['STREET']}, {row['NEIGHBORHOOD']}, {row['CITY']}"
116
 
@@ -127,6 +123,8 @@ def geocode_address(address):
127
 
128
  def get_classifier_ner(bot_token):
129
  data = classify_message(bot_token)
 
 
130
 
131
  for text in data:
132
  doc = nlp(text)
@@ -152,6 +150,8 @@ def get_classifier_ner(bot_token):
152
 
153
  def get_ner(bot_token):
154
  data = get_data(bot_token)
 
 
155
 
156
  for text in data:
157
  doc = nlp(text)
@@ -162,6 +162,7 @@ def get_ner(bot_token):
162
  # html = displacy.render(doc, style="ent")
163
  # row.append(html)
164
 
 
165
  num_cols = len(df.columns)
166
  while len(row) < num_cols:
167
  row.append("")
@@ -201,18 +202,20 @@ with demo:
201
  with gr.Row():
202
  gr.Markdown("""This is an Telegram based Disaster Recovery Assist app that uses Named Entity Recognition to extract important entities from the unstructured text and stores it in an dataframe.
203
  You need to provide your personal Telegram Bot API token (API token of the bot that is added to the channel as an administrator) to use this app.
204
- Steps to create a Telegram Bot:
205
  1. Download the Telegram app on your device or use the web version.
206
  2. Open the app and search for the "BotFather" bot.
207
  3. Start a chat with the BotFather bot by clicking on the "START" button.
208
  4. Type "/newbot" and follow the on-screen instructions to create a new bot.
209
  5. Choose a name and username for your bot. \6. Once your bot is created, the BotFather will give you a unique API token.
210
 
211
- Steps to add your telegram bot to your channel as an administrator:
212
  1. Create a new channel or choose an existing one that you want to use the bot in.
213
  2. Add your bot to the channel as an administrator. To do this, go to the channel settings, click on "Administrators", and then click on "Add Administrator". Search for your bot and add it to the channel.
214
  3. Now you can send commands to the bot in the channel by mentioning the bot using the "@" symbol followed by the bot's username. For example, "@my_bot help" will send the "help" command to the bot.
215
 
 
 
216
  *If you get any errors or dependency issues, feel free to reach out to me!*""")
217
 
218
 
@@ -222,4 +225,5 @@ with demo:
222
  clear.click(lambda: None, None, ner_df, queue=True)
223
 
224
  demo.queue(concurrency_count=3)
225
- demo.launch()
 
 
11
  # Imports
12
  """
13
 
 
14
  !pip install transformers
15
 
16
  !pip install https://huggingface.co/Madhana/en_pipeline/resolve/main/en_pipeline-any-py3-none-any.whl
 
19
  !pip install spacy-transformers
20
 
21
  !pip install gradio
 
22
 
23
  import json
24
  import requests
 
63
  text_list = [r['channel_post']['text'] for r in response_json['result']]
64
  return text_list
65
  except KeyError:
66
+ # print('An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.')
67
  error_list = ['An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.']
68
  return error_list
69
  except Exception as e:
70
+ # print('An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.') #, e
71
  error_list = ['An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.']
72
  return error_list
73
 
74
+ get_data('6261742702:AAGkil1tWLckpBbC088rTO3Lb-lH2hJVmBg')
75
+
76
  """# Classifier"""
77
 
78
  def classify_message(bot_token):
79
+ error_msg = ['An error occurred. Possibly empty request result or your Telegram Bot Token is incorrect.']
80
+ disaster_docs = []
81
+ classifier = pipeline("sentiment-analysis", model="Madhana/disaster_msges_classifier_v1")
82
+ results = []
83
+ for data in get_data(bot_token):
84
+ if data == error_msg[0]:
85
+ return error_msg
86
+ classification = classifier(data)
87
+ label = classification[0]['label']
88
+ results.append((data, label))
89
+ if label == 'DISASTER':
90
+ disaster_docs.append(data)
91
+ return disaster_docs
 
92
 
93
  """# NER Pipeline"""
94
 
 
107
  nlp = spacy.load("en_pipeline")
108
  nlp.add_pipe("disaster_ner", name="disaster_ner", before='ner')
109
 
 
 
 
110
  def create_address(row):
111
  return f"{row['STREET']}, {row['NEIGHBORHOOD']}, {row['CITY']}"
112
 
 
123
 
124
  def get_classifier_ner(bot_token):
125
  data = classify_message(bot_token)
126
+ entity_types = ["NAME", "STREET", "NEIGHBORHOOD", "CITY", "PHONE NUMBER","YO!"]
127
+ df = pd.DataFrame(columns=["Text"] + entity_types)
128
 
129
  for text in data:
130
  doc = nlp(text)
 
150
 
151
  def get_ner(bot_token):
152
  data = get_data(bot_token)
153
+ entity_types = ["NAME", "STREET", "NEIGHBORHOOD", "CITY", "PHONE NUMBER","YO!"]
154
+ df = pd.DataFrame(columns=["Text"] + entity_types)
155
 
156
  for text in data:
157
  doc = nlp(text)
 
162
  # html = displacy.render(doc, style="ent")
163
  # row.append(html)
164
 
165
+
166
  num_cols = len(df.columns)
167
  while len(row) < num_cols:
168
  row.append("")
 
202
  with gr.Row():
203
  gr.Markdown("""This is an Telegram based Disaster Recovery Assist app that uses Named Entity Recognition to extract important entities from the unstructured text and stores it in an dataframe.
204
  You need to provide your personal Telegram Bot API token (API token of the bot that is added to the channel as an administrator) to use this app.
205
+ **Steps to create a Telegram Bot**:
206
  1. Download the Telegram app on your device or use the web version.
207
  2. Open the app and search for the "BotFather" bot.
208
  3. Start a chat with the BotFather bot by clicking on the "START" button.
209
  4. Type "/newbot" and follow the on-screen instructions to create a new bot.
210
  5. Choose a name and username for your bot. \6. Once your bot is created, the BotFather will give you a unique API token.
211
 
212
+ **Steps to add your telegram bot to your channel as an administrator**:
213
  1. Create a new channel or choose an existing one that you want to use the bot in.
214
  2. Add your bot to the channel as an administrator. To do this, go to the channel settings, click on "Administrators", and then click on "Add Administrator". Search for your bot and add it to the channel.
215
  3. Now you can send commands to the bot in the channel by mentioning the bot using the "@" symbol followed by the bot's username. For example, "@my_bot help" will send the "help" command to the bot.
216
 
217
+ **Get Classifier-NER Output VS Get NER Output**:
218
+ The 'Get Classifier Ner Output' function first classifies the message as either a disaster message or a random message, and then applies the NER pipeline to the classified output. In contrast, the 'Get NER Output' function applies the NER pipeline directly to the message.
219
  *If you get any errors or dependency issues, feel free to reach out to me!*""")
220
 
221
 
 
225
  clear.click(lambda: None, None, ner_df, queue=True)
226
 
227
  demo.queue(concurrency_count=3)
228
+ demo.launch(share=True, debug=True) # share=True, debug=True
229
+