Spaces:
Sleeping
Sleeping
Commit
·
adf4e91
1
Parent(s):
0f97b90
update: Remote Production Calls
Browse files
app.py
CHANGED
@@ -1,386 +1,11 @@
|
|
1 |
import json
|
2 |
-
import
|
3 |
-
from dataclasses import dataclass
|
4 |
-
from typing import Dict, Generator, List
|
5 |
|
6 |
import gradio as gr
|
7 |
-
import requests
|
8 |
-
from bs4 import BeautifulSoup
|
9 |
from openai import OpenAI
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
class TranscriptSegment:
|
14 |
-
speaker_id: str
|
15 |
-
start_time: float
|
16 |
-
end_time: float
|
17 |
-
text: str
|
18 |
-
speaker_name: str = ""
|
19 |
-
|
20 |
-
|
21 |
-
class TranscriptProcessor:
|
22 |
-
def __init__(
|
23 |
-
self,
|
24 |
-
transcript_file: str = None,
|
25 |
-
transcript_data: dict = None,
|
26 |
-
max_segment_duration: int = None,
|
27 |
-
call_type: str = "le",
|
28 |
-
):
|
29 |
-
self.transcript_file = transcript_file
|
30 |
-
self.transcript_data = transcript_data
|
31 |
-
self.formatted_transcript = None
|
32 |
-
self.segments = []
|
33 |
-
self.speaker_mapping = {}
|
34 |
-
self.max_segment_duration = max_segment_duration
|
35 |
-
if self.transcript_file:
|
36 |
-
self._load_transcript()
|
37 |
-
elif self.transcript_data:
|
38 |
-
pass # transcript_data is already set
|
39 |
-
else:
|
40 |
-
raise ValueError(
|
41 |
-
"Either transcript_file or transcript_data must be provided."
|
42 |
-
)
|
43 |
-
|
44 |
-
self._process_transcript()
|
45 |
-
self._create_formatted_transcript() # Create initial formatted transcript
|
46 |
-
if call_type != "si":
|
47 |
-
self.map_speaker_ids_to_names()
|
48 |
-
|
49 |
-
def _load_transcript(self) -> None:
|
50 |
-
"""Load the transcript JSON file."""
|
51 |
-
with open(self.transcript_file, "r") as f:
|
52 |
-
self.transcript_data = json.load(f)
|
53 |
-
|
54 |
-
def _format_time(self, seconds: float) -> str:
|
55 |
-
"""Convert seconds to formatted time string (MM:SS)."""
|
56 |
-
minutes = int(seconds // 60)
|
57 |
-
seconds = int(seconds % 60)
|
58 |
-
return f"{minutes:02d}:{seconds:02d}"
|
59 |
-
|
60 |
-
def _process_transcript(self) -> None:
|
61 |
-
results = self.transcript_data["results"]
|
62 |
-
current_words = []
|
63 |
-
current_speaker = None
|
64 |
-
current_start = None
|
65 |
-
current_items = []
|
66 |
-
|
67 |
-
for item in results["items"]:
|
68 |
-
if item["type"] == "pronunciation":
|
69 |
-
speaker = (
|
70 |
-
item.get("speaker_label", "").replace("spk_", "").replace("spk", "")
|
71 |
-
)
|
72 |
-
|
73 |
-
# Initialize on first pronunciation item
|
74 |
-
if current_speaker is None:
|
75 |
-
current_speaker = speaker
|
76 |
-
current_start = float(item["start_time"])
|
77 |
-
|
78 |
-
# Check for speaker change
|
79 |
-
if speaker != current_speaker:
|
80 |
-
if current_items:
|
81 |
-
self._create_segment(
|
82 |
-
current_speaker,
|
83 |
-
current_start,
|
84 |
-
float(item["start_time"]),
|
85 |
-
current_items,
|
86 |
-
)
|
87 |
-
current_items = []
|
88 |
-
current_words = []
|
89 |
-
current_speaker = speaker
|
90 |
-
current_start = float(item["start_time"])
|
91 |
-
|
92 |
-
current_items.append(item)
|
93 |
-
current_words.append(item["alternatives"][0]["content"])
|
94 |
-
elif item["type"] == "punctuation":
|
95 |
-
current_items.append(item)
|
96 |
-
# Only check for segment break if we're over 20 words
|
97 |
-
if len(current_words) >= 20:
|
98 |
-
# Break on this punctuation
|
99 |
-
next_item = next(
|
100 |
-
(
|
101 |
-
it
|
102 |
-
for it in results["items"][
|
103 |
-
results["items"].index(item) + 1 :
|
104 |
-
]
|
105 |
-
if it["type"] == "pronunciation"
|
106 |
-
),
|
107 |
-
None,
|
108 |
-
)
|
109 |
-
if next_item:
|
110 |
-
self._create_segment(
|
111 |
-
current_speaker,
|
112 |
-
current_start,
|
113 |
-
float(next_item["start_time"]),
|
114 |
-
current_items,
|
115 |
-
)
|
116 |
-
current_items = []
|
117 |
-
current_words = []
|
118 |
-
current_start = float(next_item["start_time"])
|
119 |
-
|
120 |
-
# Don't forget the last segment
|
121 |
-
if current_items:
|
122 |
-
last_time = max(
|
123 |
-
float(item["end_time"])
|
124 |
-
for item in current_items
|
125 |
-
if item["type"] == "pronunciation"
|
126 |
-
)
|
127 |
-
self._create_segment(
|
128 |
-
current_speaker, current_start, last_time, current_items
|
129 |
-
)
|
130 |
-
|
131 |
-
def _create_segment(
|
132 |
-
self, speaker_id: str, start: float, end: float, items: list
|
133 |
-
) -> None:
|
134 |
-
segment_content = []
|
135 |
-
for item in items:
|
136 |
-
if item["type"] == "pronunciation":
|
137 |
-
segment_content.append(item["alternatives"][0]["content"])
|
138 |
-
elif item["type"] == "punctuation":
|
139 |
-
# Append punctuation to the last word without a space
|
140 |
-
if segment_content:
|
141 |
-
segment_content[-1] += item["alternatives"][0]["content"]
|
142 |
-
|
143 |
-
if segment_content:
|
144 |
-
self.segments.append(
|
145 |
-
TranscriptSegment(
|
146 |
-
speaker_id=speaker_id,
|
147 |
-
start_time=start,
|
148 |
-
end_time=end,
|
149 |
-
text=" ".join(segment_content),
|
150 |
-
)
|
151 |
-
)
|
152 |
-
|
153 |
-
def correct_speaker_mapping_with_agenda(self, url: str) -> None:
|
154 |
-
"""Fetch agenda from a URL and correct the speaker mapping using OpenAI."""
|
155 |
-
try:
|
156 |
-
if not url.startswith("http"):
|
157 |
-
# add https to the url
|
158 |
-
url = "https://" + url
|
159 |
-
|
160 |
-
response = requests.get(url)
|
161 |
-
response.raise_for_status()
|
162 |
-
html_content = response.text
|
163 |
-
|
164 |
-
# Parse the HTML to find the desired description
|
165 |
-
soup = BeautifulSoup(html_content, "html.parser")
|
166 |
-
description_tag = soup.find(
|
167 |
-
"script", {"type": "application/ld+json"}
|
168 |
-
) # Find the ld+json metadata block
|
169 |
-
agenda = ""
|
170 |
-
|
171 |
-
if description_tag:
|
172 |
-
# Extract the JSON content
|
173 |
-
json_data = json.loads(description_tag.string)
|
174 |
-
if "description" in json_data:
|
175 |
-
agenda = json_data["description"]
|
176 |
-
else:
|
177 |
-
print("Agenda description not found in the JSON metadata.")
|
178 |
-
else:
|
179 |
-
print("No structured data (ld+json) found.")
|
180 |
-
|
181 |
-
if not agenda:
|
182 |
-
print("No agenda found in the structured metadata. Trying meta tags.")
|
183 |
-
|
184 |
-
# Fallback: Use meta description if ld+json doesn't have it
|
185 |
-
meta_description = soup.find("meta", {"name": "description"})
|
186 |
-
agenda = meta_description["content"] if meta_description else ""
|
187 |
-
|
188 |
-
if not agenda:
|
189 |
-
print("No agenda found in any description tags.")
|
190 |
-
return
|
191 |
-
|
192 |
-
prompt = (
|
193 |
-
f"Given the original speaker mapping {self.speaker_mapping}, agenda:\n{agenda}, and the transcript: {self.formatted_transcript}\n\n"
|
194 |
-
"Some speaker names in the mapping might have spelling errors or be incomplete."
|
195 |
-
"Remember that the content in agenda is accurate and transcript can have errors so prioritize the spellings and names in the agenda content."
|
196 |
-
"If the speaker name and introduction is similar to the agenda, update the speaker name in the mapping."
|
197 |
-
"Please correct the names based on the agenda. Return the corrected mapping in JSON format as "
|
198 |
-
"{'spk_0': 'Correct Name', 'spk_1': 'Correct Name', ...}."
|
199 |
-
"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."
|
200 |
-
"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."
|
201 |
-
)
|
202 |
-
|
203 |
-
client = OpenAI()
|
204 |
-
|
205 |
-
completion = client.chat.completions.create(
|
206 |
-
model="gpt-4o-mini",
|
207 |
-
messages=[
|
208 |
-
{
|
209 |
-
"role": "system",
|
210 |
-
"content": "You are a helpful assistant. Who analyzes the given transcript, original speaker mapping and agenda. From the Agenda, you fix the spelling mistakes in the speaker names or update the names if they are similar to the agenda. 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.",
|
211 |
-
},
|
212 |
-
{"role": "user", "content": prompt},
|
213 |
-
],
|
214 |
-
temperature=0,
|
215 |
-
)
|
216 |
-
|
217 |
-
response_text = completion.choices[0].message.content.strip()
|
218 |
-
try:
|
219 |
-
corrected_mapping = json.loads(response_text)
|
220 |
-
except Exception:
|
221 |
-
response_text = response_text[
|
222 |
-
response_text.find("{") : response_text.rfind("}") + 1
|
223 |
-
]
|
224 |
-
try:
|
225 |
-
corrected_mapping = json.loads(response_text)
|
226 |
-
except json.JSONDecodeError:
|
227 |
-
print(
|
228 |
-
"Error parsing corrected speaker mapping JSON, keeping the original mapping."
|
229 |
-
)
|
230 |
-
corrected_mapping = self.speaker_mapping
|
231 |
-
# Update the speaker mapping with corrected names
|
232 |
-
self.speaker_mapping = corrected_mapping
|
233 |
-
|
234 |
-
# Update the transcript segments with corrected names
|
235 |
-
for segment in self.segments:
|
236 |
-
spk_id = f"spk_{segment.speaker_id}"
|
237 |
-
segment.speaker_name = self.speaker_mapping.get(spk_id, spk_id)
|
238 |
-
|
239 |
-
# Recreate the formatted transcript with corrected names
|
240 |
-
formatted_segments = []
|
241 |
-
for seg in self.segments:
|
242 |
-
start_time_str = self._format_time(seg.start_time)
|
243 |
-
end_time_str = self._format_time(seg.end_time)
|
244 |
-
formatted_segments.append(
|
245 |
-
f"time_stamp: {start_time_str}-{end_time_str}\n"
|
246 |
-
f"{seg.speaker_name}: {seg.text}\n"
|
247 |
-
)
|
248 |
-
self.formatted_transcript = "\n".join(formatted_segments)
|
249 |
-
|
250 |
-
except requests.exceptions.RequestException as e:
|
251 |
-
print(f" ching agenda from URL: {str(e)}")
|
252 |
-
except Exception as e:
|
253 |
-
print(f"Error correcting speaker mapping: {str(e)}")
|
254 |
-
|
255 |
-
def _create_formatted_transcript(self) -> None:
|
256 |
-
"""Create formatted transcript with default speaker labels."""
|
257 |
-
formatted_segments = []
|
258 |
-
for seg in self.segments:
|
259 |
-
start_time_str = self._format_time(seg.start_time)
|
260 |
-
end_time_str = self._format_time(seg.end_time)
|
261 |
-
# Use default speaker label (spk_X) if no mapping exists
|
262 |
-
speaker_label = f"spk_{seg.speaker_id}"
|
263 |
-
formatted_segments.append(
|
264 |
-
f"time_stamp: {start_time_str}-{end_time_str}\n"
|
265 |
-
f"{speaker_label}: {seg.text}\n"
|
266 |
-
)
|
267 |
-
self.formatted_transcript = "\n".join(formatted_segments)
|
268 |
-
|
269 |
-
def map_speaker_ids_to_names(self) -> None:
|
270 |
-
"""Map speaker IDs to names based on introductions in the transcript."""
|
271 |
-
try:
|
272 |
-
transcript = self.formatted_transcript
|
273 |
-
|
274 |
-
prompt = (
|
275 |
-
"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"
|
276 |
-
f"Transcript:\n{transcript}"
|
277 |
-
)
|
278 |
-
|
279 |
-
client = OpenAI()
|
280 |
-
|
281 |
-
completion = client.chat.completions.create(
|
282 |
-
model="gpt-4o",
|
283 |
-
messages=[
|
284 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
285 |
-
{"role": "user", "content": prompt},
|
286 |
-
],
|
287 |
-
temperature=0,
|
288 |
-
)
|
289 |
-
|
290 |
-
response_text = completion.choices[0].message.content.strip()
|
291 |
-
try:
|
292 |
-
self.speaker_mapping = json.loads(response_text)
|
293 |
-
except json.JSONDecodeError:
|
294 |
-
response_text = response_text[
|
295 |
-
response_text.find("{") : response_text.rfind("}") + 1
|
296 |
-
]
|
297 |
-
try:
|
298 |
-
self.speaker_mapping = json.loads(response_text)
|
299 |
-
except json.JSONDecodeError:
|
300 |
-
print("Error parsing speaker mapping JSON.")
|
301 |
-
self.speaker_mapping = {}
|
302 |
-
|
303 |
-
# Update segments with speaker names and recreate formatted transcript
|
304 |
-
for segment in self.segments:
|
305 |
-
spk_id = f"spk_{segment.speaker_id}"
|
306 |
-
speaker_name = self.speaker_mapping.get(spk_id, spk_id)
|
307 |
-
segment.speaker_name = speaker_name
|
308 |
-
|
309 |
-
self._create_formatted_transcript_with_names()
|
310 |
-
|
311 |
-
except Exception as e:
|
312 |
-
print(f"Error mapping speaker IDs to names: {str(e)}")
|
313 |
-
self.speaker_mapping = {}
|
314 |
-
|
315 |
-
def _create_formatted_transcript_with_names(self) -> None:
|
316 |
-
"""Create formatted transcript with mapped speaker names."""
|
317 |
-
formatted_segments = []
|
318 |
-
for seg in self.segments:
|
319 |
-
start_time_str = self._format_time(seg.start_time)
|
320 |
-
end_time_str = self._format_time(seg.end_time)
|
321 |
-
speaker_name = getattr(seg, "speaker_name", f"spk_{seg.speaker_id}")
|
322 |
-
formatted_segments.append(
|
323 |
-
f"Start Time: {start_time_str} - End Time: {end_time_str}\n"
|
324 |
-
f"{speaker_name}: {seg.text}\n"
|
325 |
-
)
|
326 |
-
self.formatted_transcript = "\n".join(formatted_segments)
|
327 |
-
|
328 |
-
def get_transcript(self) -> str:
|
329 |
-
"""Return the formatted transcript with speaker names."""
|
330 |
-
return self.formatted_transcript
|
331 |
-
|
332 |
-
def get_transcript_data(self) -> Dict:
|
333 |
-
"""Return the raw transcript data."""
|
334 |
-
return self.transcript_data
|
335 |
-
|
336 |
-
|
337 |
-
def setup_openai_key() -> None:
|
338 |
-
"""Set up OpenAI API key from file."""
|
339 |
-
try:
|
340 |
-
with open("api.key", "r") as f:
|
341 |
-
os.environ["OPENAI_API_KEY"] = f.read().strip()
|
342 |
-
except FileNotFoundError:
|
343 |
-
print("Using ENV variable")
|
344 |
-
# raise FileNotFoundError(
|
345 |
-
# "api.key file not found. Please create it with your OpenAI API key."
|
346 |
-
# )
|
347 |
-
|
348 |
-
|
349 |
-
def get_transcript_for_url(url: str) -> dict:
|
350 |
-
"""
|
351 |
-
This function fetches the transcript data for a signed URL.
|
352 |
-
If the URL results in a direct download, it processes the downloaded content.
|
353 |
-
|
354 |
-
:param url: Signed URL for the JSON file
|
355 |
-
:return: Parsed JSON data as a dictionary
|
356 |
-
"""
|
357 |
-
headers = {
|
358 |
-
"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"
|
359 |
-
}
|
360 |
-
|
361 |
-
try:
|
362 |
-
response = requests.get(url, headers=headers)
|
363 |
-
response.raise_for_status()
|
364 |
-
|
365 |
-
if "application/json" in response.headers.get("Content-Type", ""):
|
366 |
-
return response.json() # Parse and return JSON directly
|
367 |
-
|
368 |
-
# If not JSON, assume it's a file download (e.g., content-disposition header)
|
369 |
-
content_disposition = response.headers.get("Content-Disposition", "")
|
370 |
-
if "attachment" in content_disposition:
|
371 |
-
# Process the content as JSON
|
372 |
-
return json.loads(response.content)
|
373 |
-
|
374 |
-
return json.loads(response.content)
|
375 |
-
|
376 |
-
except requests.exceptions.HTTPError as http_err:
|
377 |
-
print(f"HTTP error occurred: {http_err}")
|
378 |
-
except requests.exceptions.RequestException as req_err:
|
379 |
-
print(f"Request error occurred: {req_err}")
|
380 |
-
except json.JSONDecodeError as json_err:
|
381 |
-
print(f"JSON decoding error: {json_err}")
|
382 |
-
|
383 |
-
return {}
|
384 |
|
385 |
|
386 |
def get_initial_analysis(
|
@@ -596,45 +221,7 @@ def chat(
|
|
596 |
origin,
|
597 |
ct,
|
598 |
uid,
|
599 |
-
)
|
600 |
-
tools = [
|
601 |
-
{
|
602 |
-
"type": "function",
|
603 |
-
"function": {
|
604 |
-
"name": "correct_speaker_name_with_url",
|
605 |
-
"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.",
|
606 |
-
"parameters": {
|
607 |
-
"type": "object",
|
608 |
-
"properties": {
|
609 |
-
"url": {
|
610 |
-
"type": "string",
|
611 |
-
"description": "The url to the agenda.",
|
612 |
-
},
|
613 |
-
},
|
614 |
-
"required": ["url"],
|
615 |
-
"additionalProperties": False,
|
616 |
-
},
|
617 |
-
},
|
618 |
-
},
|
619 |
-
{
|
620 |
-
"type": "function",
|
621 |
-
"function": {
|
622 |
-
"name": "correct_call_type",
|
623 |
-
"description": "If the user tells you the correct call type, you have to apologize and call this function with correct call type.",
|
624 |
-
"parameters": {
|
625 |
-
"type": "object",
|
626 |
-
"properties": {
|
627 |
-
"call_type": {
|
628 |
-
"type": "string",
|
629 |
-
"description": "The correct call type. If street interview, call type is 'si'.",
|
630 |
-
},
|
631 |
-
},
|
632 |
-
"required": ["call_type"],
|
633 |
-
"additionalProperties": False,
|
634 |
-
},
|
635 |
-
},
|
636 |
-
},
|
637 |
-
]
|
638 |
|
639 |
try:
|
640 |
client = OpenAI()
|
@@ -790,151 +377,6 @@ If the user provides the correct call type, use the correct_call_type function t
|
|
790 |
|
791 |
def create_chat_interface():
|
792 |
"""Create and configure the chat interface."""
|
793 |
-
css = """
|
794 |
-
.gradio-container {
|
795 |
-
|
796 |
-
padding-top: 0px !important;
|
797 |
-
padding-left: 0px !important;
|
798 |
-
padding-right: 0px !important;
|
799 |
-
padding: 0px !important;
|
800 |
-
margin: 0px !important;
|
801 |
-
}
|
802 |
-
#component-0 {
|
803 |
-
gap: 0px !important;
|
804 |
-
}
|
805 |
-
|
806 |
-
.icon-button-wrapper{
|
807 |
-
display: none !important;
|
808 |
-
}
|
809 |
-
|
810 |
-
|
811 |
-
footer {
|
812 |
-
display: none !important;
|
813 |
-
}
|
814 |
-
#chatbot_box{
|
815 |
-
flex-grow: 1 !important;
|
816 |
-
border-width: 0px !important;
|
817 |
-
}
|
818 |
-
|
819 |
-
#link-frame {
|
820 |
-
position: absolute !important;
|
821 |
-
width: 1px !important;
|
822 |
-
height: 1px !important;
|
823 |
-
right: -100px !important;
|
824 |
-
bottom: -100px !important;
|
825 |
-
display: none !important;
|
826 |
-
}
|
827 |
-
.html-container {
|
828 |
-
display: none !important;
|
829 |
-
}
|
830 |
-
a {
|
831 |
-
text-decoration: none !important;
|
832 |
-
}
|
833 |
-
#topic {
|
834 |
-
color: #999 !important;
|
835 |
-
}
|
836 |
-
.bubble-wrap {
|
837 |
-
padding-top: 0px !important;
|
838 |
-
}
|
839 |
-
.message-content {
|
840 |
-
border: 0px !important;
|
841 |
-
margin: 5px !important;
|
842 |
-
}
|
843 |
-
.message-row {
|
844 |
-
border-style: none !important;
|
845 |
-
margin: 0px !important;
|
846 |
-
width: 100% !important;
|
847 |
-
max-width: 100% !important;
|
848 |
-
}
|
849 |
-
.flex-wrap {
|
850 |
-
border-style: none !important;
|
851 |
-
}
|
852 |
-
|
853 |
-
.panel-full-width {
|
854 |
-
border-style: none !important;
|
855 |
-
border-width: 0px !important;
|
856 |
-
}
|
857 |
-
ol {
|
858 |
-
list-style-position: outside;
|
859 |
-
margin-left: 20px;
|
860 |
-
}
|
861 |
-
|
862 |
-
body.waiting * {
|
863 |
-
cursor: progress;
|
864 |
-
}
|
865 |
-
"""
|
866 |
-
head = f"""
|
867 |
-
<script defer src="https://www.gstatic.com/firebasejs/11.1.0/firebase-firestore.js"></script>
|
868 |
-
<script type="module">
|
869 |
-
// Import the functions you need from the SDKs you need
|
870 |
-
import {{ initializeApp }} from "https://www.gstatic.com/firebasejs/11.1.0/firebase-app.js";
|
871 |
-
import {{ getDatabase, ref, set }} from "https://www.gstatic.com/firebasejs/11.1.0/firebase-database.js";
|
872 |
-
// TODO: Add SDKs for Firebase products that you want to use
|
873 |
-
// https://firebase.google.com/docs/web/setup#available-libraries
|
874 |
-
console.log("Initializing Firebase");
|
875 |
-
// Your web app's Firebase configuration
|
876 |
-
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
|
877 |
-
const firebaseConfig = {{
|
878 |
-
apiKey: "{os.getenv('FIREBASE_API_KEY')}",
|
879 |
-
authDomain: "{os.getenv('FIREBASE_AUTH_DOMAIN')}",
|
880 |
-
databaseURL: "{os.getenv('FIREBASE_DATABASE_URL')}",
|
881 |
-
projectId: "{os.getenv('FIREBASE_PROJECT_ID')}",
|
882 |
-
storageBucket: "{os.getenv('FIREBASE_STORAGE_BUCKET')}",
|
883 |
-
messagingSenderId: "{os.getenv('FIREBASE_MESSAGING_SENDER_ID')}",
|
884 |
-
appId: "{os.getenv('FIREBASE_APP_ID')}",
|
885 |
-
measurementId: "{os.getenv('FIREBASE_MEASUREMENT_ID')}"
|
886 |
-
}};
|
887 |
-
|
888 |
-
// Initialize Firebase
|
889 |
-
const app = initializeApp(firebaseConfig);
|
890 |
-
const realtimeDB = getDatabase(app);
|
891 |
-
const rollAccount = "{os.getenv('ROLL_ACCOUNT')}";
|
892 |
-
const COLLECTIONS = {{
|
893 |
-
COLLAB_EDIT_LINK: "collab_link_handler",
|
894 |
-
}};
|
895 |
-
|
896 |
-
// Event listener for click
|
897 |
-
document.addEventListener('click', function (event) {{
|
898 |
-
var link = event.target.closest('a');
|
899 |
-
event.preventDefault();
|
900 |
-
if (link && link.href) {{
|
901 |
-
|
902 |
-
// Parse the URL to extract 'st' and 'et'
|
903 |
-
const url = new URL(link.href);
|
904 |
-
const startTime = url.searchParams.get('st');
|
905 |
-
const endTime = url.searchParams.get('et');
|
906 |
-
const userId = url.searchParams.get('uid') || "";
|
907 |
-
|
908 |
-
if (startTime || endTime) {{
|
909 |
-
let components = url.pathname.split("/");
|
910 |
-
let callId = components[2];
|
911 |
-
let recordingSessionId = components[3];
|
912 |
-
|
913 |
-
let data = {{
|
914 |
-
startTime: parseInt(startTime, 10),
|
915 |
-
endTime: parseInt(endTime, 10),
|
916 |
-
}};
|
917 |
-
|
918 |
-
console.log("Data to save:", data);
|
919 |
-
|
920 |
-
// Firebase reference
|
921 |
-
let reference = ref(
|
922 |
-
realtimeDB,
|
923 |
-
`${{rollAccount}}/${{COLLECTIONS.COLLAB_EDIT_LINK}}/${{userId}}/${{callId}}/${{recordingSessionId}}`
|
924 |
-
);
|
925 |
-
|
926 |
-
set(reference, data)
|
927 |
-
.then(() => {{
|
928 |
-
console.log("Data saved successfully:", data);
|
929 |
-
}})
|
930 |
-
.catch((error) => {{
|
931 |
-
console.error("Error saving data:", error);
|
932 |
-
}});
|
933 |
-
}}
|
934 |
-
}}
|
935 |
-
}});
|
936 |
-
</script>
|
937 |
-
"""
|
938 |
|
939 |
with gr.Blocks(
|
940 |
fill_height=True,
|
@@ -1010,12 +452,14 @@ def create_chat_interface():
|
|
1010 |
|
1011 |
# Handle initial loading with streaming
|
1012 |
def on_app_load(request: gr.Request):
|
|
|
1013 |
cid = request.query_params.get("cid", None)
|
1014 |
rsid = request.query_params.get("rsid", None)
|
1015 |
origin = request.query_params.get("origin", None)
|
1016 |
ct = request.query_params.get("ct", None)
|
1017 |
turl = request.query_params.get("turl", None)
|
1018 |
uid = request.query_params.get("uid", None)
|
|
|
1019 |
|
1020 |
required_params = ["cid", "rsid", "origin", "ct", "turl", "uid"]
|
1021 |
missing_params = [
|
@@ -1023,6 +467,7 @@ def create_chat_interface():
|
|
1023 |
for param in required_params
|
1024 |
if request.query_params.get(param) is None
|
1025 |
]
|
|
|
1026 |
|
1027 |
if missing_params:
|
1028 |
error_message = (
|
@@ -1031,32 +476,51 @@ def create_chat_interface():
|
|
1031 |
chatbot_value = [(None, error_message)]
|
1032 |
return [chatbot_value, None, None, None, None, None, None, None]
|
1033 |
|
1034 |
-
|
1035 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1036 |
transcript_processor = TranscriptProcessor(
|
1037 |
transcript_data=transcript_data,
|
1038 |
-
max_segment_duration=5 if ct != "si" else 10,
|
1039 |
call_type=ct,
|
|
|
1040 |
)
|
1041 |
|
1042 |
-
|
1043 |
-
|
1044 |
-
|
1045 |
-
|
1046 |
-
|
1047 |
-
|
1048 |
-
|
1049 |
-
|
1050 |
-
|
1051 |
-
|
1052 |
-
|
1053 |
-
|
1054 |
-
|
1055 |
-
|
1056 |
-
|
1057 |
-
|
1058 |
-
|
1059 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1060 |
|
1061 |
def display_processing_message(chatbot_value):
|
1062 |
"""Display the processing message while maintaining state."""
|
|
|
1 |
import json
|
2 |
+
from typing import Generator, List
|
|
|
|
|
3 |
|
4 |
import gradio as gr
|
|
|
|
|
5 |
from openai import OpenAI
|
6 |
+
from transcript import TranscriptProcessor
|
7 |
+
from utils import css, get_transcript_for_url, head, setup_openai_key
|
8 |
+
from utils import openai_tools as tools
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
def get_initial_analysis(
|
|
|
221 |
origin,
|
222 |
ct,
|
223 |
uid,
|
224 |
+
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
try:
|
227 |
client = OpenAI()
|
|
|
377 |
|
378 |
def create_chat_interface():
|
379 |
"""Create and configure the chat interface."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
380 |
|
381 |
with gr.Blocks(
|
382 |
fill_height=True,
|
|
|
452 |
|
453 |
# Handle initial loading with streaming
|
454 |
def on_app_load(request: gr.Request):
|
455 |
+
turls = None
|
456 |
cid = request.query_params.get("cid", None)
|
457 |
rsid = request.query_params.get("rsid", None)
|
458 |
origin = request.query_params.get("origin", None)
|
459 |
ct = request.query_params.get("ct", None)
|
460 |
turl = request.query_params.get("turl", None)
|
461 |
uid = request.query_params.get("uid", None)
|
462 |
+
pnames = request.query_params.get("pnames", None)
|
463 |
|
464 |
required_params = ["cid", "rsid", "origin", "ct", "turl", "uid"]
|
465 |
missing_params = [
|
|
|
467 |
for param in required_params
|
468 |
if request.query_params.get(param) is None
|
469 |
]
|
470 |
+
print("Missing Params", missing_params)
|
471 |
|
472 |
if missing_params:
|
473 |
error_message = (
|
|
|
476 |
chatbot_value = [(None, error_message)]
|
477 |
return [chatbot_value, None, None, None, None, None, None, None]
|
478 |
|
479 |
+
if ct == "rp":
|
480 |
+
# split turls based on ,
|
481 |
+
turls = turl.split(",")
|
482 |
+
pnames = [pname.replace("_", " ") for pname in pnames.split(",")]
|
483 |
+
print(pnames)
|
484 |
+
|
485 |
+
# try:
|
486 |
+
|
487 |
+
if turls:
|
488 |
+
transcript_data = []
|
489 |
+
for turl in turls:
|
490 |
+
print("Getting Transcript for URL")
|
491 |
+
transcript_data.append(get_transcript_for_url(turl))
|
492 |
+
print("Now creating Processor")
|
493 |
transcript_processor = TranscriptProcessor(
|
494 |
transcript_data=transcript_data,
|
|
|
495 |
call_type=ct,
|
496 |
+
person_names=pnames,
|
497 |
)
|
498 |
|
499 |
+
else:
|
500 |
+
transcript_data = get_transcript_for_url(turl)
|
501 |
+
transcript_processor = TranscriptProcessor(
|
502 |
+
transcript_data=transcript_data, call_type=ct
|
503 |
+
)
|
504 |
+
|
505 |
+
# Initialize with empty message
|
506 |
+
chatbot_value = [(None, "")]
|
507 |
+
|
508 |
+
# Return initial values with the transcript processor
|
509 |
+
return [
|
510 |
+
chatbot_value,
|
511 |
+
transcript_processor,
|
512 |
+
cid,
|
513 |
+
rsid,
|
514 |
+
origin,
|
515 |
+
ct,
|
516 |
+
turl,
|
517 |
+
uid,
|
518 |
+
]
|
519 |
+
# except Exception as e:
|
520 |
+
# print(e)
|
521 |
+
# error_message = f"Error processing call_id {cid}: {str(e)}"
|
522 |
+
# chatbot_value = [(None, error_message)]
|
523 |
+
# return [chatbot_value, None, None, None, None, None, None, None]
|
524 |
|
525 |
def display_processing_message(chatbot_value):
|
526 |
"""Display the processing message while maintaining state."""
|