XINZHANG-Geotab commited on
Commit
c3ad4b8
·
verified ·
1 Parent(s): 364f00c

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +93 -24
app.py CHANGED
@@ -2,34 +2,87 @@ import gradio as gr
2
  import whisper
3
  from langchain_openai import ChatOpenAI
4
  from utils import RefineDataSummarizer
 
 
 
 
 
 
5
  import os
6
 
7
- def transcript(file_dir, language, model_type):
8
- model_dir = os.path.join('models', model_type)
9
- model = whisper.load_model(model_dir)
10
- result = model.transcribe(file_dir, language=language, task='transcribe')
11
 
12
- lines = [s['text'] for s in result['segments']]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  text = ''
14
- for line in lines:
15
- text += f"{line}\n"
16
- return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
 
19
  def upload_file(file_paths):
20
  return file_paths
21
 
22
 
23
- def summary(text, chunk_num, chunk_overlap, user_api, llm_type):
24
  if user_api == "Not Provided":
25
- api_key = os.getenv("openai_api")
 
26
  else:
27
  api_key = user_api
28
  api_key = api_key.strip()
29
  llm = ChatOpenAI(temperature=1, openai_api_key=api_key, model_name=llm_type)
30
- rds = RefineDataSummarizer(llm=llm)
31
  result = rds.get_summarization(text, chunk_num=chunk_num, chunk_overlap=chunk_overlap)
32
- return result["output_text"]
 
 
 
33
 
34
  with gr.Blocks() as demo:
35
  with gr.Row(equal_height=False):
@@ -37,8 +90,6 @@ with gr.Blocks() as demo:
37
  file_output = gr.File()
38
  upload_button = gr.UploadButton("Click to Upload a File", file_types=["audio", "video"], file_count="single")
39
  upload_button.upload(upload_file, upload_button, file_output)
40
- language = gr.Dropdown(
41
- ["English", "Chinese"], label="Transcript Language", value="English")
42
  model_type = gr.Dropdown(
43
  [
44
  "tiny.en.pt",
@@ -50,15 +101,29 @@ with gr.Blocks() as demo:
50
  "medium.en.pt",
51
  "medium.pt",
52
  "large-v1.pt",
53
- "large-v2.pt",], label="Model Type", value="medium.en.pt")
 
54
  TranscriptButton = gr.Button("Transcript", variant="primary")
 
 
 
55
 
56
  with gr.Column():
57
- transcript_text = gr.Textbox(placeholder="Transcript Result", label="Transcript")
58
-
59
- with gr.Accordion(open=False, label=["summary settings"]):
60
  chunk_num = gr.Number(precision=0, minimum=1, maximum=9999, step=1, label="Chunk Number", value=1)
61
- chunk_overlap = gr.Number(precision=0, minimum=1, maximum=9999, step=1, label="Chunk Overlap", value=100)
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  with gr.Accordion(open=False, label=["llm settings"]):
64
  user_api = gr.Textbox(placeholder="If Empty, Use Default Key", label="Your API Key", value="Not Provided")
@@ -70,16 +135,18 @@ with gr.Blocks() as demo:
70
  ], label="LLM Type", value="gpt-4-1106-preview")
71
  SunmmaryButton = gr.Button("Summary", variant="primary")
72
  summary_text = gr.Textbox(placeholder="Summary Result", label="Summary")
 
 
73
 
74
 
75
  TranscriptButton.click(
76
  fn=transcript,
77
  inputs=[
78
  file_output,
79
- language,
80
- model_type
81
  ],
82
- outputs=[transcript_text]
83
  )
84
  SunmmaryButton.click(
85
  fn=summary,
@@ -88,9 +155,11 @@ with gr.Blocks() as demo:
88
  chunk_num,
89
  chunk_overlap,
90
  user_api,
91
- llm_type
 
 
92
  ],
93
- outputs=[summary_text]
94
  )
95
 
96
  demo.launch()
 
2
  import whisper
3
  from langchain_openai import ChatOpenAI
4
  from utils import RefineDataSummarizer
5
+ from utils import (
6
+ prompt_template,
7
+ refine_template,
8
+ prompt_template_bullet_point,
9
+ refine_prompt_template_bullet_point
10
+ )
11
  import os
12
 
 
 
 
 
13
 
14
+ def get_prompt_examples():
15
+ examples=[
16
+ ["Regular Template: ", prompt_template, refine_template],
17
+ ["Bullet Point Template: ", prompt_template_bullet_point, refine_prompt_template_bullet_point],
18
+ ["Empty Template: ", '{text}', '{text}'],
19
+ ]
20
+ return examples
21
+
22
+
23
+ def convert_to_time_format(seconds_float):
24
+ # Split the input into whole seconds and fractional part (milliseconds)
25
+ seconds, milliseconds = divmod(seconds_float, 1)
26
+ milliseconds = round(milliseconds * 1000) # Convert fractional part to milliseconds
27
+
28
+ # Convert the whole seconds into hours, minutes, and remaining seconds
29
+ minutes, seconds = divmod(int(seconds), 60)
30
+ hours, minutes = divmod(minutes, 60)
31
+
32
+ # Format the time components into HH:MM:SS:OO
33
+ time_format = f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"
34
+
35
+ return time_format
36
+
37
+
38
+ def time_stamped_text(transcript_result):
39
  text = ''
40
+ for idx, segment in enumerate(transcript_result['segments']):
41
+ start_stamp = segment["start"]
42
+ end_stamp = segment["end"]
43
+ sentence = segment["text"].strip()
44
+ text += f"{idx + 1}\n"
45
+ text += f"{convert_to_time_format(start_stamp)} --> {convert_to_time_format(end_stamp)}\n{sentence}\n\n"
46
+ return text.strip()
47
+
48
+
49
+ def transcript(file_dir, model_type, time_stamp):
50
+ # model_dir = os.path.join('models', model_type)
51
+ model_dir = "E:\\Whisper\\" + model_type
52
+ model = whisper.load_model(model_dir)
53
+ result = model.transcribe(file_dir, language='English', task='transcribe')
54
+
55
+ if time_stamp:
56
+ text = time_stamped_text(result)
57
+ else:
58
+ lines = [s['text'] for s in result['segments']]
59
+ text = ''
60
+ for line in lines:
61
+ text += f"{line}\n"
62
+ text = text.strip()
63
+ with open("Transcript.txt", 'w') as file:
64
+ file.write(text)
65
+ return [text, "Transcript.txt"]
66
 
67
 
68
  def upload_file(file_paths):
69
  return file_paths
70
 
71
 
72
+ def summary(text, chunk_num, chunk_overlap, user_api, llm_type, prompt, refine_prompt):
73
  if user_api == "Not Provided":
74
+ # api_key = os.getenv("openai_api")
75
+ api_key = "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"
76
  else:
77
  api_key = user_api
78
  api_key = api_key.strip()
79
  llm = ChatOpenAI(temperature=1, openai_api_key=api_key, model_name=llm_type)
80
+ rds = RefineDataSummarizer(llm=llm, prompt_template=prompt, refine_template=refine_prompt)
81
  result = rds.get_summarization(text, chunk_num=chunk_num, chunk_overlap=chunk_overlap)
82
+ text = result["output_text"]
83
+ with open("Summary.txt", 'w') as file:
84
+ file.write(text)
85
+ return [text, "Summary.txt"]
86
 
87
  with gr.Blocks() as demo:
88
  with gr.Row(equal_height=False):
 
90
  file_output = gr.File()
91
  upload_button = gr.UploadButton("Click to Upload a File", file_types=["audio", "video"], file_count="single")
92
  upload_button.upload(upload_file, upload_button, file_output)
 
 
93
  model_type = gr.Dropdown(
94
  [
95
  "tiny.en.pt",
 
101
  "medium.en.pt",
102
  "medium.pt",
103
  "large-v1.pt",
104
+ "large-v2.pt",], label="Model Type", value="medium.pt")
105
+ time_stamp = gr.Checkbox(label="SRT Format", info="SRT format with timestamps")
106
  TranscriptButton = gr.Button("Transcript", variant="primary")
107
+ transcript_text = gr.Textbox(placeholder="Transcript Result", label="Transcript")
108
+ with gr.Accordion(open=False, label=["Download Transcript"]):
109
+ transcript_file = gr.File()
110
 
111
  with gr.Column():
112
+ with gr.Accordion(open=True, label=["summary settings"]):
 
 
113
  chunk_num = gr.Number(precision=0, minimum=1, maximum=9999, step=1, label="Chunk Number", value=1)
114
+ chunk_overlap = gr.Number(precision=0, minimum=1, maximum=9999, step=1, label="Chunk Overlap", value=100)
115
+ placeholder = gr.Textbox(visible=False)
116
+ prompt = gr.Textbox(placeholder="summary prompt", label="Summary Template", lines=5, value=prompt_template)
117
+ refine_prompt = gr.Textbox(placeholder="refine summary prompt", label="Refine Summary Template", lines=10, value=refine_template)
118
+ with gr.Accordion(open=False, label=["Templates"]):
119
+ gr.Examples(
120
+ examples=get_prompt_examples(),
121
+ inputs=[placeholder, prompt, refine_prompt],
122
+ fn=None,
123
+ outputs=None,
124
+ cache_examples=False,
125
+ label="Prompt Template"
126
+ )
127
 
128
  with gr.Accordion(open=False, label=["llm settings"]):
129
  user_api = gr.Textbox(placeholder="If Empty, Use Default Key", label="Your API Key", value="Not Provided")
 
135
  ], label="LLM Type", value="gpt-4-1106-preview")
136
  SunmmaryButton = gr.Button("Summary", variant="primary")
137
  summary_text = gr.Textbox(placeholder="Summary Result", label="Summary")
138
+ with gr.Accordion(open=False, label=["Download Summary"]):
139
+ summary_file = gr.File()
140
 
141
 
142
  TranscriptButton.click(
143
  fn=transcript,
144
  inputs=[
145
  file_output,
146
+ model_type,
147
+ time_stamp
148
  ],
149
+ outputs=[transcript_text, transcript_file]
150
  )
151
  SunmmaryButton.click(
152
  fn=summary,
 
155
  chunk_num,
156
  chunk_overlap,
157
  user_api,
158
+ llm_type,
159
+ prompt,
160
+ refine_prompt
161
  ],
162
+ outputs=[summary_text, summary_file]
163
  )
164
 
165
  demo.launch()