import csv import datetime import requests import gradio as gr import pandas as pd from io import BytesIO from pathlib import Path from urllib.parse import urlparse from pydub import AudioSegment, silence def format_seconds(secs): t = datetime.datetime( year=1, month=1, day=1, hour=0, minute=0 ) + datetime.timedelta(seconds=secs) return t.strftime("%M:%S.%f")[:-3] def get_filename_and_extension(url): parsed_url = urlparse(url) path = parsed_url.path filename = Path(path).name filename_without_extension = Path(filename).stem file_extension = Path(filename).suffix return filename, filename_without_extension, file_extension def calculate_times(input_url, input_text, ms_before, ms_after): _, _, file_extension = get_filename_and_extension(input_url) file_extension = file_extension.replace(".", "") res = requests.get(input_url) audio = AudioSegment.from_file(BytesIO(res.content), file_extension) non_silent_parts = silence.detect_nonsilent( audio, min_silence_len=1250, silence_thresh=-80 ) segments = [ ( format_seconds((start - ms_before) / 1000), format_seconds((stop + ms_after) / 1000), ) for start, stop in non_silent_parts ] lines = input_text.splitlines() if len(lines) != len(segments): raise gr.Error("Detected clips and input lines do not match!") else: df = pd.DataFrame({"text": [], "start": [], "stop": [], "file": []}) res = [] for i in range(len(segments)): line = lines[i].rstrip() res.append(f"{line}\t{segments[i][0]}\t{segments[i][1]}\t{input_url}") df.loc[len(df.index)] = [line, segments[i][0], segments[i][1], input_url] df.to_csv('clips.tsv', sep="\t", encoding='utf-8', index=False, header=False, quoting=csv.QUOTE_NONE) return 'clips.tsv', input_url, "\n".join(res), df app = gr.Interface( fn=calculate_times, inputs=[ gr.Textbox(lines=1, placeholder="Video URL...", label="Video URL"), gr.Textbox(lines=10, placeholder="Lines...", label="Lines"), gr.Slider( minimum=0, maximum=1000, step=50, value=0, label="Milliseconds BEFORE each clip", ), gr.Slider( minimum=0, maximum=1000, step=50, value=500, label="Milliseconds AFTER each clip", ), ], outputs=[ gr.File(label="Clips", show_label=True), gr.Video(format="mp4", label="Video file", show_label=True), gr.Textbox(lines=10, label="Clips", interactive=False, show_copy_button=True), gr.Dataframe( label="Clips", headers=["text", "start", "stop", "file"], datatype=["str", "str", "str", "str"], row_count=0, ), ], # examples=[ # [ # "https://simulation-temporary-test.s3.amazonaws.com/SanofiFluCoach.mp4", # 'Are you ready to jump into a practice? Just say "Lets Practice"\nIf you\'d like to return to the menu, say "Main Menu"\nMain Menu\nPlease say or click one of the choices above\nWelcome back.\nGreat to have you back.\nIt\'s a pleasure to have you back\nWelcome home.\nWould you like to hop on another call? Or do you need to hop off?\nYou can also just return to the main menu.\nDo you have time for another practice?\nLooks like you didn\'t make it all the way through the call.\nYou\'ll need to start over next time, but it will only take a few minutes.\nYou can choose to do it again now, or you can return to the main menu.\nYou hung up on that call in a hurry.\nDo you want to try again now or return to the main menu?\nAre you ready to view your results?\nYour results are ready. Let\'s take a look. Say or click "show my results"\nThe results are in. Say or click "show my results" to take a look.\nReady to see how you did? Say or click "show my results" to check it out.\nYour results are now available. Say or click "show my results"\nWell, I think those were fair questions, and you\'re likely to face those from other HCP\'s as well. So it pays off to be ready. Let\'s see how you did. Click Show my results\nWere you ready for those questions? By exercising your response muscles, you\'ll be even more ready next time questions like these come up.\nDo you want an overview of Simulator? Or, tips on how-to practice?', # 0, # 0, # ], # [ # "https://simulation-temporary-test.s3.amazonaws.com/SanofiVisAidAvatarPoppy.mp4", # "Let's get started! You will soon be connected to Dr. Poppy Thompson, an AI physician who is interested in this new drug. Be prepared to discuss all areas of the Beyfortus Visual Aid and to answer any questions she might have. I will give you a few moments to prepare. When you are ready to start the call, just say \"Let's Go\".\nHi, great to see you! I understand you are here to talk to me about your new RSV product. Why don't you jump in? What is it, and what is it for?\nThat's interesting. Why do all infants need protection against RSV?\nUnderstood, so will the protection offered by Beyfortus last for the entire RSV season?\nTell me about the clinical trials – can you give me a broad overview of what was measured?\nThanks, can we dive deeper into Trial 4? What were the results of the primary endpoints, what do I need to take away here?\nI noticed that Beyfortus didn't meet the secondary endpoint of hospitalization in Trial 4. Can you please elaborate on that?\nThanks, let's also take a closer look at Trial three. What were the results of the primary endpoints, what do I need to take-away here?\nThat all sounds great, but is Beyfortus safe? Were there any adverse reactions?\nSo, I know RSV is seasonal, and Beyfortus offers 5 months of protection. How can I fit this into my immunization schedule?\nWhat do I need to know from an administration standpoint?\nThinking through some of the practical considerations, how is Beyfortus administered? How is Beyfortus stored, and what is its shelf-life?\nThanks for your time today – it's definitely very interesting.\nThis has been so helpful. I really appreciate your time today. I do think this makes sense – let's determine next steps to make sure we're protecting all infants from RSV by using Beyfortus. Take care!", # 0, # 0, # ], # ], ) app.launch()