zmbfeng commited on
Commit
6465bf9
1 Parent(s): 6d9b69f

keywords extraction and intention classification

Browse files
Files changed (1) hide show
  1. app.py +52 -3
app.py CHANGED
@@ -24,6 +24,8 @@ login(os.environ["HF_TOKEN"])
24
  dt = datetime.datetime.now()
25
  print(dt)
26
  print("loading models")
 
 
27
  tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
28
  original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
29
  untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500')
@@ -86,7 +88,37 @@ def create_response_paraphrase(input_str, max_length,num_return_sequences):
86
  # results.append(line)
87
  # return results
88
  return result_output_str
89
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  interface_question_generation = gr.Interface(fn=create_response_question_generation,
92
  title="Question Generation",
@@ -118,9 +150,26 @@ interface_paraphrase = gr.Interface(fn=create_response_paraphrase,
118
  ],
119
  outputs="html"
120
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
 
123
-
124
- demo = gr.TabbedInterface([interface_question_generation, interface_paraphrase], ["Question Generation", "Paraphrase"])
125
 
126
  demo.launch()
 
24
  dt = datetime.datetime.now()
25
  print(dt)
26
  print("loading models")
27
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
28
+
29
  tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
30
  original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
31
  untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500')
 
88
  # results.append(line)
89
  # return results
90
  return result_output_str
91
+ import string
92
+ def contains_digit_or_punctuation(s):
93
+ return any(char.isdigit() or char in string.punctuation for char in s)
94
+ rake = Rake()
95
+ def create_response_keywords_extraction(input_str):
96
+ rake.extract_keywords_from_text(input_str)
97
+ keywords_with_scores = rake.get_ranked_phrases_with_scores()
98
+ filtered_keywords = []
99
+ seen_keywords = set()
100
+ for score, keyword in keywords_with_scores:
101
+ # Apply filters: score must be greater than 1, keyword must not contain digits or punctuation
102
+ if score > 1 and not contains_digit_or_punctuation(keyword) and keyword not in seen_keywords:
103
+ filtered_keywords.append((score, keyword))
104
+ seen_keywords.add(keyword)
105
+ output_string=""
106
+ for score, keyword in filtered_keywords:
107
+ #print(f"Score: {score}, Keyword: {keyword}")
108
+ output_string= output_string + f"Score: {score}, Keyword: {keyword} <br/>"
109
+
110
+ return output_string
111
+
112
+ def create_response_intention_classification(input_str):
113
+ labels = ["dialogue", "long content generation"]
114
+
115
+ # Perform classification
116
+ output_string=""
117
+ result = classifier(input_str, labels)
118
+ for label, score in zip(result["labels"], result["scores"]):
119
+ output_string= output_string + f"Label: {label}, Score: {score:.4f} <br/>"
120
+
121
+ return output_string
122
 
123
  interface_question_generation = gr.Interface(fn=create_response_question_generation,
124
  title="Question Generation",
 
150
  ],
151
  outputs="html"
152
  )
153
+ interface_extract_keywords = gr.Interface(fn=create_response_keywords_extraction,
154
+ title="Extract Keywords",
155
+ description="Extract Keywords ",
156
+ #examples=examples,
157
+ inputs=[
158
+ gr.Textbox(label="input text here", lines=3, value="It is truly a great cosmic paradox that one of the best teachers in all of life turns out to be death. No person or situation could ever teach you as much as death has to teach you. "),
159
+ ],
160
+ outputs="html"
161
+ )
162
+ interface_intention_classification = gr.Interface(fn=create_response_intention_classification,
163
+ title="Intention Classification",
164
+ description="Find if question intention is short dialog or long content generation. How are you? versus WWhat are the implications of quantum computing on global security? (difference not very dramatic as of now)",
165
+ #examples=examples,
166
+ inputs=[
167
+ gr.Textbox(label="input text here", lines=3, value="What are the implications of quantum computing on global security?"),
168
+ ],
169
+ outputs="html"
170
+ )
171
 
172
 
173
+ demo = gr.TabbedInterface([interface_question_generation, interface_paraphrase,interface_extract_keywords,interface_intention_classification], ["Question Generation", "Paraphrase", "Keywords Extraction", "Intention Classification"])
 
174
 
175
  demo.launch()