Spaces:
Sleeping
Sleeping
Commit ·
21057c0
1
Parent(s): a4fc8e5
update: dyanmic input for call id, rsid, session id, transcript url
Browse files7 unmodified lines891011121112135 unmodified lines1920212422232624252627282930323132333435363738394041237 unmodified lines2792802812762822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273282803292823303313323333 unmodified lines3373383392922933403413423433443453462972983473483493503513523533543003553563573043583593603613623633643 unmodified lines36836937031431531631731831932032132232337132537237337437537637737837938038132933033133233333433533633733833938238338438538638738838939039139239339439522 unmodified lines4184194203694214223713723733743753763773783793804234244253844264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864873863874884894904914924934944954964974984995005013893905025035045055065075085095105115125135145155165175185195205215225235245253923935265275285295305315325335345355365375385392 unmodified lines5425435444024034044054064074084094104114124134144155455464175475485497 unmodified linesfrom bs4 import BeautifulSoupfrom openai import OpenAI
street_interview = False
@dataclassclass TranscriptSegment:5 unmodified lines
class TranscriptProcessor: def __init__(self, transcript_file: str): def __init__(self, transcript_file: str = None, transcript_data: dict = None): self.transcript_file = transcript_file self.transcript_data = None self.transcript_data = transcript_data self.formatted_transcript = None self.segments = [] self.text_windows = [] self.window_size = 2 self.speaker_mapping = {} if self.transcript_file: self._load_transcript() self._load_transcript() elif self.transcript_data: pass # transcript_data is already set else: 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_initial_analysis(transcript_processor: TranscriptProcessor) -> str: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 JSON return 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() # print("Transcript is: ", transcript) client = OpenAI() if street_interview: if ct == "si": # 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."Best 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 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...."""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...."""
print(prompt) completion = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful assistant."}, { "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 lines return "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 message: str, chat_history: List, transcript_processor: TranscriptProcessor, cid, rsid, origin, ct,) -> 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."""
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 lines return "Sorry, there was an error processing your request."
def create_chat_interface(transcript_processor: TranscriptProcessor):def create_chat_interface(): """Create and configure the chat interface."""
def respond(message: str, chat_history: List) -> Tuple[str, List]: if not message: return "", chat_history
bot_message = chat(message, chat_history, transcript_processor) new_history = list(chat_history) new_history.append((message, bot_message)) return "", new_history
with gr.Blocks() as demo: chatbot = gr.Chatbot() msg = gr.Textbox() clear = gr.ClearButton([msg, chatbot]) transcript_processor_state = gr.State() # maintain state of imp things call_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 error if 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 None return [ 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, ]
# Initialize with transcript analysis initial_analysis = get_initial_analysis(transcript_processor) 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 init_chat(): return [(None, initial_analysis)] 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_history
chatbot.value = init_chat() msg.submit(respond, [msg, chatbot], [msg, chatbot]) msg.submit( respond, [ msg, chatbot, transcript_processor_state, call_id_state, colab_id_state, ], [msg, chatbot], )
return demo
2 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 = create_chat_interface() demo.launch(share=True)
except Exception as e: print(f"Error starting application: {str(e)}") raiseapp.py+198-69