Update functionality.py
Browse files- functionality.py +18 -40
functionality.py
CHANGED
@@ -112,9 +112,9 @@ def get_collection() -> chromadb.Collection:
|
|
112 |
papers = pd.read_csv("hf://datasets/somosnlp-hackathon-2022/scientific_papers_en/scientific_paper_en.csv")
|
113 |
logging.info(f"The data downloaded from url.")
|
114 |
papers = papers.drop(['id'], axis=1)
|
115 |
-
papers = papers.iloc[:
|
116 |
|
117 |
-
for i in range(
|
118 |
paper = papers.iloc[i]
|
119 |
idx = paper.name
|
120 |
|
@@ -231,7 +231,7 @@ def _chat_stream(initial_text: str, parts: list):
|
|
231 |
)
|
232 |
inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
|
233 |
streamer = TextIteratorStreamer(
|
234 |
-
tokenizer=tokenizer, skip_prompt=True, timeout=
|
235 |
)
|
236 |
generation_kwargs = {
|
237 |
**inputs,
|
@@ -253,44 +253,22 @@ def predict(goal: str, parts: list, context: str):
|
|
253 |
logging.info("No context was provided!")
|
254 |
elif goal == 'Fix Academic Style':
|
255 |
formal_text = ""
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
yield formal_text
|
260 |
-
if not formal_text:
|
261 |
-
yield "Generation failed or timed out. Please try again!"
|
262 |
-
|
263 |
-
logging.info(f"\n---Academic style corrected:---\n {formal_text}\n")
|
264 |
-
except Exception as e:
|
265 |
-
logging.error(f"Error in 'Fix Academic Style' occured: {e}")
|
266 |
-
yield "Try to wait a little bit and resend your request!"
|
267 |
|
|
|
268 |
elif goal == 'Fix Grammar':
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
if not full_response:
|
276 |
-
yield "Generation failed or timed out. Please try again!"
|
277 |
-
|
278 |
-
logging.info(f"\n---Grammar corrected:---\n{full_response}\n")
|
279 |
-
except Exception as e:
|
280 |
-
logging.error(f"Error in 'Fix Grammar' occured: {e}")
|
281 |
-
yield "Try to wait a little bit and resend your request!"
|
282 |
-
|
283 |
else:
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
yield full_response
|
289 |
-
|
290 |
-
if not full_response:
|
291 |
-
yield "Generation failed or timed out. Please try again!"
|
292 |
|
293 |
-
|
294 |
-
except Exception as e:
|
295 |
-
logging.error(f"Error in 'Write Text' occured: {e}")
|
296 |
-
yield "Try to wait a little bit and resend your request!"
|
|
|
112 |
papers = pd.read_csv("hf://datasets/somosnlp-hackathon-2022/scientific_papers_en/scientific_paper_en.csv")
|
113 |
logging.info(f"The data downloaded from url.")
|
114 |
papers = papers.drop(['id'], axis=1)
|
115 |
+
papers = papers.iloc[:200]
|
116 |
|
117 |
+
for i in range(200):
|
118 |
paper = papers.iloc[i]
|
119 |
idx = paper.name
|
120 |
|
|
|
231 |
)
|
232 |
inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
|
233 |
streamer = TextIteratorStreamer(
|
234 |
+
tokenizer=tokenizer, skip_prompt=True, timeout=60.0, skip_special_tokens=True
|
235 |
)
|
236 |
generation_kwargs = {
|
237 |
**inputs,
|
|
|
253 |
logging.info("No context was provided!")
|
254 |
elif goal == 'Fix Academic Style':
|
255 |
formal_text = ""
|
256 |
+
for new_text in fix_academic_style(context):
|
257 |
+
formal_text = new_text
|
258 |
+
yield formal_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
|
260 |
+
logging.info(f"\n---Academic style corrected:---\n {formal_text}\n")
|
261 |
elif goal == 'Fix Grammar':
|
262 |
+
full_response = ""
|
263 |
+
for new_text in fix_grammar(context):
|
264 |
+
full_response = new_text
|
265 |
+
yield full_response
|
266 |
+
|
267 |
+
logging.info(f"\n---Grammar corrected:---\n{full_response}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
else:
|
269 |
+
full_response = ""
|
270 |
+
for new_text in _chat_stream(context, parts):
|
271 |
+
full_response = new_text
|
272 |
+
yield full_response
|
|
|
|
|
|
|
|
|
273 |
|
274 |
+
logging.info(f"\nThe text was generated!\n{full_response}")
|
|
|
|
|
|