BeardedMonster commited on
Commit
83f3921
·
verified ·
1 Parent(s): 1c1cae4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -102,7 +102,7 @@ popular_topics = [
102
  "Philosophy", "Religion", "Society", "World"
103
  ]
104
 
105
- def extract_answer(text):
106
  pattern = r'[a-z][A-Z]'
107
  result = re.split(pattern, text)[0]
108
  result = text[:len(result) + 1]
@@ -115,14 +115,14 @@ async def assign_topic(generated_text, topic_list=popular_topics):
115
  return topic.title()
116
  return generated_text
117
 
118
- def count_sentences(text):
119
  # Split the text based on sentence-ending punctuation followed by a space or end of string
120
  sentences = re.split(r'[.!?]+\s*', text.strip())
121
  # Filter out any empty strings from the resulting list
122
  sentences = [sentence for sentence in sentences if sentence]
123
  return len(sentences), sentences
124
 
125
- def wrap_text(text, task_value):
126
  tasks = ["<classify>", "<prompt>", "<clean>", "<title>", "<diacritize>", "<translate>"]
127
  if any(task in text for task in tasks):
128
  return text
@@ -271,7 +271,7 @@ else:
271
  if st.button("Generate"):
272
  if user_input:
273
  with st.spinner("Please wait..."):
274
- wrapped_input = wrap_text(user_input, task_value)
275
  # print("wrapped_input: ", wrapped_input)
276
  generation_config["max_new_tokens"]= min(max_new_tokens, 1024 - len(tokenizer.tokenize(wrapped_input)))
277
  start_time = time.time()
@@ -307,14 +307,17 @@ if st.button("Generate"):
307
 
308
  elif task == "Translation" or "<translate>" in wrapped_input:
309
  # print("split for translation: ", n_sentences, re.split(r"\.|\n", generated_text)[:n_sentences])
310
- n_sentences, split_= count_sentences(initial_input)
311
  print(n_sentences, split_)
312
- _, generated_text = count_sentences(generated_text)
313
  generated_text = ". ".join(generated_text[:n_sentences]) + "."
314
 
315
  elif task == "Question Generation" or "Question Generation:" in sample_text:
316
  if "?" in generated_text:
317
  generated_text = "? ".join(re.split(r"\?", generated_text)[:-1]) + "?"
 
 
 
318
 
319
 
320
  full_output = st.empty()
 
102
  "Philosophy", "Religion", "Society", "World"
103
  ]
104
 
105
+ async def extract_answer(text):
106
  pattern = r'[a-z][A-Z]'
107
  result = re.split(pattern, text)[0]
108
  result = text[:len(result) + 1]
 
115
  return topic.title()
116
  return generated_text
117
 
118
+ async def count_sentences(text):
119
  # Split the text based on sentence-ending punctuation followed by a space or end of string
120
  sentences = re.split(r'[.!?]+\s*', text.strip())
121
  # Filter out any empty strings from the resulting list
122
  sentences = [sentence for sentence in sentences if sentence]
123
  return len(sentences), sentences
124
 
125
+ async def wrap_text(text, task_value):
126
  tasks = ["<classify>", "<prompt>", "<clean>", "<title>", "<diacritize>", "<translate>"]
127
  if any(task in text for task in tasks):
128
  return text
 
271
  if st.button("Generate"):
272
  if user_input:
273
  with st.spinner("Please wait..."):
274
+ wrapped_input = asyncio.run(wrap_text(user_input, task_value))
275
  # print("wrapped_input: ", wrapped_input)
276
  generation_config["max_new_tokens"]= min(max_new_tokens, 1024 - len(tokenizer.tokenize(wrapped_input)))
277
  start_time = time.time()
 
307
 
308
  elif task == "Translation" or "<translate>" in wrapped_input:
309
  # print("split for translation: ", n_sentences, re.split(r"\.|\n", generated_text)[:n_sentences])
310
+ n_sentences, split_= asyncio.run(count_sentences(initial_input))
311
  print(n_sentences, split_)
312
+ _, generated_text = asyncio.run(count_sentences(generated_text))
313
  generated_text = ". ".join(generated_text[:n_sentences]) + "."
314
 
315
  elif task == "Question Generation" or "Question Generation:" in sample_text:
316
  if "?" in generated_text:
317
  generated_text = "? ".join(re.split(r"\?", generated_text)[:-1]) + "?"
318
+
319
+ elif task == "Question-Answering" or "Question-Answering:" in sample_text:
320
+ generated_text = asyncio.run(extract_answer(generated_text)
321
 
322
 
323
  full_output = st.empty()