Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,34 @@
|
|
1 |
-
|
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 |
-
|
17 |
-
|
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 |
-
|
26 |
-
|
27 |
-
)
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
MAX_CHUNK = 6_000 # ≈ 4 k tokens
|
33 |
|
34 |
def summarize(txt: str) -> str:
|
35 |
-
|
|
|
36 |
partials = [
|
37 |
-
|
38 |
-
|
39 |
-
for p in parts
|
40 |
]
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|