Luigi commited on
Commit
7ac9e1f
·
1 Parent(s): b4e6021

summarization script

Browse files
Files changed (1) hide show
  1. summarize_transcript.py +109 -0
summarize_transcript.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Script to summarize transcript using Falcon-H1-Tiny-Multilingual model with SYCL acceleration.
4
+ """
5
+
6
+ import os
7
+ from llama_cpp import Llama
8
+ from huggingface_hub import hf_hub_download
9
+
10
+ def load_model():
11
+ """Load the model from Hugging Face Hub."""
12
+
13
+ # Initialize the model with SYCL support
14
+ llm = Llama.from_pretrained(
15
+ repo_id="Luigi/Falcon-H1-Tiny-Multilingual-100M-Instruct-GGUF",
16
+ filename="*IQ4_NL.gguf",
17
+ n_gpu_layers=-1, # Use all layers on GPU
18
+ seed=1337,
19
+ n_ctx=32768, # Context size
20
+ verbose=True,
21
+ n_batch=1024,
22
+ n_ubatch=512,
23
+ v_type=2,
24
+ k_type=2
25
+ )
26
+
27
+ return llm
28
+
29
+ def read_transcript(file_path):
30
+ """Read the transcript file."""
31
+ with open(file_path, 'r', encoding='utf-8') as f:
32
+ content = f.read()
33
+ return content
34
+
35
+ def summarize_transcript(llm, transcript, language='zh-TW'):
36
+ """Summarize the transcript using the loaded model."""
37
+ # Truncate the transcript to fit within the context window
38
+ # Account for the prompt tokens as well
39
+ max_transcript_length = 1000 # Leave room for prompt and response
40
+
41
+ if len(transcript) > max_transcript_length:
42
+ transcript = transcript[:max_transcript_length]
43
+ print(f"Transcript truncated to {max_transcript_length} characters to fit context window.")
44
+
45
+ # Use the model's chat format based on its template
46
+ if language == 'en':
47
+ messages = [
48
+ {"role": "system", "content": "You are a helpful assistant that summarizes transcripts."},
49
+ {"role": "user", "content": f"Please summarize the following transcript:\n\n{transcript}"}
50
+ ]
51
+ else: # Default to zh-TW
52
+ messages = [
53
+ {"role": "system", "content": "你是一個有助的助手,負責總結轉錄內容。"},
54
+ {"role": "user", "content": f"請總結以下內容:\n\n{transcript}"}
55
+ ]
56
+
57
+ # Generate the summary using chat completion
58
+ output = llm.create_chat_completion(
59
+ messages=messages,
60
+ max_tokens=512,
61
+ temperature=0.3,
62
+ top_p=0.9,
63
+ repeat_penalty=1.1,
64
+ stop=["<|end_of_text|>", "<|eot_id|>", "<|eom_id|>"]
65
+ )
66
+
67
+ llm.reset()
68
+
69
+ return output['choices'][0]['message']['content'].strip()
70
+
71
+ def main():
72
+ print("Loading Falcon-H1-Tiny-Multilingual model with SYCL acceleration...")
73
+
74
+ # Load the model
75
+ llm = load_model()
76
+
77
+ # Read the transcript
78
+ transcript_path = "/home/luigi/tiny-scribe/transcripts/short.txt"
79
+ transcript = read_transcript(transcript_path)
80
+
81
+ print("\nOriginal Transcript:")
82
+ print(transcript[:500] + "..." if len(transcript) > 500 else transcript)
83
+
84
+ # Summarize in Chinese (zh-TW)
85
+ print("\nGenerating Chinese (zh-TW) summary...")
86
+ chinese_summary = summarize_transcript(llm, transcript, language='zh-TW')
87
+ print("Chinese Summary:")
88
+ print(chinese_summary)
89
+
90
+ # Summarize in English
91
+ print("\nGenerating English summary...")
92
+ english_summary = summarize_transcript(llm, transcript, language='en')
93
+ print("English Summary:")
94
+ print(english_summary)
95
+
96
+ # Save summaries to files
97
+ with open("/home/luigi/tiny-scribe/chinese_summary.txt", 'w', encoding='utf-8') as f:
98
+ f.write(chinese_summary)
99
+
100
+ with open("/home/luigi/tiny-scribe/english_summary.txt", 'w', encoding='utf-8') as f:
101
+ f.write(english_summary)
102
+
103
+ print("\nSummaries saved to files.")
104
+
105
+ # Clean up
106
+ del llm
107
+
108
+ if __name__ == "__main__":
109
+ main()