Aneeshmishra commited on
Commit
d1f836f
·
verified ·
1 Parent(s): 283ceb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -40
app.py CHANGED
@@ -1,49 +1,34 @@
1
- import os, textwrap, torch, gradio as gr
2
- import torch, textwrap, gradio as gr
3
- from transformers import (
4
- AutoTokenizer,
5
- AutoModelForCausalLM,
6
- BitsAndBytesConfig,
7
- pipeline,
8
- )
9
- AUTH = os.environ.get("HF_TOKEN")
10
- MODEL_ID = "mistralai/Mixtral-8x7B-Instruct-v0.1" # FP16 weights
11
- bnb_cfg = BitsAndBytesConfig(
12
- load_in_4bit=True,
13
- bnb_4bit_compute_dtype=torch.float16, # keeps mat-mul fast
14
- )
15
 
16
- tok = AutoTokenizer.from_pretrained(MODEL_ID, token=AUTH, use_fast=True)
17
- model = AutoModelForCausalLM.from_pretrained(
18
- MODEL_ID,
19
- token=AUTH,
20
- device_map="auto",
21
- trust_remote_code=True,
22
- quantization_config=bnb_cfg, # perfectly fine here
23
- )
24
 
25
- prompt_tpl = (
26
- "Summarise the following transcript in short in 1 or 2 paragraph and point wise and don't miss any key information cover all"
27
- )
28
 
29
- gen = pipeline("text-generation", model=model, tokenizer=tok,
30
- max_new_tokens=256, temperature=0.3)
31
-
32
- MAX_CHUNK = 6_000 # ≈ 4 k tokens
33
 
34
  def summarize(txt: str) -> str:
35
- parts = textwrap.wrap(txt, MAX_CHUNK, break_long_words=False)
 
36
  partials = [
37
- gen(prompt_tpl.format(chunk=p))[0]["generated_text"]
38
- .split("### Summary:")[-1].strip()
39
- for p in parts
40
  ]
41
- return gen(prompt_tpl.format(chunk=" ".join(partials)))[0]["generated_text"]\
42
- .split("### Summary:")[-1].strip()
 
 
 
 
 
 
 
 
 
 
43
 
44
- demo = gr.Interface(fn=summarize,
45
- inputs=gr.Textbox(lines=20, label="Transcript"),
46
- outputs="text",
47
- title="Free Transcript Summariser – Mixtral-8×7B")
48
  if __name__ == "__main__":
49
- demo.launch()
 
1
+ # app.py – CPU-only summariser for Hugging Face Spaces (free tier)
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import textwrap, gradio as gr
4
+ from transformers import pipeline
 
 
 
 
 
 
5
 
6
+ # 1️⃣ small, open model that needs no access-token
7
+ MODEL_ID = "sshleifer/distilbart-cnn-12-6"
8
+ summariser = pipeline("summarization", model=MODEL_ID, device=-1) # -1 = CPU
9
 
10
+ # 2️⃣ rough char limit that maps to the model’s 1 024-token window
11
+ MAX_CHUNK = 3_500
 
 
12
 
13
  def summarize(txt: str) -> str:
14
+ """Chunk long transcripts, summarise each, then summarise the summaries."""
15
+ chunks = textwrap.wrap(txt, MAX_CHUNK, break_long_words=False)
16
  partials = [
17
+ summariser(c, max_length=160, min_length=30, do_sample=False)[0]["summary_text"]
18
+ for c in chunks
 
19
  ]
20
+ first_pass = " ".join(partials)
21
+ # if we had to chunk, do a second pass to get a coherent overall summary
22
+ if len(chunks) > 1:
23
+ first_pass = summariser(first_pass, max_length=180, min_length=40, do_sample=False)[0]["summary_text"]
24
+ return first_pass
25
+
26
+ demo = gr.Interface(
27
+ fn=summarize,
28
+ inputs=gr.Textbox(lines=20, label="Transcript"),
29
+ outputs="text",
30
+ title="Free Transcript Summariser – DistilBART-CNN",
31
+ )
32
 
 
 
 
 
33
  if __name__ == "__main__":
34
+ demo.launch()