File size: 7,097 Bytes
ae46fe0
 
 
d72a50f
ae46fe0
c3ad4b8
 
 
 
 
 
ae46fe0
 
 
c3ad4b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae46fe0
c3ad4b8
 
 
 
 
 
 
 
 
 
6390a01
 
c3ad4b8
 
 
 
 
 
 
 
 
 
 
 
 
 
ae46fe0
 
 
 
 
 
d72a50f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ad4b8
ae46fe0
c3ad4b8
 
 
 
ae46fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3ad4b8
 
ae46fe0
c3ad4b8
 
 
ae46fe0
 
c3ad4b8
0010e43
c3ad4b8
 
 
 
 
 
 
 
 
 
 
 
 
0010e43
 
d72a50f
 
 
 
 
 
 
0010e43
 
d72a50f
 
 
 
 
0010e43
ae46fe0
 
c3ad4b8
 
ae46fe0
 
 
 
 
 
c3ad4b8
 
ae46fe0
c3ad4b8
ae46fe0
 
 
 
 
 
 
d72a50f
c3ad4b8
 
 
ae46fe0
c3ad4b8
ae46fe0
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import gradio as gr
import whisper
from langchain_openai import ChatOpenAI
from langchain_openai import AzureChatOpenAI
from utils import RefineDataSummarizer
from utils import (
    prompt_template,
    refine_template,
    prompt_template_bullet_point,
    refine_prompt_template_bullet_point
)
import os


def get_prompt_examples():
    examples=[
        ["Regular Template: ", prompt_template, refine_template],
        ["Bullet Point Template: ", prompt_template_bullet_point, refine_prompt_template_bullet_point],
        ["Empty Template: ", '{text}', '{text}'],
    ]
    return examples


def convert_to_time_format(seconds_float):
    # Split the input into whole seconds and fractional part (milliseconds)
    seconds, milliseconds = divmod(seconds_float, 1)
    milliseconds = round(milliseconds * 1000)  # Convert fractional part to milliseconds

    # Convert the whole seconds into hours, minutes, and remaining seconds
    minutes, seconds = divmod(int(seconds), 60)
    hours, minutes = divmod(minutes, 60)

    # Format the time components into HH:MM:SS:OO
    time_format = f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"
    
    return time_format


def time_stamped_text(transcript_result):
    text = ''
    for idx, segment in enumerate(transcript_result['segments']):
        start_stamp = segment["start"]
        end_stamp = segment["end"]
        sentence = segment["text"].strip()
        text += f"{idx + 1}\n"
        text += f"{convert_to_time_format(start_stamp)} --> {convert_to_time_format(end_stamp)}\n{sentence}\n\n"
    return text.strip()
    

def transcript(file_dir, model_type, time_stamp):
    model_dir = os.path.join('models', model_type)
    # model_dir = "E:\\Whisper\\" + model_type
    model = whisper.load_model(model_dir)
    result = model.transcribe(file_dir, language='English', task='transcribe')

    if time_stamp:
        text = time_stamped_text(result)
    else:
        lines = [s['text'] for s in result['segments']]
        text = ''
        for line in lines:
            text += f"{line}\n"
    text = text.strip()
    with open("Transcript.txt", 'w') as file:
        file.write(text)
    return [text, "Transcript.txt"]


def upload_file(file_paths):
    return file_paths


def summary(text, chunk_num, chunk_overlap, llm_type, prompt, refine_prompt):
    #if user_api == "Not Provided":
        # api_key = os.getenv("openai_api")
        #deployment_name = llm_type
    #else:
    #    api_key = user_api
    #api_key = api_key.strip()
    # llm = ChatOpenAI(temperature=1, openai_api_key=api_key, model_name=llm_type)

    os.environ["AZURE_OPENAI_API_KEY"] = os.getenv("azure_api")
    os.environ["AZURE_OPENAI_ENDPOINT"] =  os.getenv("azure_endpoint")
    openai_api_version=os.getenv("azure_api_version")
    deployment_name = llm_type

    llm = AzureChatOpenAI(
        openai_api_version=openai_api_version,
        azure_deployment=deployment_name
    )

    rds = RefineDataSummarizer(llm=llm, prompt_template=prompt, refine_template=refine_prompt)
    result = rds.get_summarization(text, chunk_num=chunk_num, chunk_overlap=chunk_overlap)
    text = result["output_text"]
    with open("Summary.txt", 'w') as file:
        file.write(text)
    return [text, "Summary.txt"]

with gr.Blocks() as demo:
    with gr.Row(equal_height=False):
        with gr.Column():
            file_output = gr.File()
            upload_button = gr.UploadButton("Click to Upload a File", file_types=["audio", "video"], file_count="single")
            upload_button.upload(upload_file, upload_button, file_output)
            model_type = gr.Dropdown(
                [
                    "tiny.en.pt", 
                    "tiny.pt", 
                    "small.en.pt", 
                    "small.pt",
                    "base.en.pt", 
                    "base.pt",
                    "medium.en.pt", 
                    "medium.pt",
                    "large-v1.pt", 
                    "large-v2.pt",], label="Model Type", value="medium.pt")
            time_stamp = gr.Checkbox(label="SRT Format", info="SRT format with timestamps")
            TranscriptButton = gr.Button("Transcript", variant="primary")
            transcript_text = gr.Textbox(placeholder="Transcript Result", label="Transcript")
            with gr.Accordion(open=False, label=["Download Transcript"]):
                transcript_file = gr.File()
        
        with gr.Column():
            with gr.Accordion(open=True, label=["summary settings"]):
                chunk_num = gr.Number(precision=0, minimum=1, maximum=9999, step=1, label="Chunk Number", value=1)
                chunk_overlap = gr.Number(precision=0, minimum=1, maximum=9999, step=1, label="Chunk Overlap", value=100)
                placeholder = gr.Textbox(visible=False)
                prompt = gr.Textbox(placeholder="summary prompt", label="Summary Template", lines=5, value=prompt_template)
                refine_prompt = gr.Textbox(placeholder="refine summary prompt", label="Refine Summary Template", lines=10, value=refine_template)
                with gr.Accordion(open=False, label=["Templates"]):
                    gr.Examples(
                            examples=get_prompt_examples(),
                            inputs=[placeholder, prompt, refine_prompt],
                            fn=None,
                            outputs=None,
                            cache_examples=False,
                            label="Prompt Template"
                    )       

            with gr.Accordion(open=False, label=["llm settings"]):
                # user_api = gr.Textbox(placeholder="If Empty, Use Default Key", label="Your API Key", value="Not Provided")
                # llm_type =  gr.Dropdown(
                #     [
                #     "gpt-3.5-turbo", 
                #     "gpt-3.5-turbo-16k", 
                #     "gpt-4-1106-preview"
                #     ], label="LLM Type", value="gpt-4-1106-preview")
                llm_type =  gr.Dropdown(
                    [
                        "gpt-4-32k",
                        "gpt-4",
                        "gpt-4-1106-preview",
                        "gpt-35-turbo", 
                        "gpt-35-turbo-16k"
                    ], label="LLM Type", value="gpt-4-1106-preview")
            SunmmaryButton = gr.Button("Summary", variant="primary")
            summary_text = gr.Textbox(placeholder="Summary Result", label="Summary")
            with gr.Accordion(open=False, label=["Download Summary"]):
                summary_file = gr.File()


    TranscriptButton.click(
        fn=transcript, 
        inputs=[
            file_output,
            model_type,
            time_stamp
        ],
        outputs=[transcript_text, transcript_file]
    )
    SunmmaryButton.click(
        fn=summary, 
        inputs=[
            transcript_text,
            chunk_num,
            chunk_overlap,
            #user_api,
            llm_type,
            prompt,
            refine_prompt
        ],
        outputs=[summary_text, summary_file]
    )

demo.launch()