Spaces:
Sleeping
Sleeping
Commit ·
21057c0
1
Parent(s): a4fc8e5
update: dyanmic input for call id, rsid, session id, transcript url
Browse files7 unmodified lines891011121314155 unmodified lines212223242526272829303132333435237 unmodified lines2732742752762772782792802812822832842853 unmodified lines2892902912922932942952962972982993003013023033043053063073 unmodified lines31131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234322 unmodified lines3663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953962 unmodified lines3994004014024034044054064074084094104114124134144154164174184194207 unmodified linesfrom bs4 import BeautifulSoupfrom openai import OpenAIstreet_interview = False@dataclassclass TranscriptSegment:5 unmodified linesclass TranscriptProcessor:def __init__(self, transcript_file: str):self.transcript_file = transcript_fileself.transcript_data = Noneself.formatted_transcript = Noneself.segments = []self.text_windows = []self.window_size = 2self.speaker_mapping = {}self._load_transcript()self._process_transcript()self.map_speaker_ids_to_names()237 unmodified lines# )def get_initial_analysis(transcript_processor: TranscriptProcessor) -> str:"""Perform initial analysis of the transcript using OpenAI."""try:transcript = transcript_processor.get_transcript()# print("Transcript is: ", transcript)client = OpenAI()if street_interview:prompt = f"""This is a transcript for a street interview. Transcript: {transcript}In this street interview, the host asks multiple questions to the interviewees.The interviewee can repeat a single answer multiple time to get the best take.3 unmodified linesReturn format is:1. Question: questionNumber of takes: numberBest Answer timestamp: start_time - end_timeYou can visit the call segment on this URL: https://roll.ai/call_id/colab_id?starttime=start_time?endtime=end_time.""""else:prompt = f"""Given the transcript {transcript}, For All the speakers, short list all people, news, events, trends, and source that are discussed by speakers along with the start time of that topic and end time of that topic from the transcript. Rank all topics based on what would make for the best social clips. I need atleast 3 topics per speaker.You should mention the Speaker Name first, then 3 posts with their timestamps, and so on.Return format is: Speaker Name\n1.Topic: topic, Start Time: start_time, End Time: end_time\n2...."""print(prompt)completion = client.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": prompt},],)3 unmodified linesreturn "An error occurred during initial analysis. Please check your API key and file path."call_id = "20240226t210135"colab_id = "1231412431212"def generate_call_link(start_time: str) -> str:"""Generate a link to the call at a specific timestamp."""formatted_time = start_time.replace(":", ".")return f"https://roll.ai/{call_id}/{colab_id}?t={formatted_time}"def chat(message: str, chat_history: List, transcript_processor: TranscriptProcessor) -> str:try:client = OpenAI()# if street_interview:# prompt = f"""You are a helpful assistant analyzing transcripts and generating timestamps and URL. Call ID is {call_id} and Colab ID is {colab_id}.# Transcript: {transcript_processor.get_transcript()}# If a user asks t# """# else:prompt = f"""You are a helpful assistant analyzing transcripts and generating timestamps and URL. Call ID is {call_id} and Colab ID is {colab_id}.Transcript: {transcript_processor.get_transcript()}If a user asks timestamps for a specific topic, find the start time and end time of that specific topic and return answer in the format: 'Timestamp: start_time - end_time'.You can visit the call segment on this URL: https://roll.ai/call_id/colab_id?starttime=start_time?endtime=end_time."If a user requests a link to a specific segment topic, generate a link to that segment using the following format: https://roll.ai/call_id/colab_id?starttime=start_time?endtime=end_time."""messages = [{"role": "system", "content": prompt}]for user_msg, assistant_msg in chat_history:22 unmodified linesreturn "Sorry, there was an error processing your request."def create_chat_interface(transcript_processor: TranscriptProcessor):"""Create and configure the chat interface."""def respond(message: str, chat_history: List) -> Tuple[str, List]:if not message:return "", chat_historybot_message = chat(message, chat_history, transcript_processor)new_history = list(chat_history)new_history.append((message, bot_message))return "", new_historywith gr.Blocks() as demo:chatbot = gr.Chatbot()msg = gr.Textbox()clear = gr.ClearButton([msg, chatbot])# Initialize with transcript analysisinitial_analysis = get_initial_analysis(transcript_processor)def init_chat():return [(None, initial_analysis)]chatbot.value = init_chat()msg.submit(respond, [msg, chatbot], [msg, chatbot])return demo2 unmodified lines"""Main function to run the application."""try:setup_openai_key()current_dir = os.path.dirname(os.path.abspath(__file__))transcript_file = os.path.join(current_dir, "step_take19AWS.json")if not os.path.exists(transcript_file):raise FileNotFoundError("Transcript file not found. Please check the file path.")transcript_processor = TranscriptProcessor(transcript_file)transcript_processor.correct_speaker_mapping_with_agenda("https://lu.ma/STEPSF24")demo = create_chat_interface(transcript_processor)demo.launch(share=True)except Exception as e:print(f"Error starting application: {str(e)}")raise7 unmodified lines89101112135 unmodified lines1920212223242526272829303132333435363738394041237 unmodified lines2792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333 unmodified lines3373383393403413423433443453463473483493503513523533543553563573583593603613623633643 unmodified lines36836937037137237337437537637737837938038138238338438538638738838939039139239339439522 unmodified lines4184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385392 unmodified lines5425435445455465475485497 unmodified linesfrom bs4 import BeautifulSoupfrom openai import OpenAI@dataclassclass TranscriptSegment:5 unmodified linesclass TranscriptProcessor:def __init__(self, transcript_file: str = None, transcript_data: dict = None):self.transcript_file = transcript_fileself.transcript_data = transcript_dataself.formatted_transcript = Noneself.segments = []self.text_windows = []self.window_size = 2self.speaker_mapping = {}if self.transcript_file:self._load_transcript()elif self.transcript_data:pass # transcript_data is already setelse:raise ValueError("Either transcript_file or transcript_data must be provided.")self._process_transcript()self.map_speaker_ids_to_names()237 unmodified lines# )def get_transcript_for_url(url: str) -> dict:"""This function fetches the transcript data for a signed URL.If the URL results in a direct download, it processes the downloaded content.:param url: Signed URL for the JSON file:return: Parsed JSON data as a dictionary"""headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}try:response = requests.get(url, headers=headers)response.raise_for_status()if "application/json" in response.headers.get("Content-Type", ""):return response.json() # Parse and return JSON directly# If not JSON, assume it's a file download (e.g., content-disposition header)content_disposition = response.headers.get("Content-Disposition", "")if "attachment" in content_disposition:# Process the content as JSONreturn json.loads(response.content)return json.loads(response.content)except requests.exceptions.HTTPError as http_err:print(f"HTTP error occurred: {http_err}")except requests.exceptions.RequestException as req_err:print(f"Request error occurred: {req_err}")except json.JSONDecodeError as json_err:print(f"JSON decoding error: {json_err}")return {}def get_initial_analysis(transcript_processor: TranscriptProcessor,cid,rsid,origin,ct,) -> str:"""Perform initial analysis of the transcript using OpenAI."""try:transcript = transcript_processor.get_transcript()client = OpenAI()if ct == "si": # street interviewprompt = f"""This is a transcript for a street interview. Transcript: {transcript}In this street interview, the host asks multiple questions to the interviewees.The interviewee can repeat a single answer multiple time to get the best take.3 unmodified linesReturn format is:1. Question: questionNumber of takes: numberBest Answer timestamp: [Timestamp: start_time - end_time](https://{{origin}}/collab/{{cid}}/{{rsid}}&st={{start_time_in_sec}}&et={{end_time_in_sec}}"').For Example:If the start time is 10:13 and end time is 10:18, the url will be:https://roll.ai/colab/1234aq_12314/51234151?st=613&et=618"""else:prompt = f"""Given the transcript {transcript}, For All the speakers, short list all people, news, events, trends, and source that are discussed by speakers along with the start time of that topic and end time of that topic from the transcript. Rank all topics based on what would make for the best social clips. I need atleast 3 topics per speaker.You should mention the Speaker Name first, then atleast 3 posts with their timestamps, and so on.Return format is:Speaker Name1.Topic: topic,[Timestamp: start_time - end_time](https://{{origin}}/collab/{{cid}}/{{rsid}}&st={{start_time_in_sec}}&et={{end_time_in_sec}}"').2...."""completion = client.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system","content": f"You are a helpful assistant who is analyzing the transcript. The transcript is for Call ID: {cid}, Session ID: {rsid}, Origin: {origin}, Call Type: {ct}.",},{"role": "user", "content": prompt},],)3 unmodified linesreturn "An error occurred during initial analysis. Please check your API key and file path."def chat(message: str,chat_history: List,transcript_processor: TranscriptProcessor,cid,rsid,origin,ct,) -> str:try:client = OpenAI()prompt = f"""You are a helpful assistant analyzing transcripts and generating timestamps and URL. Call ID is {cid}, Session ID is {rsid}, origin is {origin}, Call Type is {ct}.Transcript:\n{transcript_processor.get_transcript()}If a user asks timestamps for a specific topic, find the start time and end time of that specific topic and return answer in the format:Answer format:Topic: Heading [Timestamp: start_time - end_time](https://{{origin}}/collab/{{cid}}/{{rsid}}&st={{start_time_in_sec}}&et={{end_time_in_sec}}"').For Example:If the start time is 10:13 and end time is 10:18, the url will be:https://roll.ai/colab/1234aq_12314/51234151?st=613&et=618"""messages = [{"role": "system", "content": prompt}]for user_msg, assistant_msg in chat_history:22 unmodified linesreturn "Sorry, there was an error processing your request."def create_chat_interface():"""Create and configure the chat interface."""with gr.Blocks() as demo:chatbot = gr.Chatbot()msg = gr.Textbox()transcript_processor_state = gr.State() # maintain state of imp thingscall_id_state = gr.State()colab_id_state = gr.State()origin_state = gr.State()ct_state = gr.State()turl_state = gr.State()def on_app_load(request: gr.Request):cid = request.query_params.get("cid", None)rsid = request.query_params.get("rsid", None)origin = request.query_params.get("origin", None)ct = request.query_params.get("ct", None)turl = request.query_params.get("turl", None)# if any param is missing, return errorif not cid or not rsid or not origin or not ct or not turl:error_message = "Error processing"chatbot_value = [(None, error_message)]return [chatbot_value,None,None,None,None,None,None,]try:transcript_data = get_transcript_for_url(turl)transcript_processor = TranscriptProcessor(transcript_data=transcript_data)initial_analysis = get_initial_analysis(transcript_processor, cid, rsid, origin, ct)chatbot_value = [(None, initial_analysis)] # initialized with initial analysis and assistant is Nonereturn [chatbot_value,transcript_processor,cid,rsid,origin,ct,turl,]except Exception as e:error_message = f"Error processing call_id {cid}: {str(e)}"chatbot_value = [(None, error_message)]return [chatbot_value,None,None,None,None,None,None,]demo.load(on_app_load,inputs=None,outputs=[chatbot,transcript_processor_state,call_id_state,colab_id_state,origin_state,ct_state,turl_state,],)def respond(message: str,chat_history: List,transcript_processor,cid,rsid,origin,ct,):if not transcript_processor:bot_message = "Transcript processor not initialized."else:bot_message = chat(message,chat_history,transcript_processor,cid,rsid,origin,ct,)chat_history.append((message, bot_message))return "", chat_historymsg.submit(respond,[msg,chatbot,transcript_processor_state,call_id_state,colab_id_state,],[msg, chatbot],)return demo2 unmodified lines"""Main function to run the application."""try:setup_openai_key()demo = create_chat_interface()demo.launch(share=True)except Exception as e:print(f"Error starting application: {str(e)}")raise
app.py+198-69