xTorch8 commited on
Commit
1096299
·
1 Parent(s): 5d23333

Add exceptiion handling

Browse files
Files changed (1) hide show
  1. app.py +38 -35
app.py CHANGED
@@ -10,43 +10,46 @@ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL, token = TOKEN)
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL, token = TOKEN)
11
 
12
  def summarize_text(text):
13
- chunk_size = MAX_TOKENS * 4
14
- overlap = chunk_size // 4
15
- step = chunk_size - overlap
16
- chunks = [text[i:i + chunk_size] for i in range(0, len(text), step)]
 
17
 
18
- summaries = []
19
- for chunk in chunks:
20
- inputs = tokenizer(chunk, return_tensors = "pt", truncation = True, max_length = 1024, padding = True)
21
- with torch.no_grad():
22
- summary_ids = model.generate(
23
- **inputs,
24
- max_length = 1500,
25
- length_penalty = 2.0,
26
- num_beams = 4,
27
- early_stopping = True
28
- )
29
- summary = tokenizer.decode(summary_ids[0], skip_special_tokens = True)
30
- summaries.append(summary)
31
 
32
- final_text = " ".join(summaries)
33
- summarization = final_text
34
- if len(final_text) > MAX_TOKENS:
35
- inputs = tokenizer(final_text, return_tensors = "pt", truncation = True, max_length = 1024, padding = True)
36
- with torch.no_grad():
37
- summary_ids = model.generate(
38
- **inputs,
39
- min_length = 300,
40
- max_length = 1500,
41
- length_penalty = 2.0,
42
- num_beams = 4,
43
- early_stopping = True
44
- )
45
- summarization = tokenizer.decode(summary_ids[0], skip_special_tokens = True)
46
- else:
47
- summarization = final_text
48
-
49
- return summarization
 
 
50
 
51
  demo = gr.Interface(
52
  fn = summarize_text,
 
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL, token = TOKEN)
11
 
12
  def summarize_text(text):
13
+ try:
14
+ chunk_size = MAX_TOKENS * 4
15
+ overlap = chunk_size // 4
16
+ step = chunk_size - overlap
17
+ chunks = [text[i:i + chunk_size] for i in range(0, len(text), step)]
18
 
19
+ summaries = []
20
+ for chunk in chunks:
21
+ inputs = tokenizer(chunk, return_tensors = "pt", truncation = True, max_length = 1024, padding = True)
22
+ with torch.no_grad():
23
+ summary_ids = model.generate(
24
+ **inputs,
25
+ max_length = 1500,
26
+ length_penalty = 2.0,
27
+ num_beams = 4,
28
+ early_stopping = True
29
+ )
30
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens = True)
31
+ summaries.append(summary)
32
 
33
+ final_text = " ".join(summaries)
34
+ summarization = final_text
35
+ if len(final_text) > MAX_TOKENS:
36
+ inputs = tokenizer(final_text, return_tensors = "pt", truncation = True, max_length = 1024, padding = True)
37
+ with torch.no_grad():
38
+ summary_ids = model.generate(
39
+ **inputs,
40
+ min_length = 300,
41
+ max_length = 1500,
42
+ length_penalty = 2.0,
43
+ num_beams = 4,
44
+ early_stopping = True
45
+ )
46
+ summarization = tokenizer.decode(summary_ids[0], skip_special_tokens = True)
47
+ else:
48
+ summarization = final_text
49
+
50
+ return summarization
51
+ except Exception as e:
52
+ return e
53
 
54
  demo = gr.Interface(
55
  fn = summarize_text,