Yurii Paniv commited on
Commit
a94d4e4
·
1 Parent(s): c2f5fd0

Handle new lines

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -21,23 +21,30 @@ model = MistralForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1",
21
  model = PeftModel.from_pretrained(model, "lang-uk/dragoman").to("cuda")
22
  tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", use_fast=False, add_bos_token=False)
23
 
24
- @spaces.GPU(duration=20)
25
  def translate(input_text):
26
- # iteratively generate
27
- input_text = input_text.strip()
28
- input_text = f"[INST] {input_text} [/INST]"
29
- inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
30
-
31
- streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
32
- generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=200)
33
-
34
- thread = Thread(target=model.generate, kwargs=generation_kwargs)
35
-
36
- thread.start()
37
-
38
  generated_text = ""
39
- for new_text in streamer:
40
- generated_text += new_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  yield generated_text
42
 
43
 
 
21
  model = PeftModel.from_pretrained(model, "lang-uk/dragoman").to("cuda")
22
  tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", use_fast=False, add_bos_token=False)
23
 
24
+ @spaces.GPU(duration=30)
25
  def translate(input_text):
 
 
 
 
 
 
 
 
 
 
 
 
26
  generated_text = ""
27
+ input_text = input_text.strip()
28
+ for chunk in input_text.split("\n"):
29
+ if not chunk:
30
+ generated_text += "\n"
31
+ yield generated_text
32
+ continue
33
+ chunk = f"[INST] {chunk} [/INST]"
34
+ inputs = tokenizer([chunk], return_tensors="pt").to(model.device)
35
+
36
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
37
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=200)
38
+
39
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
40
+
41
+ thread.start()
42
+
43
+ for new_text in streamer:
44
+ generated_text += new_text
45
+ yield generated_text
46
+
47
+ generated_text += "\n"
48
  yield generated_text
49
 
50