|
import json |
|
import os |
|
from dataclasses import dataclass |
|
from typing import Dict, List |
|
|
|
import gradio as gr |
|
import requests |
|
from bs4 import BeautifulSoup |
|
from openai import OpenAI |
|
|
|
|
|
@dataclass |
|
class TranscriptSegment: |
|
speaker_id: str |
|
start_time: float |
|
end_time: float |
|
text: str |
|
speaker_name: str = "" |
|
|
|
|
|
class TranscriptProcessor: |
|
def __init__(self, transcript_file: str = None, transcript_data: dict = None): |
|
self.transcript_file = transcript_file |
|
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() |
|
elif self.transcript_data: |
|
pass |
|
else: |
|
raise ValueError( |
|
"Either transcript_file or transcript_data must be provided." |
|
) |
|
|
|
self._process_transcript() |
|
self.map_speaker_ids_to_names() |
|
|
|
def _load_transcript(self) -> None: |
|
"""Load the transcript JSON file.""" |
|
with open(self.transcript_file, "r") as f: |
|
self.transcript_data = json.load(f) |
|
|
|
def _format_time(self, seconds: float) -> str: |
|
"""Convert seconds to formatted time string (MM:SS).""" |
|
minutes = int(seconds // 60) |
|
seconds = int(seconds % 60) |
|
return f"{minutes:02d}:{seconds:02d}" |
|
|
|
def _process_transcript(self) -> None: |
|
"""Process the transcript into segments with speaker information and create a formatted version with timestamps.""" |
|
results = self.transcript_data["results"] |
|
|
|
|
|
for segment in results["speaker_labels"]["segments"]: |
|
speaker_id = segment.get("speaker_label", segment.get("speakerlabel", "")) |
|
speaker_id = ( |
|
speaker_id.replace("spk_", "").replace("spk", "") if speaker_id else "" |
|
) |
|
|
|
start_time = float(segment.get("start_time", 0)) |
|
end_time = float(segment.get("end_time", 0)) |
|
|
|
items = [ |
|
item |
|
for item in results["items"] |
|
if "start_time" in item |
|
and float(item["start_time"]) >= start_time |
|
and float(item["start_time"]) < end_time |
|
and item["type"] == "pronunciation" |
|
] |
|
|
|
words = [item["alternatives"][0]["content"] for item in items] |
|
if words: |
|
self.segments.append( |
|
TranscriptSegment( |
|
speaker_id=speaker_id, |
|
start_time=start_time, |
|
end_time=end_time, |
|
text=" ".join(words), |
|
) |
|
) |
|
|
|
formatted_segments = [] |
|
for seg in self.segments: |
|
start_time_str = self._format_time(seg.start_time) |
|
end_time_str = self._format_time(seg.end_time) |
|
formatted_segments.append( |
|
f"time_stamp: {start_time_str}-{end_time_str}\n" |
|
f"spk {seg.speaker_id}: {seg.text}\n" |
|
) |
|
|
|
self.formatted_transcript = "\n".join(formatted_segments) |
|
|
|
|
|
for i in range(len(self.segments)): |
|
|
|
window_segments = self.segments[i : i + self.window_size] |
|
combined_text = " ".join(seg.text for seg in window_segments) |
|
if window_segments: |
|
self.text_windows.append( |
|
{ |
|
"text": combined_text, |
|
"start_time": window_segments[0].start_time, |
|
"end_time": window_segments[-1].end_time, |
|
} |
|
) |
|
|
|
def map_speaker_ids_to_names(self) -> None: |
|
"""Map speaker IDs to names based on introductions in the transcript.""" |
|
try: |
|
|
|
transcript = self.formatted_transcript |
|
|
|
prompt = ( |
|
"Given the following transcript where speakers are identified as spk 0, spk 1, spk 2, etc., please map each spk ID to the speaker's name based on their introduction in the transcript. If no name is introduced for a speaker, keep it as spk_id. Return the mapping as a JSON object in the format {'spk_0': 'Speaker Name', 'spk_1': 'Speaker Name', ...}\n\n" |
|
f"Transcript:\n{transcript}" |
|
) |
|
|
|
client = OpenAI() |
|
|
|
completion = client.chat.completions.create( |
|
model="gpt-4o-mini", |
|
messages=[ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": prompt}, |
|
], |
|
temperature=0, |
|
) |
|
|
|
response_text = completion.choices[0].message.content.strip() |
|
try: |
|
self.speaker_mapping = json.loads(response_text) |
|
except json.JSONDecodeError: |
|
|
|
response_text = response_text[ |
|
response_text.find("{") : response_text.rfind("}") + 1 |
|
] |
|
try: |
|
self.speaker_mapping = json.loads(response_text) |
|
except json.JSONDecodeError: |
|
print("Error parsing speaker mapping JSON.") |
|
self.speaker_mapping = {} |
|
for segment in self.segments: |
|
spk_id = f"spk_{segment.speaker_id}" |
|
speaker_name = self.speaker_mapping.get(spk_id, spk_id) |
|
segment.speaker_name = speaker_name |
|
|
|
|
|
formatted_segments = [] |
|
for seg in self.segments: |
|
start_time_str = self._format_time(seg.start_time) |
|
end_time_str = self._format_time(seg.end_time) |
|
formatted_segments.append( |
|
f"time_stamp: {start_time_str}-{end_time_str}\n" |
|
f"{seg.speaker_name}: {seg.text}\n" |
|
) |
|
self.formatted_transcript = "\n".join(formatted_segments) |
|
|
|
except Exception as e: |
|
print(f"Error mapping speaker IDs to names: {str(e)}") |
|
self.speaker_mapping = {} |
|
|
|
def correct_speaker_mapping_with_agenda(self, url: str) -> None: |
|
"""Fetch agenda from a URL and correct the speaker mapping using OpenAI.""" |
|
try: |
|
|
|
response = requests.get(url) |
|
response.raise_for_status() |
|
html_content = response.text |
|
|
|
|
|
soup = BeautifulSoup(html_content, "html.parser") |
|
description_tag = soup.find( |
|
"script", {"type": "application/ld+json"} |
|
) |
|
agenda = "" |
|
|
|
if description_tag: |
|
|
|
json_data = json.loads(description_tag.string) |
|
if "description" in json_data: |
|
agenda = json_data["description"] |
|
else: |
|
print("Agenda description not found in the JSON metadata.") |
|
else: |
|
print("No structured data (ld+json) found.") |
|
|
|
if not agenda: |
|
print("No agenda found in the structured metadata. Trying meta tags.") |
|
|
|
|
|
meta_description = soup.find("meta", {"name": "description"}) |
|
agenda = meta_description["content"] if meta_description else "" |
|
|
|
if not agenda: |
|
print("No agenda found in any description tags.") |
|
return |
|
|
|
print(self.speaker_mapping) |
|
|
|
prompt = ( |
|
f"Given the original speaker mapping {self.speaker_mapping}, agenda:\n{agenda}, and the transcript: {self.formatted_transcript}\n\n" |
|
"Some speaker names in the mapping might have spelling errors or be incomplete." |
|
"Remember that the content in agenda is accurate and transcript can have errors so prioritize the spellings and names in the agenda content." |
|
"If the speaker name and introduction is similar to the agenda, update the speaker name in the mapping." |
|
"Please correct the names based on the agenda. Return the corrected mapping in JSON format as " |
|
"{'spk_0': 'Correct Name', 'spk_1': 'Correct Name', ...}." |
|
"You should only update the name if the name sounds very similar, or there is a good spelling overlap/ The Speaker Introduction matches the description of the Talk from Agends. If the name is totally unrelated, keep the original name." |
|
"You should always include all the speakers in the mapping from the original mapping, even if you don't update their names. i.e if there are 4 speakers in original mapping, new mapping should have 4 speakers always, ignore all the other spekaers in the agenda. I REPEAT DO NOT ADD OTHER NEW SPEAKERS IN THE MAPPING." |
|
) |
|
|
|
client = OpenAI() |
|
|
|
completion = client.chat.completions.create( |
|
model="gpt-4o-mini", |
|
messages=[ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": prompt}, |
|
], |
|
temperature=0, |
|
) |
|
|
|
response_text = completion.choices[0].message.content.strip() |
|
try: |
|
corrected_mapping = json.loads(response_text) |
|
except Exception: |
|
response_text = response_text[ |
|
response_text.find("{") : response_text.rfind("}") + 1 |
|
] |
|
try: |
|
corrected_mapping = json.loads(response_text) |
|
except json.JSONDecodeError: |
|
print( |
|
"Error parsing corrected speaker mapping JSON, keeping the original mapping." |
|
) |
|
corrected_mapping = self.speaker_mapping |
|
|
|
self.speaker_mapping = corrected_mapping |
|
print("Corrected Speaker Mapping:", self.speaker_mapping) |
|
|
|
|
|
for segment in self.segments: |
|
spk_id = f"spk_{segment.speaker_id}" |
|
segment.speaker_name = self.speaker_mapping.get(spk_id, spk_id) |
|
|
|
|
|
formatted_segments = [] |
|
for seg in self.segments: |
|
start_time_str = self._format_time(seg.start_time) |
|
end_time_str = self._format_time(seg.end_time) |
|
formatted_segments.append( |
|
f"time_stamp: {start_time_str}-{end_time_str}\n" |
|
f"{seg.speaker_name}: {seg.text}\n" |
|
) |
|
self.formatted_transcript = "\n".join(formatted_segments) |
|
|
|
except requests.exceptions.RequestException as e: |
|
print(f" ching agenda from URL: {str(e)}") |
|
except Exception as e: |
|
print(f"Error correcting speaker mapping: {str(e)}") |
|
|
|
def get_transcript(self) -> str: |
|
"""Return the formatted transcript with speaker names.""" |
|
return self.formatted_transcript |
|
|
|
def get_transcript_data(self) -> Dict: |
|
"""Return the raw transcript data.""" |
|
return self.transcript_data |
|
|
|
|
|
def setup_openai_key() -> None: |
|
"""Set up OpenAI API key from file.""" |
|
try: |
|
with open("api.key", "r") as f: |
|
os.environ["OPENAI_API_KEY"] = f.read().strip() |
|
except FileNotFoundError: |
|
print("Using ENV variable") |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
content_disposition = response.headers.get("Content-Disposition", "") |
|
if "attachment" in content_disposition: |
|
|
|
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() |
|
client = OpenAI() |
|
if "localhost" in origin: |
|
link_start = "http" |
|
else: |
|
link_start = "https" |
|
|
|
if ct == "si": |
|
prompt = f"""This is a transcript for a street interview. Transcript: {transcript} |
|
|
|
Your task is to analyze this street interview transcript and identify the final/best timestamps for each topic or question discussed. Here are the key rules: |
|
|
|
1. For any topic/answer that appears multiple times in the transcript (even partially): |
|
- The LAST occurrence is always considered the best version. If the same thing is said multiple times, the last time is the best, all previous times are considered as additional takes. |
|
- This includes cases where parts of an answer are scattered throughout the transcript |
|
- Even slight variations of the same answer should be tracked |
|
- List timestamps for ALL takes, with the final take highlighted as the best answer |
|
|
|
2. Introduction handling: |
|
- Question 1 is ALWAYS the speaker's introduction/self-introduction |
|
- If someone introduces themselves multiple times, use the last timestamp as best answer |
|
- Include all variations of how they state their name/background |
|
- List ALL introduction timestamps chronologically |
|
|
|
3. Question sequence: |
|
- After the introduction, list questions in the order they were first asked |
|
- If a question or introduction is revisited later at any point, please use the later timestamp |
|
- Track partial answers to the same question across the transcript |
|
|
|
You need to make sure that any words that are repeated, you need to pick the last of them. |
|
|
|
Return format: |
|
|
|
[Question Title] |
|
Total takes: [X] (Include ONLY if content appears more than once) |
|
- [Take 1. <div id='topic' style="display: inline"> 15s at 12:30 </div>]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{750}}&et={{765}}) |
|
- [Take 2. <div id='topic' style="display: inline"> 30s at 14:45 </div>]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{885}}&et={{915}}) |
|
... |
|
- [Take X (Best). <div id='topic' style="display: inline"> 1m 10s at 16:20 </div>]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{980}}&et={{1050}}) |
|
|
|
URL formatting: |
|
- Convert timestamps to seconds (e.g., 10:13 → 613) |
|
- Format: {link_start}://[origin]/colab/[cid]/[rsid]?st=[start_seconds]&et=[end_seconds] |
|
- Parameters after RSID must start with ? and subsequent parameters use & |
|
|
|
Example: |
|
1. Introduction |
|
Total takes: 2 |
|
- [Take 1. <div id='topic' style="display: inline"> 10s at 09:45]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{585}}&et={{595}}) |
|
- [Take 1. <div id='topic' style="display: inline"> 20s at 25:45]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{1245}}&et={{1265}}) |
|
- [Take 3 (Best). <div id='topic' style="display: inline"> 5s at 10:13 </div>]({link_start}://roll.ai/colab/1234aq_12314/51234151?st=613&et=618)""" |
|
else: |
|
prompt = f"""<div id="initial_message"> Given the transcript {transcript}, analyze all speakers' discussions and list out people, news, events, trends, and sources mentioned. For each speaker, provide at least 3 topics that would make engaging social media clips. Include timestamp and duration for each topic. |
|
|
|
Format the output as follows: |
|
|
|
**Speaker Name** |
|
1. [Topic title <div id='topic' style="display: inline"> 12s at 12:30 </div>]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{750}}&et={{762}}) |
|
2. [Topic title <div id='topic' style="display: inline"> 33s at 14:45 </div>]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{885}}&et={{918}}) |
|
3. [Topic title <div id='topic' style="display: inline"> 1m 10s at 16:20 </div>]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{980}}&et={{1050}}) |
|
4. [Topic title <div id='topic' style="display: inline"> 25s at 14:00 </div>]({link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{840}}&et={{865}}) |
|
|
|
Requirements: |
|
- Only speaker names should be bold using markdown **Name** |
|
- Topic titles should be in normal case (i.e first word of Sentence capital rest small) |
|
- Use timestamp and duration format: HH:MM (Xs) where X is seconds |
|
- Each line should be a clickable link including timestamp and title |
|
- When selecting timestamps, STRICTLY limit the start and end times to contain only the current speaker's continuous dialogue. The timestamp should begin exactly when the current speaker starts talking about that specific topic and end when they finish their point or another speaker begins. Do not include any portions of previous or subsequent speakers' dialogue in the selected time range. |
|
- IN NO CASE should the duration include any overlapping dialogue from other speakers. i.e if you chose 1m 10s at 16:20, 16:20 till 17:30 should only contain the current speaker's dialogue. THIS IS IMPORTANT. |
|
- Convert display timestamps to seconds for URL parameters |
|
Example: 21s at 10:13 in URL would be st=613&et=634 |
|
|
|
URL format: {link_start}://{{origin}}/collab/{{cid}}/{{rsid}}?st={{start_time_in_sec}}&et={{end_time_in_sec}} |
|
|
|
Rank topics based on their potential virality and engagement for social media clips. </div> |
|
""" |
|
|
|
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}, |
|
], |
|
stream=True, |
|
temperature=0.5, |
|
) |
|
|
|
collected_messages = [] |
|
|
|
for chunk in completion: |
|
if chunk.choices[0].delta.content is not None: |
|
chunk_message = chunk.choices[0].delta.content |
|
collected_messages.append(chunk_message) |
|
|
|
yield "".join(collected_messages) |
|
|
|
except Exception as e: |
|
print(f"Error in initial analysis: {str(e)}") |
|
yield "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: |
|
tools = [ |
|
{ |
|
"type": "function", |
|
"function": { |
|
"name": "correct_speaker_name_with_url", |
|
"description": "If a User provides a link to Agenda file, call the correct_speaker_name_with_url function to correct the speaker names based on the url, i.e if a user says 'Here is the Luma link for the event' and provides a link to the event, the function will correct the speaker names based on the event.", |
|
"parameters": { |
|
"type": "object", |
|
"properties": { |
|
"url": { |
|
"type": "string", |
|
"description": "The url to the agenda.", |
|
}, |
|
}, |
|
"required": ["url"], |
|
"additionalProperties": False, |
|
}, |
|
}, |
|
}, |
|
{ |
|
"type": "function", |
|
"function": { |
|
"name": "correct_call_type", |
|
"description": "If the user tells you the correct call type, you have to apologize and call this function with correct call type.", |
|
"parameters": { |
|
"type": "object", |
|
"properties": { |
|
"call_type": { |
|
"type": "string", |
|
"description": "The correct call type. If street interview, call type is 'si'.", |
|
}, |
|
}, |
|
"required": ["call_type"], |
|
"additionalProperties": False, |
|
}, |
|
}, |
|
}, |
|
] |
|
|
|
try: |
|
client = OpenAI() |
|
|
|
if "localhost" in origin: |
|
link_start = "http" |
|
else: |
|
link_start = "https" |
|
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: |
|
If the user provides a link to the agenda, use the correct_speaker_name_with_url function to correct the speaker names based on the agenda. |
|
If the user provides the correct call type, use the correct_call_type function to correct the call type. Call Type for street interviews is 'si'. |
|
Answer format: |
|
Topic: Heading [Timestamp: start_time - end_time]({link_start}://{{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: |
|
{link_start}://roll.ai/colab/1234aq_12314/51234151?st=613&et=618 |
|
In the URL, make sure that after RSID there is ? and then rest of the fields are added via &. |
|
""" |
|
messages = [{"role": "system", "content": prompt}] |
|
|
|
for user_msg, assistant_msg in chat_history: |
|
if user_msg is not None: |
|
messages.append({"role": "user", "content": user_msg}) |
|
if assistant_msg is not None: |
|
messages.append({"role": "assistant", "content": assistant_msg}) |
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
completion = client.chat.completions.create( |
|
model="gpt-4o-mini", messages=messages, tools=tools, stream=True |
|
) |
|
collected_messages = [] |
|
tool_calls_detected = False |
|
|
|
for chunk in completion: |
|
if chunk.choices[0].delta.tool_calls: |
|
tool_calls_detected = True |
|
|
|
response = client.chat.completions.create( |
|
model="gpt-4o-mini", |
|
messages=messages, |
|
tools=tools, |
|
) |
|
|
|
if response.choices[0].message.tool_calls: |
|
tool_call = response.choices[0].message.tool_calls[0] |
|
if tool_call.function.name == "correct_speaker_name_with_url": |
|
args = eval(tool_call.function.arguments) |
|
url = args.get("url", None) |
|
if url: |
|
transcript_processor.correct_speaker_mapping_with_agenda( |
|
url |
|
) |
|
corrected_speaker_mapping = ( |
|
transcript_processor.speaker_mapping |
|
) |
|
function_call_result_message = { |
|
"role": "tool", |
|
"content": json.dumps( |
|
{"speaker_mapping": f"Corrected Speaker Mapping..."} |
|
), |
|
"name": tool_call.function.name, |
|
"tool_call_id": tool_call.id, |
|
} |
|
messages.append(function_call_result_message) |
|
|
|
|
|
final_response = client.chat.completions.create( |
|
model="gpt-4o-mini", messages=messages, stream=True |
|
) |
|
|
|
|
|
for final_chunk in final_response: |
|
if final_chunk.choices[0].delta.content: |
|
yield final_chunk.choices[0].delta.content |
|
return |
|
|
|
elif tool_call.function.name == "correct_call_type": |
|
args = eval(tool_call.function.arguments) |
|
call_type = args.get("call_type", None) |
|
if call_type: |
|
|
|
for content in get_initial_analysis( |
|
transcript_processor, call_type, rsid, origin, call_type |
|
): |
|
yield content |
|
return |
|
break |
|
|
|
if not tool_calls_detected and chunk.choices[0].delta.content is not None: |
|
chunk_message = chunk.choices[0].delta.content |
|
collected_messages.append(chunk_message) |
|
yield "".join(collected_messages) |
|
|
|
except Exception as e: |
|
print(f"Unexpected error in chat: {str(e)}") |
|
import traceback |
|
|
|
print(f"Traceback: {traceback.format_exc()}") |
|
yield "Sorry, there was an error processing your request." |
|
|
|
|
|
def create_chat_interface(): |
|
"""Create and configure the chat interface.""" |
|
css = """ |
|
.gradio-container { |
|
|
|
padding-top: 0px !important; |
|
padding-left: 0px !important; |
|
padding-right: 0px !important; |
|
padding: 0px !important; |
|
margin: 0px !important; |
|
} |
|
#component-0 { |
|
gap: 0px !important; |
|
} |
|
|
|
.icon-button-wrapper{ |
|
display: none !important; |
|
} |
|
|
|
|
|
footer { |
|
display: none !important; |
|
} |
|
#chatbot_box{ |
|
flex-grow: 1 !important; |
|
border-width: 0px !important; |
|
} |
|
|
|
#link-frame { |
|
position: absolute !important; |
|
width: 1px !important; |
|
height: 1px !important; |
|
right: -100px !important; |
|
bottom: -100px !important; |
|
display: none !important; |
|
} |
|
.html-container { |
|
display: none !important; |
|
} |
|
a { |
|
text-decoration: none !important; |
|
} |
|
#topic { |
|
color: #aaa !important; |
|
} |
|
.bubble-wrap { |
|
padding-top: 0px !important; |
|
} |
|
.message-content { |
|
border: 0px !important; |
|
margin: 5px !important; |
|
} |
|
.message-row { |
|
border-style: none !important; |
|
margin: 0px !important; |
|
width: 100% !important; |
|
max-width: 100% !important; |
|
} |
|
.flex-wrap { |
|
border-style: none !important; |
|
} |
|
|
|
.panel-full-width { |
|
border-style: none !important; |
|
border-width: 0px !important; |
|
} |
|
ol { |
|
list-style-position: outside; |
|
margin-left: 20px; |
|
} |
|
""" |
|
js = """ |
|
function createIframeHandler() { |
|
let iframe = document.getElementById('link-frame'); |
|
if (!iframe) { |
|
iframe = document.createElement('iframe'); |
|
iframe.id = 'link-frame'; |
|
iframe.style.position = 'absolute'; |
|
iframe.style.width = '1px'; |
|
iframe.style.height = '1px'; |
|
iframe.style.right = '-100px'; |
|
iframe.style.bottom = '-100px'; |
|
iframe.style.display = 'none'; // Hidden initially |
|
document.body.appendChild(iframe); |
|
} |
|
|
|
document.addEventListener('click', function (event) { |
|
var link = event.target.closest('a'); |
|
if (link && link.href) { |
|
try { |
|
iframe.src = link.href; |
|
iframe.style.display = 'block'; // Show iframe on link click |
|
event.preventDefault(); |
|
console.log('Opening link in iframe:', link.href); |
|
} catch (error) { |
|
console.error('Failed to open link in iframe:', error); |
|
} |
|
} |
|
}); |
|
|
|
return 'Iframe handler initialized'; |
|
} |
|
""" |
|
|
|
with gr.Blocks( |
|
fill_height=True, |
|
fill_width=True, |
|
css=css, |
|
js=js, |
|
theme=gr.themes.Default( |
|
font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"] |
|
), |
|
) as demo: |
|
chatbot = gr.Chatbot( |
|
elem_id="chatbot_box", |
|
layout="bubble", |
|
show_label=False, |
|
show_share_button=False, |
|
show_copy_all_button=False, |
|
show_copy_button=False, |
|
) |
|
msg = gr.Textbox(elem_id="chatbot_textbox", show_label=False) |
|
transcript_processor_state = gr.State() |
|
call_id_state = gr.State() |
|
colab_id_state = gr.State() |
|
origin_state = gr.State() |
|
ct_state = gr.State() |
|
turl_state = gr.State() |
|
iframe_html = "<iframe id='link-frame'></iframe>" |
|
gr.HTML(value=iframe_html) |
|
|
|
def respond( |
|
message: str, |
|
chat_history: List, |
|
transcript_processor, |
|
cid, |
|
rsid, |
|
origin, |
|
ct, |
|
): |
|
if not transcript_processor: |
|
bot_message = "Transcript processor not initialized." |
|
chat_history.append((message, bot_message)) |
|
return "", chat_history |
|
|
|
chat_history.append((message, "")) |
|
for chunk in chat( |
|
message, |
|
chat_history[:-1], |
|
transcript_processor, |
|
cid, |
|
rsid, |
|
origin, |
|
ct, |
|
): |
|
chat_history[-1] = (message, chunk) |
|
yield "", chat_history |
|
|
|
msg.submit( |
|
respond, |
|
[ |
|
msg, |
|
chatbot, |
|
transcript_processor_state, |
|
call_id_state, |
|
colab_id_state, |
|
origin_state, |
|
ct_state, |
|
], |
|
[msg, chatbot], |
|
) |
|
|
|
|
|
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) |
|
|
|
required_params = ["cid", "rsid", "origin", "ct", "turl"] |
|
missing_params = [ |
|
param |
|
for param in required_params |
|
if request.query_params.get(param) is None |
|
] |
|
|
|
if missing_params: |
|
error_message = ( |
|
f"Missing required parameters: {', '.join(missing_params)}" |
|
) |
|
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 |
|
) |
|
|
|
|
|
chatbot_value = [(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, |
|
] |
|
|
|
def stream_initial_analysis( |
|
chatbot_value, transcript_processor, cid, rsid, origin, ct |
|
): |
|
if transcript_processor: |
|
for chunk in get_initial_analysis( |
|
transcript_processor, cid, rsid, origin, ct |
|
): |
|
chatbot_value[0] = (None, chunk) |
|
yield chatbot_value |
|
else: |
|
yield chatbot_value |
|
|
|
|
|
demo.load( |
|
on_app_load, |
|
inputs=None, |
|
outputs=[ |
|
chatbot, |
|
transcript_processor_state, |
|
call_id_state, |
|
colab_id_state, |
|
origin_state, |
|
ct_state, |
|
turl_state, |
|
], |
|
).then( |
|
stream_initial_analysis, |
|
inputs=[ |
|
chatbot, |
|
transcript_processor_state, |
|
call_id_state, |
|
colab_id_state, |
|
origin_state, |
|
ct_state, |
|
], |
|
outputs=[chatbot], |
|
) |
|
|
|
return demo |
|
|
|
|
|
def main(): |
|
"""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 |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|