Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import csv
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
-
import subprocess
|
| 6 |
import tempfile
|
| 7 |
import time
|
| 8 |
import xml.etree.ElementTree as ET
|
|
@@ -24,36 +23,41 @@ import openpyxl
|
|
| 24 |
import pptx
|
| 25 |
import PyPDF2
|
| 26 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
nltk.download("punkt")
|
| 30 |
-
nltk.download("punkt_tab")
|
| 31 |
-
nltk.download("omw-1.4")
|
| 32 |
-
nltk.download("wordnet")
|
| 33 |
|
| 34 |
|
| 35 |
def read_csv(file_path):
|
| 36 |
with open(
|
| 37 |
file_path,
|
| 38 |
-
|
| 39 |
-
encoding=
|
| 40 |
-
errors=
|
| 41 |
-
newline=
|
| 42 |
) as csvfile:
|
| 43 |
csv_reader = csv.reader(csvfile)
|
| 44 |
csv_data = [row for row in csv_reader]
|
| 45 |
|
| 46 |
-
return
|
| 47 |
-
[" ".join(row) for row in csv_data]
|
| 48 |
-
)
|
| 49 |
|
| 50 |
|
| 51 |
def read_text(file_path):
|
| 52 |
with open(
|
| 53 |
file_path,
|
| 54 |
-
|
| 55 |
-
encoding=
|
| 56 |
-
errors=
|
| 57 |
) as f:
|
| 58 |
return f.read()
|
| 59 |
|
|
@@ -61,104 +65,65 @@ def read_text(file_path):
|
|
| 61 |
def read_pdf(file_path):
|
| 62 |
text_data = []
|
| 63 |
|
| 64 |
-
with open(
|
| 65 |
-
|
| 66 |
-
"rb"
|
| 67 |
-
) as pdf_file:
|
| 68 |
-
pdf_reader = PyPDF2.PdfReader(
|
| 69 |
-
pdf_file
|
| 70 |
-
)
|
| 71 |
|
| 72 |
for page in pdf_reader.pages:
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
text_data.append(page_text)
|
| 77 |
-
|
| 78 |
-
return "\n".join(text_data)
|
| 79 |
|
| 80 |
|
| 81 |
def read_docx(file_path):
|
| 82 |
doc = docx.Document(file_path)
|
| 83 |
|
| 84 |
-
return
|
| 85 |
-
[
|
| 86 |
-
paragraph.text
|
| 87 |
-
for paragraph in doc.paragraphs
|
| 88 |
-
]
|
| 89 |
)
|
| 90 |
|
| 91 |
|
| 92 |
def read_pptx(file_path):
|
| 93 |
ppt = pptx.Presentation(file_path)
|
| 94 |
-
text_data =
|
| 95 |
|
| 96 |
for slide in ppt.slides:
|
| 97 |
for shape in slide.shapes:
|
| 98 |
-
if hasattr(shape,
|
| 99 |
-
text_data +=
|
| 100 |
-
shape.text + "\n"
|
| 101 |
-
)
|
| 102 |
|
| 103 |
return text_data
|
| 104 |
|
| 105 |
|
| 106 |
def read_xlsx(file_path):
|
| 107 |
-
workbook = openpyxl.load_workbook(
|
| 108 |
-
file_path
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
sheet = workbook.active
|
| 112 |
-
text_data =
|
| 113 |
|
| 114 |
-
for row in sheet.iter_rows(
|
| 115 |
-
values_only=True
|
| 116 |
-
):
|
| 117 |
text_data += (
|
| 118 |
-
|
| 119 |
-
[
|
| 120 |
-
str(cell)
|
| 121 |
-
for cell in row
|
| 122 |
-
if cell is not None
|
| 123 |
-
]
|
| 124 |
)
|
| 125 |
-
+
|
| 126 |
)
|
| 127 |
|
| 128 |
return text_data
|
| 129 |
|
| 130 |
|
| 131 |
def read_json(file_path):
|
| 132 |
-
with open(
|
| 133 |
-
file_path,
|
| 134 |
-
"r",
|
| 135 |
-
encoding="utf-8"
|
| 136 |
-
) as f:
|
| 137 |
json_data = json.load(f)
|
| 138 |
|
| 139 |
-
return json.dumps(
|
| 140 |
-
json_data,
|
| 141 |
-
ensure_ascii=False
|
| 142 |
-
)
|
| 143 |
|
| 144 |
|
| 145 |
def read_html(file_path):
|
| 146 |
-
with open(
|
| 147 |
-
file_path,
|
| 148 |
-
"r",
|
| 149 |
-
encoding="utf-8",
|
| 150 |
-
errors="ignore"
|
| 151 |
-
) as f:
|
| 152 |
html_content = f.read()
|
| 153 |
|
| 154 |
-
soup = BeautifulSoup(
|
| 155 |
-
html_content,
|
| 156 |
-
"html.parser"
|
| 157 |
-
)
|
| 158 |
|
| 159 |
-
return soup
|
| 160 |
-
separator="\n"
|
| 161 |
-
)
|
| 162 |
|
| 163 |
|
| 164 |
def read_xml(file_path):
|
|
@@ -167,98 +132,36 @@ def read_xml(file_path):
|
|
| 167 |
|
| 168 |
return ET.tostring(
|
| 169 |
root,
|
| 170 |
-
encoding=
|
| 171 |
)
|
| 172 |
|
| 173 |
|
| 174 |
def extract_youtube_video_id(url):
|
| 175 |
-
if not url:
|
| 176 |
-
return None
|
| 177 |
-
|
| 178 |
-
url = str(url).strip()
|
| 179 |
-
|
| 180 |
-
url = url.replace("\\n", "")
|
| 181 |
-
url = url.replace("\n", "")
|
| 182 |
-
url = url.replace("\\", "")
|
| 183 |
-
|
| 184 |
-
markdown_match = re.search(
|
| 185 |
-
r"\]\(\s*(https?://[^)\s]+)",
|
| 186 |
-
url,
|
| 187 |
-
flags=re.IGNORECASE
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
-
if markdown_match:
|
| 191 |
-
url = markdown_match.group(1)
|
| 192 |
-
|
| 193 |
-
url_match = re.search(
|
| 194 |
-
r"https?://(?:www\.)?(?:youtube\.com|youtu\.be)[^\s<>\"']+",
|
| 195 |
-
url,
|
| 196 |
-
flags=re.IGNORECASE
|
| 197 |
-
)
|
| 198 |
-
|
| 199 |
-
if url_match:
|
| 200 |
-
url = url_match.group(0)
|
| 201 |
-
|
| 202 |
-
url = url.rstrip(
|
| 203 |
-
".,!?;:)]}"
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
parsed = urlparse(url)
|
|
|
|
| 207 |
|
| 208 |
-
hostname
|
| 209 |
-
|
| 210 |
-
).lower()
|
| 211 |
-
|
| 212 |
-
if hostname in (
|
| 213 |
-
"youtu.be",
|
| 214 |
-
"www.youtu.be"
|
| 215 |
-
):
|
| 216 |
-
video_id = (
|
| 217 |
-
parsed.path
|
| 218 |
-
.lstrip("/")
|
| 219 |
-
.split("/")[0]
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
return video_id or None
|
| 223 |
|
| 224 |
-
if hostname
|
| 225 |
-
"youtube.com",
|
| 226 |
-
"www.youtube.com",
|
| 227 |
-
"m.youtube.com"
|
| 228 |
-
):
|
| 229 |
-
|
| 230 |
-
if parsed.path == "/watch":
|
| 231 |
|
|
|
|
| 232 |
video_ids = parse_qs(
|
| 233 |
parsed.query
|
| 234 |
-
).get(
|
| 235 |
-
|
| 236 |
-
if video_ids:
|
| 237 |
-
|
| 238 |
-
video_id = (
|
| 239 |
-
video_ids[0]
|
| 240 |
-
.split("&")[0]
|
| 241 |
-
.strip()
|
| 242 |
-
)
|
| 243 |
|
| 244 |
-
|
| 245 |
|
| 246 |
for prefix in (
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
):
|
| 251 |
-
|
| 252 |
-
if parsed.path.startswith(
|
| 253 |
-
prefix
|
| 254 |
-
):
|
| 255 |
-
|
| 256 |
video_id = (
|
| 257 |
-
parsed.path[
|
| 258 |
-
|
| 259 |
-
]
|
| 260 |
-
.split("/")[0]
|
| 261 |
-
.strip()
|
| 262 |
)
|
| 263 |
|
| 264 |
return video_id or None
|
|
@@ -267,284 +170,107 @@ def extract_youtube_video_id(url):
|
|
| 267 |
|
| 268 |
|
| 269 |
def is_youtube_url(url):
|
| 270 |
-
return (
|
| 271 |
-
extract_youtube_video_id(url)
|
| 272 |
-
is not None
|
| 273 |
-
)
|
| 274 |
-
|
| 275 |
|
| 276 |
-
def convert_vtt_to_text(vtt_content):
|
| 277 |
-
lines = vtt_content.splitlines()
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
if line.startswith("WEBVTT"):
|
| 289 |
-
continue
|
| 290 |
-
|
| 291 |
-
if "-->" in line:
|
| 292 |
-
continue
|
| 293 |
-
|
| 294 |
-
if re.match(
|
| 295 |
-
r"^\d+$",
|
| 296 |
-
line
|
| 297 |
-
):
|
| 298 |
-
continue
|
| 299 |
-
|
| 300 |
-
line = re.sub(
|
| 301 |
-
r"<[^>]+>",
|
| 302 |
-
"",
|
| 303 |
-
line
|
| 304 |
)
|
| 305 |
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
.replace("<", "<")
|
| 310 |
-
.replace(">", ">")
|
| 311 |
-
.replace("'", "'")
|
| 312 |
-
.replace(""", '"')
|
| 313 |
)
|
| 314 |
|
| 315 |
-
|
| 316 |
-
text_lines.append(line)
|
| 317 |
-
|
| 318 |
-
result = []
|
| 319 |
-
previous = None
|
| 320 |
-
|
| 321 |
-
for line in text_lines:
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
previous = line
|
| 327 |
-
|
| 328 |
-
return " ".join(result)
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
def fetch_youtube_transcript(video_id):
|
| 332 |
-
print(
|
| 333 |
-
"FETCHING YOUTUBE TRANSCRIPT:",
|
| 334 |
-
repr(video_id)
|
| 335 |
)
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
|
|
|
| 339 |
)
|
| 340 |
|
| 341 |
-
output_template = os.path.join(
|
| 342 |
-
temp_dir,
|
| 343 |
-
"%(id)s.%(ext)s"
|
| 344 |
-
)
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
"--sub-langs",
|
| 352 |
-
"en.*,en,ar.*",
|
| 353 |
-
"--sub-format",
|
| 354 |
-
"vtt",
|
| 355 |
-
"--no-playlist",
|
| 356 |
-
"--no-warnings",
|
| 357 |
-
"--quiet",
|
| 358 |
-
"--output",
|
| 359 |
-
output_template,
|
| 360 |
-
f"https://www.youtube.com/watch?v={video_id}"
|
| 361 |
-
]
|
| 362 |
-
|
| 363 |
-
try:
|
| 364 |
-
process = subprocess.run(
|
| 365 |
-
command,
|
| 366 |
-
capture_output=True,
|
| 367 |
-
text=True,
|
| 368 |
-
timeout=90
|
| 369 |
-
)
|
| 370 |
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
|
|
|
| 374 |
)
|
| 375 |
|
| 376 |
-
|
| 377 |
-
print(
|
| 378 |
-
"YTDLP STDOUT:",
|
| 379 |
-
process.stdout[:2000]
|
| 380 |
-
)
|
| 381 |
-
|
| 382 |
-
if process.stderr:
|
| 383 |
-
print(
|
| 384 |
-
"YTDLP STDERR:",
|
| 385 |
-
process.stderr[:3000]
|
| 386 |
-
)
|
| 387 |
-
|
| 388 |
-
if process.returncode != 0:
|
| 389 |
-
|
| 390 |
-
error_output = (
|
| 391 |
-
process.stderr
|
| 392 |
-
or process.stdout
|
| 393 |
-
or "Unknown yt-dlp error"
|
| 394 |
-
)
|
| 395 |
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
|
|
|
|
|
|
| 399 |
|
| 400 |
-
|
|
|
|
| 401 |
|
| 402 |
-
|
| 403 |
-
temp_dir
|
| 404 |
-
):
|
| 405 |
|
| 406 |
-
|
| 407 |
-
".vtt"
|
| 408 |
-
):
|
| 409 |
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
|
|
|
| 414 |
)
|
| 415 |
)
|
| 416 |
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
"English or Arabic subtitles."
|
| 422 |
-
)
|
| 423 |
-
|
| 424 |
-
vtt_files.sort(
|
| 425 |
-
key=lambda x: (
|
| 426 |
-
0
|
| 427 |
-
if ".en." in x.lower()
|
| 428 |
-
else 1
|
| 429 |
-
)
|
| 430 |
-
)
|
| 431 |
-
|
| 432 |
-
selected_file = vtt_files[0]
|
| 433 |
-
|
| 434 |
-
print(
|
| 435 |
-
"SELECTED SUBTITLE:",
|
| 436 |
-
selected_file
|
| 437 |
-
)
|
| 438 |
-
|
| 439 |
-
with open(
|
| 440 |
-
selected_file,
|
| 441 |
-
"r",
|
| 442 |
-
encoding="utf-8"
|
| 443 |
-
) as subtitle_file:
|
| 444 |
-
|
| 445 |
-
vtt_content = (
|
| 446 |
-
subtitle_file.read()
|
| 447 |
-
)
|
| 448 |
-
|
| 449 |
-
transcript = convert_vtt_to_text(
|
| 450 |
-
vtt_content
|
| 451 |
-
)
|
| 452 |
-
|
| 453 |
-
if not transcript:
|
| 454 |
-
|
| 455 |
-
raise RuntimeError(
|
| 456 |
-
"Subtitle file was found "
|
| 457 |
-
"but contains no readable text."
|
| 458 |
-
)
|
| 459 |
-
|
| 460 |
-
return transcript
|
| 461 |
-
|
| 462 |
-
except subprocess.TimeoutExpired:
|
| 463 |
-
|
| 464 |
-
raise RuntimeError(
|
| 465 |
-
"YouTube subtitle request timed out."
|
| 466 |
-
)
|
| 467 |
-
|
| 468 |
-
finally:
|
| 469 |
-
|
| 470 |
-
try:
|
| 471 |
-
|
| 472 |
-
for filename in os.listdir(
|
| 473 |
-
temp_dir
|
| 474 |
):
|
| 475 |
-
|
| 476 |
-
filepath = os.path.join(
|
| 477 |
-
temp_dir,
|
| 478 |
-
filename
|
| 479 |
-
)
|
| 480 |
-
|
| 481 |
-
if os.path.isfile(filepath):
|
| 482 |
-
os.remove(filepath)
|
| 483 |
-
|
| 484 |
-
os.rmdir(temp_dir)
|
| 485 |
-
|
| 486 |
-
except Exception:
|
| 487 |
-
pass
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
def process_youtube_video(url):
|
| 491 |
-
video_id = extract_youtube_video_id(
|
| 492 |
-
url
|
| 493 |
-
)
|
| 494 |
-
|
| 495 |
-
if not video_id:
|
| 496 |
|
| 497 |
return (
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
repr(video_id)
|
| 505 |
-
)
|
| 506 |
-
|
| 507 |
-
try:
|
| 508 |
-
|
| 509 |
-
transcript = fetch_youtube_transcript(
|
| 510 |
-
video_id
|
| 511 |
-
)
|
| 512 |
-
|
| 513 |
-
if transcript:
|
| 514 |
-
return transcript
|
| 515 |
-
|
| 516 |
-
return (
|
| 517 |
-
"The YouTube transcript was retrieved "
|
| 518 |
-
"but contains no readable text."
|
| 519 |
)
|
| 520 |
|
| 521 |
except Exception as e:
|
| 522 |
-
|
| 523 |
-
print(
|
| 524 |
-
"YOUTUBE TRANSCRIPT ERROR:",
|
| 525 |
-
repr(e)
|
| 526 |
-
)
|
| 527 |
-
|
| 528 |
-
return (
|
| 529 |
-
"Unable to retrieve the YouTube transcript.\n\n"
|
| 530 |
-
f"Error: {str(e)}"
|
| 531 |
-
)
|
| 532 |
|
| 533 |
|
| 534 |
def read_web_page(url):
|
|
|
|
| 535 |
try:
|
| 536 |
|
| 537 |
-
url =
|
| 538 |
|
| 539 |
response = requests.get(
|
| 540 |
url,
|
| 541 |
headers={
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
)
|
| 549 |
},
|
| 550 |
timeout=20,
|
|
@@ -552,220 +278,192 @@ def read_web_page(url):
|
|
| 552 |
)
|
| 553 |
|
| 554 |
if response.status_code >= 400:
|
| 555 |
-
|
| 556 |
return (
|
| 557 |
-
|
| 558 |
-
f
|
| 559 |
)
|
| 560 |
|
| 561 |
content_type = (
|
| 562 |
response.headers
|
| 563 |
-
.get(
|
| 564 |
.lower()
|
| 565 |
)
|
| 566 |
|
| 567 |
-
if
|
| 568 |
return response.text
|
| 569 |
|
|
|
|
|
|
|
| 570 |
soup = BeautifulSoup(
|
| 571 |
-
|
| 572 |
-
|
| 573 |
)
|
| 574 |
|
| 575 |
-
for element in soup(
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
]
|
| 582 |
-
):
|
| 583 |
element.decompose()
|
| 584 |
|
| 585 |
text_data = soup.get_text(
|
| 586 |
-
separator=
|
| 587 |
)
|
| 588 |
|
| 589 |
-
text_data =
|
| 590 |
line.strip()
|
| 591 |
for line in text_data.splitlines()
|
| 592 |
if line.strip()
|
| 593 |
)
|
| 594 |
|
| 595 |
if not text_data:
|
| 596 |
-
|
| 597 |
return (
|
| 598 |
-
|
| 599 |
-
|
| 600 |
)
|
| 601 |
|
| 602 |
return text_data
|
| 603 |
|
| 604 |
except requests.exceptions.Timeout:
|
| 605 |
-
|
| 606 |
return (
|
| 607 |
-
|
| 608 |
-
|
| 609 |
)
|
| 610 |
|
| 611 |
except requests.exceptions.RequestException as e:
|
| 612 |
-
|
| 613 |
-
return (
|
| 614 |
-
f"Unable to access webpage: {e}"
|
| 615 |
-
)
|
| 616 |
|
| 617 |
except Exception as e:
|
| 618 |
-
|
| 619 |
-
return (
|
| 620 |
-
"An error occurred while reading "
|
| 621 |
-
f"the webpage: {e}"
|
| 622 |
-
)
|
| 623 |
|
| 624 |
|
| 625 |
def read_data(
|
| 626 |
file_path_or_url,
|
| 627 |
-
languages=
|
| 628 |
):
|
| 629 |
|
| 630 |
if not file_path_or_url:
|
| 631 |
-
return
|
| 632 |
|
| 633 |
file_path_or_url = str(
|
| 634 |
file_path_or_url
|
| 635 |
).strip()
|
| 636 |
|
| 637 |
-
if is_youtube_url(
|
| 638 |
-
file_path_or_url
|
| 639 |
-
):
|
| 640 |
|
|
|
|
| 641 |
return process_youtube_video(
|
| 642 |
-
file_path_or_url
|
|
|
|
| 643 |
)
|
| 644 |
|
| 645 |
if file_path_or_url.startswith(
|
| 646 |
-
(
|
| 647 |
-
"http://",
|
| 648 |
-
"https://"
|
| 649 |
-
)
|
| 650 |
):
|
| 651 |
-
|
| 652 |
return read_web_page(
|
| 653 |
file_path_or_url
|
| 654 |
)
|
| 655 |
|
| 656 |
-
lower_path = (
|
| 657 |
-
file_path_or_url.lower()
|
| 658 |
-
)
|
| 659 |
|
| 660 |
-
|
|
|
|
|
|
|
| 661 |
return read_csv(file_path_or_url)
|
| 662 |
|
| 663 |
-
elif lower_path.endswith(
|
| 664 |
return read_text(file_path_or_url)
|
| 665 |
|
| 666 |
-
elif lower_path.endswith(
|
| 667 |
return read_pdf(file_path_or_url)
|
| 668 |
|
| 669 |
-
elif lower_path.endswith(
|
| 670 |
return read_docx(file_path_or_url)
|
| 671 |
|
| 672 |
-
elif lower_path.endswith(
|
| 673 |
return read_pptx(file_path_or_url)
|
| 674 |
|
| 675 |
-
elif lower_path.endswith(
|
| 676 |
return read_xlsx(file_path_or_url)
|
| 677 |
|
| 678 |
-
elif lower_path.endswith(
|
| 679 |
return read_json(file_path_or_url)
|
| 680 |
|
| 681 |
-
elif lower_path.endswith(
|
| 682 |
return read_html(file_path_or_url)
|
| 683 |
|
| 684 |
-
elif lower_path.endswith(
|
| 685 |
return read_xml(file_path_or_url)
|
| 686 |
|
| 687 |
-
return
|
| 688 |
|
| 689 |
|
| 690 |
def normalize_text(text):
|
| 691 |
-
if not isinstance(
|
| 692 |
-
text,
|
| 693 |
-
str
|
| 694 |
-
):
|
| 695 |
-
text = str(text)
|
| 696 |
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
" ",
|
| 700 |
-
text
|
| 701 |
-
)
|
| 702 |
|
| 703 |
text = re.sub(
|
| 704 |
-
r
|
| 705 |
-
|
| 706 |
text
|
| 707 |
)
|
| 708 |
|
| 709 |
-
text = text.lower()
|
|
|
|
| 710 |
|
| 711 |
-
punctuation = (
|
| 712 |
-
r"""!()[]{};:'"\<>/?$%^&*_`~="""
|
| 713 |
-
)
|
| 714 |
|
| 715 |
for punc in punctuation:
|
| 716 |
text = text.replace(
|
| 717 |
punc,
|
| 718 |
-
|
| 719 |
)
|
| 720 |
|
| 721 |
text = re.sub(
|
| 722 |
-
r
|
| 723 |
-
|
| 724 |
text
|
| 725 |
)
|
| 726 |
|
| 727 |
words = word_tokenize(text)
|
| 728 |
|
| 729 |
-
return
|
| 730 |
|
| 731 |
|
| 732 |
llm = HuggingFaceEndpoint(
|
| 733 |
-
repo_id=
|
| 734 |
-
task=
|
| 735 |
max_new_tokens=4096,
|
| 736 |
temperature=0.6,
|
| 737 |
top_p=0.9,
|
| 738 |
top_k=40,
|
| 739 |
repetition_penalty=1.2,
|
| 740 |
-
do_sample=True
|
| 741 |
)
|
| 742 |
|
| 743 |
-
|
| 744 |
chat_model = ChatHuggingFace(
|
| 745 |
llm=llm
|
| 746 |
)
|
| 747 |
|
| 748 |
|
|
|
|
| 749 |
model_name = (
|
| 750 |
-
|
| 751 |
)
|
| 752 |
|
| 753 |
-
|
| 754 |
embedding_llm = HuggingFaceEmbeddings(
|
| 755 |
model_name=model_name
|
| 756 |
)
|
| 757 |
|
| 758 |
-
|
| 759 |
db = FAISS.load_local(
|
| 760 |
-
|
| 761 |
embedding_llm,
|
| 762 |
allow_dangerous_deserialization=True
|
| 763 |
)
|
| 764 |
|
| 765 |
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
):
|
| 769 |
print(
|
| 770 |
x.index,
|
| 771 |
x.value,
|
|
@@ -773,6 +471,7 @@ def print_like_dislike(
|
|
| 773 |
)
|
| 774 |
|
| 775 |
|
|
|
|
| 776 |
def user(
|
| 777 |
user_message,
|
| 778 |
history
|
|
@@ -780,15 +479,15 @@ def user(
|
|
| 780 |
|
| 781 |
if not len(user_message):
|
| 782 |
raise gr.Error(
|
| 783 |
-
|
| 784 |
)
|
| 785 |
|
| 786 |
history.append({
|
| 787 |
-
|
| 788 |
-
|
| 789 |
})
|
| 790 |
|
| 791 |
-
return
|
| 792 |
|
| 793 |
|
| 794 |
def user2(
|
|
@@ -797,30 +496,24 @@ def user2(
|
|
| 797 |
link
|
| 798 |
):
|
| 799 |
|
| 800 |
-
if (
|
| 801 |
-
not len(user_message)
|
| 802 |
-
or not len(link)
|
| 803 |
-
):
|
| 804 |
raise gr.Error(
|
| 805 |
-
|
| 806 |
)
|
| 807 |
|
| 808 |
link = str(link).strip()
|
| 809 |
-
user_message = str(
|
| 810 |
-
user_message
|
| 811 |
-
).strip()
|
| 812 |
|
| 813 |
combined_message = (
|
| 814 |
-
f
|
| 815 |
-
f"QUESTION: {user_message}"
|
| 816 |
)
|
| 817 |
|
| 818 |
history.append({
|
| 819 |
-
|
| 820 |
-
|
| 821 |
})
|
| 822 |
|
| 823 |
-
return
|
| 824 |
|
| 825 |
|
| 826 |
def user3(
|
|
@@ -829,49 +522,48 @@ def user3(
|
|
| 829 |
file_path
|
| 830 |
):
|
| 831 |
|
| 832 |
-
if (
|
| 833 |
-
not len(user_message)
|
| 834 |
-
or not file_path
|
| 835 |
-
):
|
| 836 |
raise gr.Error(
|
| 837 |
-
|
| 838 |
)
|
| 839 |
|
| 840 |
combined_message = (
|
| 841 |
-
f
|
| 842 |
-
f"{user_message}"
|
| 843 |
)
|
| 844 |
|
| 845 |
history.append({
|
| 846 |
-
|
| 847 |
-
|
| 848 |
})
|
| 849 |
|
| 850 |
-
return
|
|
|
|
|
|
|
| 851 |
|
| 852 |
|
| 853 |
messages1_state = [
|
| 854 |
SystemMessage(
|
| 855 |
-
content=
|
| 856 |
),
|
| 857 |
HumanMessage(
|
| 858 |
-
content=
|
| 859 |
),
|
| 860 |
AIMessage(
|
| 861 |
content=(
|
| 862 |
"I'm great thank you. "
|
| 863 |
"How can I help you?"
|
| 864 |
)
|
| 865 |
-
)
|
| 866 |
]
|
| 867 |
|
| 868 |
|
|
|
|
| 869 |
def Chat_Message(
|
| 870 |
history,
|
| 871 |
messages1
|
| 872 |
):
|
| 873 |
|
| 874 |
-
user_msg_text = history[-1][
|
| 875 |
|
| 876 |
message = HumanMessage(
|
| 877 |
content=user_msg_text
|
|
@@ -896,14 +588,35 @@ def Chat_Message(
|
|
| 896 |
|
| 897 |
except Exception as e:
|
| 898 |
|
|
|
|
|
|
|
| 899 |
print(
|
| 900 |
-
|
| 901 |
repr(e)
|
| 902 |
)
|
| 903 |
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 907 |
|
| 908 |
messages1.append(
|
| 909 |
AIMessage(
|
|
@@ -912,26 +625,27 @@ def Chat_Message(
|
|
| 912 |
)
|
| 913 |
|
| 914 |
history.append({
|
| 915 |
-
|
| 916 |
-
|
| 917 |
})
|
| 918 |
|
| 919 |
for character in response.content:
|
| 920 |
|
| 921 |
-
history[-1][
|
| 922 |
|
| 923 |
time.sleep(0.0025)
|
| 924 |
|
| 925 |
yield history, messages1
|
| 926 |
|
| 927 |
|
|
|
|
| 928 |
def Internet_Search(
|
| 929 |
history,
|
| 930 |
messages2
|
| 931 |
):
|
| 932 |
|
| 933 |
message = str(
|
| 934 |
-
history[-1][
|
| 935 |
)
|
| 936 |
|
| 937 |
if isinstance(
|
|
@@ -947,7 +661,7 @@ def Internet_Search(
|
|
| 947 |
|
| 948 |
if similar_docs:
|
| 949 |
|
| 950 |
-
source_knowledge =
|
| 951 |
[
|
| 952 |
x.page_content
|
| 953 |
for x in similar_docs
|
|
@@ -955,7 +669,7 @@ def Internet_Search(
|
|
| 955 |
)
|
| 956 |
|
| 957 |
else:
|
| 958 |
-
source_knowledge =
|
| 959 |
|
| 960 |
augmented_prompt = f"""
|
| 961 |
You are an AI designed to help understand
|
|
@@ -992,14 +706,35 @@ information from other sources.
|
|
| 992 |
|
| 993 |
except Exception as e:
|
| 994 |
|
|
|
|
|
|
|
| 995 |
print(
|
| 996 |
-
|
| 997 |
repr(e)
|
| 998 |
)
|
| 999 |
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1003 |
|
| 1004 |
messages2.append(
|
| 1005 |
AIMessage(
|
|
@@ -1008,23 +743,24 @@ information from other sources.
|
|
| 1008 |
)
|
| 1009 |
|
| 1010 |
history.append({
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
})
|
| 1014 |
|
| 1015 |
for character in response.content:
|
| 1016 |
|
| 1017 |
-
history[-1][
|
| 1018 |
|
| 1019 |
time.sleep(0.0025)
|
| 1020 |
|
| 1021 |
yield history, messages2
|
| 1022 |
|
| 1023 |
|
|
|
|
|
|
|
| 1024 |
def generate_chart_config(
|
| 1025 |
description
|
| 1026 |
):
|
| 1027 |
-
|
| 1028 |
system_instructions = """
|
| 1029 |
You are a Chart.js configuration generator.
|
| 1030 |
|
|
@@ -1033,17 +769,17 @@ Return ONLY valid JSON.
|
|
| 1033 |
The JSON must have this structure:
|
| 1034 |
|
| 1035 |
{
|
| 1036 |
-
"type": "bar",
|
| 1037 |
-
"data": {
|
| 1038 |
-
"labels": [],
|
| 1039 |
-
"datasets": [
|
| 1040 |
-
{
|
| 1041 |
-
"label": "",
|
| 1042 |
-
"data": []
|
| 1043 |
-
}
|
| 1044 |
-
]
|
| 1045 |
-
},
|
| 1046 |
-
"options": {}
|
| 1047 |
}
|
| 1048 |
|
| 1049 |
Rules:
|
|
@@ -1067,15 +803,15 @@ Rules:
|
|
| 1067 |
),
|
| 1068 |
HumanMessage(
|
| 1069 |
content=(
|
| 1070 |
-
|
| 1071 |
-
|
| 1072 |
-
f
|
| 1073 |
)
|
| 1074 |
-
)
|
| 1075 |
]
|
| 1076 |
|
| 1077 |
print(
|
| 1078 |
-
|
| 1079 |
description
|
| 1080 |
)
|
| 1081 |
|
|
@@ -1088,478 +824,368 @@ Rules:
|
|
| 1088 |
).strip()
|
| 1089 |
|
| 1090 |
print(
|
| 1091 |
-
|
| 1092 |
raw
|
| 1093 |
)
|
| 1094 |
|
| 1095 |
raw = re.sub(
|
| 1096 |
-
r
|
| 1097 |
-
|
| 1098 |
raw,
|
| 1099 |
flags=re.IGNORECASE
|
| 1100 |
)
|
| 1101 |
|
| 1102 |
raw = re.sub(
|
| 1103 |
-
r
|
| 1104 |
-
|
| 1105 |
raw
|
| 1106 |
)
|
| 1107 |
|
| 1108 |
raw = re.sub(
|
| 1109 |
-
r
|
| 1110 |
-
|
| 1111 |
raw
|
| 1112 |
)
|
| 1113 |
|
| 1114 |
raw = raw.strip()
|
| 1115 |
|
| 1116 |
-
start = raw.find(
|
| 1117 |
-
end = raw.rfind(
|
| 1118 |
-
|
| 1119 |
-
if (
|
| 1120 |
-
start == -1
|
| 1121 |
-
or end == -1
|
| 1122 |
-
):
|
| 1123 |
|
|
|
|
| 1124 |
raise ValueError(
|
| 1125 |
-
|
| 1126 |
-
f
|
| 1127 |
)
|
| 1128 |
|
| 1129 |
raw = raw[
|
| 1130 |
start:end + 1
|
| 1131 |
]
|
| 1132 |
|
| 1133 |
-
|
| 1134 |
-
|
| 1135 |
-
|
| 1136 |
-
raise ValueError(
|
| 1137 |
-
"Chart config missing 'type'"
|
| 1138 |
-
)
|
| 1139 |
-
|
| 1140 |
-
if "data" not in config:
|
| 1141 |
-
raise ValueError(
|
| 1142 |
-
"Chart config missing 'data'"
|
| 1143 |
-
)
|
| 1144 |
-
|
| 1145 |
-
if "labels" not in config["data"]:
|
| 1146 |
-
raise ValueError(
|
| 1147 |
-
"Chart config missing 'data.labels'"
|
| 1148 |
-
)
|
| 1149 |
-
|
| 1150 |
-
if "datasets" not in config["data"]:
|
| 1151 |
-
raise ValueError(
|
| 1152 |
-
"Chart config missing 'data.datasets'"
|
| 1153 |
-
)
|
| 1154 |
-
|
| 1155 |
-
if not isinstance(
|
| 1156 |
-
config["data"]["datasets"],
|
| 1157 |
-
list
|
| 1158 |
-
):
|
| 1159 |
-
raise ValueError(
|
| 1160 |
-
"'data.datasets' must be a list"
|
| 1161 |
-
)
|
| 1162 |
-
|
| 1163 |
-
return config
|
| 1164 |
-
|
| 1165 |
-
|
| 1166 |
-
def Chart_Generator(
|
| 1167 |
-
history,
|
| 1168 |
-
messages3
|
| 1169 |
-
):
|
| 1170 |
-
|
| 1171 |
-
message = str(
|
| 1172 |
-
history[-1]["content"]
|
| 1173 |
)
|
| 1174 |
|
| 1175 |
-
|
| 1176 |
-
messages3[-1],
|
| 1177 |
-
HumanMessage
|
| 1178 |
-
):
|
| 1179 |
-
messages3 = messages3[:-2]
|
| 1180 |
-
|
| 1181 |
-
if "#chart" in message.lower():
|
| 1182 |
-
|
| 1183 |
-
chart_description = re.split(
|
| 1184 |
-
r"#chart",
|
| 1185 |
-
message,
|
| 1186 |
-
maxsplit=1,
|
| 1187 |
-
flags=re.IGNORECASE
|
| 1188 |
-
)[1].strip()
|
| 1189 |
-
|
| 1190 |
-
if not chart_description:
|
| 1191 |
-
|
| 1192 |
-
combined_content = (
|
| 1193 |
-
"Please provide chart details after #chart."
|
| 1194 |
-
)
|
| 1195 |
-
|
| 1196 |
-
else:
|
| 1197 |
-
|
| 1198 |
-
chart_config = None
|
| 1199 |
-
|
| 1200 |
-
try:
|
| 1201 |
-
|
| 1202 |
-
chart_config = (
|
| 1203 |
-
generate_chart_config(
|
| 1204 |
-
chart_description
|
| 1205 |
-
)
|
| 1206 |
-
)
|
| 1207 |
-
|
| 1208 |
-
except Exception as e:
|
| 1209 |
-
|
| 1210 |
-
print(
|
| 1211 |
-
"CHART GENERATION ERROR:",
|
| 1212 |
-
repr(e)
|
| 1213 |
-
)
|
| 1214 |
-
|
| 1215 |
-
combined_content = (
|
| 1216 |
-
"Chart generation failed.\n\n"
|
| 1217 |
-
f"Error: {str(e)}"
|
| 1218 |
-
)
|
| 1219 |
-
|
| 1220 |
-
if chart_config:
|
| 1221 |
-
|
| 1222 |
-
try:
|
| 1223 |
-
|
| 1224 |
-
config_json = json.dumps(
|
| 1225 |
-
chart_config,
|
| 1226 |
-
separators=(",", ":")
|
| 1227 |
-
)
|
| 1228 |
-
|
| 1229 |
-
encoded_config = (
|
| 1230 |
-
requests.utils.quote(
|
| 1231 |
-
config_json,
|
| 1232 |
-
safe=""
|
| 1233 |
-
)
|
| 1234 |
-
)
|
| 1235 |
-
|
| 1236 |
-
chart_url = (
|
| 1237 |
-
"https://quickchart.io/chart"
|
| 1238 |
-
f"?c={encoded_config}"
|
| 1239 |
-
"&bkg=white"
|
| 1240 |
-
)
|
| 1241 |
-
|
| 1242 |
-
chart_response = requests.get(
|
| 1243 |
-
chart_url,
|
| 1244 |
-
timeout=30
|
| 1245 |
-
)
|
| 1246 |
-
|
| 1247 |
-
if (
|
| 1248 |
-
chart_response.status_code
|
| 1249 |
-
!= 200
|
| 1250 |
-
):
|
| 1251 |
-
|
| 1252 |
-
combined_content = (
|
| 1253 |
-
"QuickChart failed to generate "
|
| 1254 |
-
"the chart.\n\n"
|
| 1255 |
-
f"HTTP Status: "
|
| 1256 |
-
f"{chart_response.status_code}"
|
| 1257 |
-
)
|
| 1258 |
-
|
| 1259 |
-
else:
|
| 1260 |
-
|
| 1261 |
-
image_html = (
|
| 1262 |
-
f'<img src="{chart_url}" '
|
| 1263 |
-
'alt="Generated Chart" '
|
| 1264 |
-
'style="display:block; '
|
| 1265 |
-
'margin:auto; '
|
| 1266 |
-
'max-width:100%; '
|
| 1267 |
-
'max-height:100%;">'
|
| 1268 |
-
)
|
| 1269 |
-
|
| 1270 |
-
chart_summary_prompt = (
|
| 1271 |
-
"The following Chart.js configuration "
|
| 1272 |
-
"was generated:\n\n"
|
| 1273 |
-
f"{json.dumps(chart_config, indent=2)}\n\n"
|
| 1274 |
-
"Briefly describe what this chart "
|
| 1275 |
-
"represents."
|
| 1276 |
-
)
|
| 1277 |
-
|
| 1278 |
-
analysis_messages = [
|
| 1279 |
-
SystemMessage(
|
| 1280 |
-
content=(
|
| 1281 |
-
"You are analyzing chart "
|
| 1282 |
-
"configuration data, not "
|
| 1283 |
-
"an image. Be concise."
|
| 1284 |
-
)
|
| 1285 |
-
),
|
| 1286 |
-
HumanMessage(
|
| 1287 |
-
content=chart_summary_prompt
|
| 1288 |
-
)
|
| 1289 |
-
]
|
| 1290 |
-
|
| 1291 |
-
try:
|
| 1292 |
-
|
| 1293 |
-
response = (
|
| 1294 |
-
chat_model.invoke(
|
| 1295 |
-
analysis_messages
|
| 1296 |
-
)
|
| 1297 |
-
)
|
| 1298 |
-
|
| 1299 |
-
analysis_text = (
|
| 1300 |
-
response.content
|
| 1301 |
-
)
|
| 1302 |
-
|
| 1303 |
-
except Exception:
|
| 1304 |
-
|
| 1305 |
-
analysis_text = (
|
| 1306 |
-
"Chart generated successfully."
|
| 1307 |
-
)
|
| 1308 |
-
|
| 1309 |
-
combined_content = (
|
| 1310 |
-
f"{image_html}"
|
| 1311 |
-
f"<br>{analysis_text}"
|
| 1312 |
-
)
|
| 1313 |
-
|
| 1314 |
-
except Exception as e:
|
| 1315 |
-
|
| 1316 |
-
combined_content = (
|
| 1317 |
-
"Chart configuration was generated, "
|
| 1318 |
-
"but QuickChart could not be reached.\n\n"
|
| 1319 |
-
f"Error: {str(e)}"
|
| 1320 |
-
)
|
| 1321 |
-
|
| 1322 |
-
else:
|
| 1323 |
-
|
| 1324 |
-
prompt = HumanMessage(
|
| 1325 |
-
content=message
|
| 1326 |
-
)
|
| 1327 |
-
|
| 1328 |
-
messages3.append(prompt)
|
| 1329 |
-
|
| 1330 |
-
if len(messages3) >= 6:
|
| 1331 |
-
messages3 = messages3[-6:]
|
| 1332 |
-
|
| 1333 |
-
try:
|
| 1334 |
-
|
| 1335 |
-
response = chat_model.invoke(
|
| 1336 |
-
messages3
|
| 1337 |
-
)
|
| 1338 |
-
|
| 1339 |
-
except Exception as e:
|
| 1340 |
-
|
| 1341 |
-
print(
|
| 1342 |
-
"CHART TAB CHAT ERROR:",
|
| 1343 |
-
repr(e)
|
| 1344 |
-
)
|
| 1345 |
-
|
| 1346 |
-
raise gr.Error(
|
| 1347 |
-
"Error occurred during response"
|
| 1348 |
-
) from e
|
| 1349 |
|
| 1350 |
-
|
| 1351 |
-
|
| 1352 |
-
|
| 1353 |
-
)
|
| 1354 |
)
|
| 1355 |
|
| 1356 |
-
|
| 1357 |
-
|
| 1358 |
-
|
| 1359 |
-
|
| 1360 |
-
"content": ""
|
| 1361 |
-
})
|
| 1362 |
-
|
| 1363 |
-
for character in combined_content:
|
| 1364 |
|
| 1365 |
-
|
|
|
|
|
|
|
|
|
|
| 1366 |
|
| 1367 |
-
|
|
|
|
|
|
|
|
|
|
| 1368 |
|
| 1369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1370 |
|
|
|
|
| 1371 |
|
| 1372 |
-
def extract_url_from_text(text):
|
| 1373 |
|
| 1374 |
-
|
| 1375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1376 |
|
| 1377 |
if isinstance(
|
| 1378 |
-
|
| 1379 |
-
|
| 1380 |
):
|
|
|
|
| 1381 |
|
| 1382 |
-
|
| 1383 |
-
|
| 1384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1385 |
|
| 1386 |
-
|
| 1387 |
-
|
| 1388 |
-
|
| 1389 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1390 |
|
| 1391 |
-
|
| 1392 |
-
|
| 1393 |
-
|
| 1394 |
-
|
| 1395 |
-
|
| 1396 |
-
)
|
| 1397 |
)
|
| 1398 |
)
|
| 1399 |
|
| 1400 |
-
|
| 1401 |
|
| 1402 |
-
|
| 1403 |
-
|
|
|
|
| 1404 |
)
|
| 1405 |
|
| 1406 |
-
|
|
|
|
|
|
|
|
|
|
| 1407 |
|
| 1408 |
-
|
| 1409 |
-
|
| 1410 |
-
|
| 1411 |
-
|
|
|
|
| 1412 |
|
| 1413 |
-
|
| 1414 |
-
|
| 1415 |
-
|
| 1416 |
-
|
| 1417 |
-
)
|
| 1418 |
-
)
|
| 1419 |
|
| 1420 |
-
|
|
|
|
|
|
|
|
|
|
| 1421 |
|
| 1422 |
-
|
|
|
|
|
|
|
|
|
|
| 1423 |
|
| 1424 |
-
|
| 1425 |
-
|
| 1426 |
-
|
| 1427 |
-
|
|
|
|
| 1428 |
|
| 1429 |
-
|
| 1430 |
-
|
| 1431 |
-
|
| 1432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1433 |
|
| 1434 |
-
|
| 1435 |
-
|
| 1436 |
-
|
| 1437 |
-
|
| 1438 |
-
|
|
|
|
|
|
|
| 1439 |
|
| 1440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1441 |
|
| 1442 |
-
|
| 1443 |
-
|
| 1444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1445 |
|
| 1446 |
-
|
| 1447 |
-
|
| 1448 |
-
|
| 1449 |
-
|
| 1450 |
-
)
|
| 1451 |
|
| 1452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1453 |
|
| 1454 |
-
|
| 1455 |
-
|
| 1456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1457 |
|
| 1458 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1459 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1460 |
|
| 1461 |
-
|
| 1462 |
-
|
| 1463 |
-
link
|
| 1464 |
-
):
|
| 1465 |
|
| 1466 |
-
|
| 1467 |
-
|
| 1468 |
-
list
|
| 1469 |
-
):
|
| 1470 |
|
| 1471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1472 |
|
| 1473 |
-
|
|
|
|
| 1474 |
|
| 1475 |
-
|
| 1476 |
-
|
| 1477 |
-
|
| 1478 |
-
|
|
|
|
|
|
|
| 1479 |
|
| 1480 |
-
|
| 1481 |
-
|
| 1482 |
-
|
| 1483 |
-
|
| 1484 |
-
|
| 1485 |
)
|
| 1486 |
-
|
| 1487 |
)
|
| 1488 |
|
| 1489 |
-
|
| 1490 |
|
| 1491 |
-
|
| 1492 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1493 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1494 |
|
| 1495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1496 |
|
| 1497 |
-
|
| 1498 |
-
|
| 1499 |
-
|
| 1500 |
-
)
|
| 1501 |
|
| 1502 |
-
|
| 1503 |
-
|
| 1504 |
-
|
| 1505 |
-
|
| 1506 |
-
)
|
| 1507 |
-
)
|
| 1508 |
|
| 1509 |
-
else:
|
| 1510 |
|
| 1511 |
-
|
|
|
|
|
|
|
| 1512 |
|
| 1513 |
-
|
| 1514 |
-
"\\n",
|
| 1515 |
-
"\n"
|
| 1516 |
-
)
|
| 1517 |
|
| 1518 |
-
|
| 1519 |
-
|
| 1520 |
-
|
| 1521 |
)
|
| 1522 |
|
| 1523 |
-
|
|
|
|
|
|
|
|
|
|
| 1524 |
|
| 1525 |
-
|
| 1526 |
-
r
|
| 1527 |
-
|
| 1528 |
-
flags=re.IGNORECASE | re.DOTALL
|
| 1529 |
)
|
| 1530 |
|
| 1531 |
-
if
|
| 1532 |
-
|
| 1533 |
-
|
| 1534 |
-
1
|
| 1535 |
-
).strip()
|
| 1536 |
-
|
| 1537 |
-
if link:
|
| 1538 |
-
|
| 1539 |
-
question = content.replace(
|
| 1540 |
-
link,
|
| 1541 |
-
""
|
| 1542 |
-
)
|
| 1543 |
-
|
| 1544 |
-
question = re.sub(
|
| 1545 |
-
r"URL\s*:\s*",
|
| 1546 |
-
"",
|
| 1547 |
-
question,
|
| 1548 |
-
flags=re.IGNORECASE
|
| 1549 |
)
|
| 1550 |
|
| 1551 |
-
|
| 1552 |
-
|
| 1553 |
-
return content
|
| 1554 |
|
| 1555 |
|
| 1556 |
def Link_Scratch(
|
| 1557 |
history,
|
| 1558 |
messages4
|
| 1559 |
):
|
| 1560 |
-
|
| 1561 |
-
|
| 1562 |
-
history[-1]["content"]
|
| 1563 |
)
|
| 1564 |
|
| 1565 |
if isinstance(
|
|
@@ -1572,109 +1198,109 @@ def Link_Scratch(
|
|
| 1572 |
combined_message
|
| 1573 |
)
|
| 1574 |
|
| 1575 |
-
user_message =
|
| 1576 |
-
extract_user_query_from_link_message(
|
| 1577 |
-
combined_message,
|
| 1578 |
-
link
|
| 1579 |
-
)
|
| 1580 |
-
)
|
| 1581 |
|
| 1582 |
-
|
| 1583 |
-
|
| 1584 |
-
|
| 1585 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1586 |
|
| 1587 |
print(
|
| 1588 |
-
|
| 1589 |
repr(link)
|
| 1590 |
)
|
| 1591 |
|
| 1592 |
print(
|
| 1593 |
-
|
| 1594 |
repr(user_message)
|
| 1595 |
)
|
| 1596 |
|
| 1597 |
if not link:
|
| 1598 |
-
|
| 1599 |
response_message = (
|
| 1600 |
-
|
| 1601 |
-
|
| 1602 |
)
|
| 1603 |
-
|
| 1604 |
else:
|
| 1605 |
-
|
| 1606 |
-
|
|
|
|
| 1607 |
|
| 1608 |
print(
|
| 1609 |
-
|
| 1610 |
type(result)
|
| 1611 |
)
|
| 1612 |
|
| 1613 |
print(
|
| 1614 |
-
|
| 1615 |
-
str(result)[:
|
| 1616 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1617 |
|
| 1618 |
if (
|
| 1619 |
isinstance(result, str)
|
| 1620 |
and (
|
| 1621 |
-
result
|
| 1622 |
-
or result.startswith(
|
| 1623 |
-
"Unable to access webpage"
|
| 1624 |
-
)
|
| 1625 |
-
or result.startswith(
|
| 1626 |
-
"An error occurred while reading"
|
| 1627 |
-
)
|
| 1628 |
-
or result.startswith(
|
| 1629 |
-
"Unable to retrieve the YouTube transcript"
|
| 1630 |
-
)
|
| 1631 |
-
or result.startswith(
|
| 1632 |
-
"Invalid YouTube video URL"
|
| 1633 |
-
)
|
| 1634 |
or result.startswith(
|
| 1635 |
-
|
| 1636 |
)
|
| 1637 |
or result.startswith(
|
| 1638 |
-
|
| 1639 |
)
|
| 1640 |
or result.startswith(
|
| 1641 |
-
|
| 1642 |
)
|
| 1643 |
)
|
| 1644 |
):
|
| 1645 |
-
|
| 1646 |
response_message = result
|
| 1647 |
-
|
| 1648 |
else:
|
| 1649 |
-
|
| 1650 |
content_data = normalize_text(
|
| 1651 |
result
|
| 1652 |
)
|
| 1653 |
|
| 1654 |
if not content_data:
|
| 1655 |
-
|
| 1656 |
response_message = (
|
| 1657 |
-
|
| 1658 |
-
|
| 1659 |
)
|
| 1660 |
-
|
| 1661 |
else:
|
| 1662 |
-
|
| 1663 |
augmented_prompt = f"""
|
| 1664 |
-
You are an AI designed to help understand
|
| 1665 |
-
and extract information from provided Link Content.
|
| 1666 |
|
| 1667 |
Based on the user's Query, answer using the provided Link Content.
|
| 1668 |
|
| 1669 |
Query:
|
| 1670 |
-
{user_message}
|
| 1671 |
|
| 1672 |
Link Content:
|
| 1673 |
{content_data}
|
| 1674 |
|
| 1675 |
-
|
| 1676 |
-
Do not
|
| 1677 |
-
|
| 1678 |
"""
|
| 1679 |
|
| 1680 |
message = HumanMessage(
|
|
@@ -1688,23 +1314,36 @@ as part of the URL.
|
|
| 1688 |
messages4 = messages4[-1:]
|
| 1689 |
|
| 1690 |
try:
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
chat_model.invoke(
|
| 1694 |
-
messages4
|
| 1695 |
-
)
|
| 1696 |
)
|
| 1697 |
-
|
| 1698 |
except Exception as e:
|
| 1699 |
-
|
| 1700 |
print(
|
| 1701 |
-
|
| 1702 |
repr(e)
|
| 1703 |
)
|
| 1704 |
|
| 1705 |
-
|
| 1706 |
-
|
| 1707 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1708 |
|
| 1709 |
messages4.append(
|
| 1710 |
AIMessage(
|
|
@@ -1712,21 +1351,16 @@ as part of the URL.
|
|
| 1712 |
)
|
| 1713 |
)
|
| 1714 |
|
| 1715 |
-
response_message =
|
| 1716 |
-
response.content
|
| 1717 |
-
)
|
| 1718 |
|
| 1719 |
history.append({
|
| 1720 |
-
|
| 1721 |
-
|
| 1722 |
})
|
| 1723 |
|
| 1724 |
for character in response_message:
|
| 1725 |
-
|
| 1726 |
-
history[-1]["content"] += character
|
| 1727 |
-
|
| 1728 |
time.sleep(0.0025)
|
| 1729 |
-
|
| 1730 |
yield history, messages4
|
| 1731 |
|
| 1732 |
|
|
@@ -1735,7 +1369,7 @@ def insert_line_breaks(
|
|
| 1735 |
every=8
|
| 1736 |
):
|
| 1737 |
|
| 1738 |
-
return
|
| 1739 |
text[i:i + every]
|
| 1740 |
for i in range(
|
| 1741 |
0,
|
|
@@ -1748,15 +1382,15 @@ def insert_line_breaks(
|
|
| 1748 |
def display_file_name(file):
|
| 1749 |
|
| 1750 |
supported_extensions = [
|
| 1751 |
-
|
| 1752 |
-
|
| 1753 |
-
|
| 1754 |
-
|
| 1755 |
-
|
| 1756 |
-
|
| 1757 |
-
|
| 1758 |
-
|
| 1759 |
-
|
| 1760 |
]
|
| 1761 |
|
| 1762 |
file_extension = os.path.splitext(
|
|
@@ -1779,8 +1413,8 @@ def display_file_name(file):
|
|
| 1779 |
)
|
| 1780 |
|
| 1781 |
icon_url = (
|
| 1782 |
-
|
| 1783 |
-
|
| 1784 |
)
|
| 1785 |
|
| 1786 |
return (
|
|
@@ -1797,11 +1431,14 @@ def display_file_name(file):
|
|
| 1797 |
"</b></div>"
|
| 1798 |
)
|
| 1799 |
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
| 1804 |
-
|
|
|
|
|
|
|
|
|
|
| 1805 |
|
| 1806 |
|
| 1807 |
def File_Interact(
|
|
@@ -1811,7 +1448,7 @@ def File_Interact(
|
|
| 1811 |
):
|
| 1812 |
|
| 1813 |
combined_message = str(
|
| 1814 |
-
history[-1][
|
| 1815 |
)
|
| 1816 |
|
| 1817 |
if isinstance(
|
|
@@ -1820,24 +1457,25 @@ def File_Interact(
|
|
| 1820 |
):
|
| 1821 |
messages5 = messages5[:-2]
|
| 1822 |
|
| 1823 |
-
|
|
|
|
| 1824 |
|
| 1825 |
-
if
|
| 1826 |
|
| 1827 |
-
|
| 1828 |
combined_message.split(
|
| 1829 |
-
|
| 1830 |
1
|
| 1831 |
)
|
| 1832 |
)
|
| 1833 |
|
| 1834 |
-
user_message = (
|
| 1835 |
-
user_message.strip()
|
| 1836 |
-
)
|
| 1837 |
|
| 1838 |
-
result = read_data(
|
|
|
|
|
|
|
| 1839 |
|
| 1840 |
-
if result ==
|
| 1841 |
|
| 1842 |
response_message = result
|
| 1843 |
|
|
@@ -1850,8 +1488,8 @@ def File_Interact(
|
|
| 1850 |
if not content_data:
|
| 1851 |
|
| 1852 |
response_message = (
|
| 1853 |
-
|
| 1854 |
-
|
| 1855 |
)
|
| 1856 |
|
| 1857 |
else:
|
|
@@ -1894,14 +1532,41 @@ information from other sources.
|
|
| 1894 |
|
| 1895 |
except Exception as e:
|
| 1896 |
|
|
|
|
|
|
|
| 1897 |
print(
|
| 1898 |
-
|
| 1899 |
repr(e)
|
| 1900 |
)
|
| 1901 |
|
| 1902 |
-
|
| 1903 |
-
|
| 1904 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1905 |
|
| 1906 |
messages5.append(
|
| 1907 |
AIMessage(
|
|
@@ -1914,25 +1579,26 @@ information from other sources.
|
|
| 1914 |
)
|
| 1915 |
|
| 1916 |
history.append({
|
| 1917 |
-
|
| 1918 |
-
|
| 1919 |
})
|
| 1920 |
|
| 1921 |
for character in response_message:
|
| 1922 |
|
| 1923 |
-
history[-1][
|
| 1924 |
|
| 1925 |
time.sleep(0.0025)
|
| 1926 |
|
| 1927 |
yield history, messages5
|
| 1928 |
|
| 1929 |
|
|
|
|
| 1930 |
def Explore_WebSite(
|
| 1931 |
history,
|
| 1932 |
messages6
|
| 1933 |
):
|
| 1934 |
|
| 1935 |
-
message = history[-1][
|
| 1936 |
|
| 1937 |
if isinstance(
|
| 1938 |
messages6[-1],
|
|
@@ -1941,10 +1607,10 @@ def Explore_WebSite(
|
|
| 1941 |
messages6 = messages6[:-2]
|
| 1942 |
|
| 1943 |
links = [
|
| 1944 |
-
|
| 1945 |
]
|
| 1946 |
|
| 1947 |
-
result =
|
| 1948 |
[
|
| 1949 |
read_data(link)
|
| 1950 |
for link in links
|
|
@@ -1990,14 +1656,37 @@ information from other sources.
|
|
| 1990 |
|
| 1991 |
except Exception as e:
|
| 1992 |
|
|
|
|
|
|
|
| 1993 |
print(
|
| 1994 |
-
|
| 1995 |
repr(e)
|
| 1996 |
)
|
| 1997 |
|
| 1998 |
-
|
| 1999 |
-
|
| 2000 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2001 |
|
| 2002 |
messages6.append(
|
| 2003 |
AIMessage(
|
|
@@ -2006,19 +1695,20 @@ information from other sources.
|
|
| 2006 |
)
|
| 2007 |
|
| 2008 |
history.append({
|
| 2009 |
-
|
| 2010 |
-
|
| 2011 |
})
|
| 2012 |
|
| 2013 |
for character in response.content:
|
| 2014 |
|
| 2015 |
-
history[-1][
|
| 2016 |
|
| 2017 |
time.sleep(0.0025)
|
| 2018 |
|
| 2019 |
yield history, messages6
|
| 2020 |
|
| 2021 |
|
|
|
|
| 2022 |
with gr.Blocks() as demo:
|
| 2023 |
|
| 2024 |
with gr.Row():
|
|
@@ -2045,14 +1735,15 @@ with gr.Blocks() as demo:
|
|
| 2045 |
"""
|
| 2046 |
)
|
| 2047 |
|
| 2048 |
-
|
|
|
|
| 2049 |
|
| 2050 |
messages1 = gr.State(
|
| 2051 |
messages1_state
|
| 2052 |
)
|
| 2053 |
|
| 2054 |
chatbot = gr.Chatbot(
|
| 2055 |
-
elem_id=
|
| 2056 |
height=500,
|
| 2057 |
placeholder=(
|
| 2058 |
"<span style='font-weight:bold;"
|
|
@@ -2068,13 +1759,13 @@ with gr.Blocks() as demo:
|
|
| 2068 |
|
| 2069 |
msg = gr.Textbox(
|
| 2070 |
show_label=False,
|
| 2071 |
-
placeholder=
|
| 2072 |
scale=10,
|
| 2073 |
container=False
|
| 2074 |
)
|
| 2075 |
|
| 2076 |
submit = gr.Button(
|
| 2077 |
-
|
| 2078 |
scale=1
|
| 2079 |
)
|
| 2080 |
|
|
@@ -2112,14 +1803,15 @@ with gr.Blocks() as demo:
|
|
| 2112 |
None
|
| 2113 |
)
|
| 2114 |
|
| 2115 |
-
|
|
|
|
| 2116 |
|
| 2117 |
messages2 = gr.State(
|
| 2118 |
messages1_state
|
| 2119 |
)
|
| 2120 |
|
| 2121 |
chatbot = gr.Chatbot(
|
| 2122 |
-
elem_id=
|
| 2123 |
height=500,
|
| 2124 |
placeholder=(
|
| 2125 |
"<span style='font-weight:bold;"
|
|
@@ -2136,13 +1828,13 @@ with gr.Blocks() as demo:
|
|
| 2136 |
|
| 2137 |
msg = gr.Textbox(
|
| 2138 |
show_label=False,
|
| 2139 |
-
placeholder=
|
| 2140 |
scale=10,
|
| 2141 |
container=False
|
| 2142 |
)
|
| 2143 |
|
| 2144 |
submit = gr.Button(
|
| 2145 |
-
|
| 2146 |
scale=1
|
| 2147 |
)
|
| 2148 |
|
|
@@ -2180,14 +1872,15 @@ with gr.Blocks() as demo:
|
|
| 2180 |
None
|
| 2181 |
)
|
| 2182 |
|
| 2183 |
-
with gr.Tab("Chart-Generator"):
|
| 2184 |
|
|
|
|
| 2185 |
messages3 = gr.State(
|
| 2186 |
messages1_state
|
| 2187 |
)
|
|
|
|
| 2188 |
|
| 2189 |
chatbot = gr.Chatbot(
|
| 2190 |
-
elem_id=
|
| 2191 |
height=500,
|
| 2192 |
placeholder=(
|
| 2193 |
"<span style='font-weight:bold;"
|
|
@@ -2201,28 +1894,28 @@ with gr.Blocks() as demo:
|
|
| 2201 |
)
|
| 2202 |
|
| 2203 |
with gr.Row():
|
| 2204 |
-
|
| 2205 |
msg = gr.Textbox(
|
| 2206 |
show_label=False,
|
| 2207 |
placeholder=(
|
| 2208 |
-
|
| 2209 |
-
|
| 2210 |
-
|
| 2211 |
-
|
| 2212 |
),
|
| 2213 |
scale=10,
|
| 2214 |
container=False
|
| 2215 |
)
|
| 2216 |
|
| 2217 |
submit = gr.Button(
|
| 2218 |
-
|
| 2219 |
scale=1
|
| 2220 |
)
|
| 2221 |
|
| 2222 |
clear = gr.ClearButton([
|
| 2223 |
msg,
|
| 2224 |
chatbot,
|
| 2225 |
-
messages3
|
|
|
|
| 2226 |
])
|
| 2227 |
|
| 2228 |
msg.submit(
|
|
@@ -2232,8 +1925,8 @@ with gr.Blocks() as demo:
|
|
| 2232 |
queue=True
|
| 2233 |
).then(
|
| 2234 |
Chart_Generator,
|
| 2235 |
-
[chatbot, messages3],
|
| 2236 |
-
[chatbot, messages3]
|
| 2237 |
)
|
| 2238 |
|
| 2239 |
submit.click(
|
|
@@ -2243,8 +1936,8 @@ with gr.Blocks() as demo:
|
|
| 2243 |
queue=True
|
| 2244 |
).then(
|
| 2245 |
Chart_Generator,
|
| 2246 |
-
[chatbot, messages3],
|
| 2247 |
-
[chatbot, messages3]
|
| 2248 |
)
|
| 2249 |
|
| 2250 |
chatbot.like(
|
|
@@ -2253,14 +1946,15 @@ with gr.Blocks() as demo:
|
|
| 2253 |
None
|
| 2254 |
)
|
| 2255 |
|
| 2256 |
-
|
|
|
|
| 2257 |
|
| 2258 |
messages4 = gr.State(
|
| 2259 |
messages1_state
|
| 2260 |
)
|
| 2261 |
|
| 2262 |
chatbot = gr.Chatbot(
|
| 2263 |
-
elem_id=
|
| 2264 |
height=500,
|
| 2265 |
placeholder=(
|
| 2266 |
"<span style='font-weight:bold;"
|
|
@@ -2277,20 +1971,20 @@ with gr.Blocks() as demo:
|
|
| 2277 |
|
| 2278 |
msg1 = gr.Textbox(
|
| 2279 |
show_label=False,
|
| 2280 |
-
placeholder=
|
| 2281 |
scale=4,
|
| 2282 |
container=False
|
| 2283 |
)
|
| 2284 |
|
| 2285 |
msg2 = gr.Textbox(
|
| 2286 |
show_label=False,
|
| 2287 |
-
placeholder=
|
| 2288 |
scale=7,
|
| 2289 |
container=False
|
| 2290 |
)
|
| 2291 |
|
| 2292 |
submit = gr.Button(
|
| 2293 |
-
|
| 2294 |
scale=1
|
| 2295 |
)
|
| 2296 |
|
|
@@ -2340,14 +2034,15 @@ with gr.Blocks() as demo:
|
|
| 2340 |
None
|
| 2341 |
)
|
| 2342 |
|
| 2343 |
-
|
|
|
|
| 2344 |
|
| 2345 |
messages5 = gr.State(
|
| 2346 |
messages1_state
|
| 2347 |
)
|
| 2348 |
|
| 2349 |
chatbot = gr.Chatbot(
|
| 2350 |
-
elem_id=
|
| 2351 |
height=500,
|
| 2352 |
placeholder=(
|
| 2353 |
"<span style='font-weight:bold;"
|
|
@@ -2367,23 +2062,23 @@ with gr.Blocks() as demo:
|
|
| 2367 |
with gr.Row():
|
| 2368 |
|
| 2369 |
filepath = gr.UploadButton(
|
| 2370 |
-
|
| 2371 |
-
file_count=
|
| 2372 |
scale=1
|
| 2373 |
)
|
| 2374 |
|
| 2375 |
msg = gr.Textbox(
|
| 2376 |
show_label=False,
|
| 2377 |
placeholder=(
|
| 2378 |
-
|
| 2379 |
-
|
| 2380 |
),
|
| 2381 |
scale=7,
|
| 2382 |
container=False
|
| 2383 |
)
|
| 2384 |
|
| 2385 |
submit = gr.Button(
|
| 2386 |
-
|
| 2387 |
scale=1
|
| 2388 |
)
|
| 2389 |
|
|
@@ -2439,14 +2134,15 @@ with gr.Blocks() as demo:
|
|
| 2439 |
None
|
| 2440 |
)
|
| 2441 |
|
| 2442 |
-
|
|
|
|
| 2443 |
|
| 2444 |
messages6 = gr.State(
|
| 2445 |
messages1_state
|
| 2446 |
)
|
| 2447 |
|
| 2448 |
chatbot = gr.Chatbot(
|
| 2449 |
-
elem_id=
|
| 2450 |
height=500,
|
| 2451 |
placeholder=(
|
| 2452 |
"<span style='font-weight:bold;"
|
|
@@ -2462,13 +2158,13 @@ with gr.Blocks() as demo:
|
|
| 2462 |
|
| 2463 |
msg = gr.Textbox(
|
| 2464 |
show_label=False,
|
| 2465 |
-
placeholder=
|
| 2466 |
scale=10,
|
| 2467 |
container=False
|
| 2468 |
)
|
| 2469 |
|
| 2470 |
submit = gr.Button(
|
| 2471 |
-
|
| 2472 |
scale=1
|
| 2473 |
)
|
| 2474 |
|
|
@@ -2513,7 +2209,7 @@ demo.queue(
|
|
| 2513 |
)
|
| 2514 |
|
| 2515 |
demo.launch(
|
| 2516 |
-
max_file_size=
|
| 2517 |
max_threads=50,
|
| 2518 |
theme=gr.themes.Soft()
|
| 2519 |
)
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import re
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import time
|
| 7 |
import xml.etree.ElementTree as ET
|
|
|
|
| 23 |
import pptx
|
| 24 |
import PyPDF2
|
| 25 |
import requests
|
| 26 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 27 |
+
from youtube_transcript_api._errors import (
|
| 28 |
+
NoTranscriptFound,
|
| 29 |
+
TranscriptsDisabled,
|
| 30 |
+
VideoUnavailable,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
|
| 34 |
+
nltk.download('punkt')
|
| 35 |
+
nltk.download('punkt_tab')
|
| 36 |
+
nltk.download('omw-1.4')
|
| 37 |
+
nltk.download('wordnet')
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
def read_csv(file_path):
|
| 42 |
with open(
|
| 43 |
file_path,
|
| 44 |
+
'r',
|
| 45 |
+
encoding='utf-8',
|
| 46 |
+
errors='ignore',
|
| 47 |
+
newline=''
|
| 48 |
) as csvfile:
|
| 49 |
csv_reader = csv.reader(csvfile)
|
| 50 |
csv_data = [row for row in csv_reader]
|
| 51 |
|
| 52 |
+
return ' '.join([' '.join(row) for row in csv_data])
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
def read_text(file_path):
|
| 56 |
with open(
|
| 57 |
file_path,
|
| 58 |
+
'r',
|
| 59 |
+
encoding='utf-8',
|
| 60 |
+
errors='ignore'
|
| 61 |
) as f:
|
| 62 |
return f.read()
|
| 63 |
|
|
|
|
| 65 |
def read_pdf(file_path):
|
| 66 |
text_data = []
|
| 67 |
|
| 68 |
+
with open(file_path, 'rb') as pdf_file:
|
| 69 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
for page in pdf_reader.pages:
|
| 72 |
+
text_data.append(page.extract_text())
|
| 73 |
|
| 74 |
+
return '\n'.join(text_data)
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
def read_docx(file_path):
|
| 78 |
doc = docx.Document(file_path)
|
| 79 |
|
| 80 |
+
return '\n'.join(
|
| 81 |
+
[paragraph.text for paragraph in doc.paragraphs]
|
|
|
|
|
|
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
|
| 85 |
def read_pptx(file_path):
|
| 86 |
ppt = pptx.Presentation(file_path)
|
| 87 |
+
text_data = ''
|
| 88 |
|
| 89 |
for slide in ppt.slides:
|
| 90 |
for shape in slide.shapes:
|
| 91 |
+
if hasattr(shape, 'text'):
|
| 92 |
+
text_data += shape.text + '\n'
|
|
|
|
|
|
|
| 93 |
|
| 94 |
return text_data
|
| 95 |
|
| 96 |
|
| 97 |
def read_xlsx(file_path):
|
| 98 |
+
workbook = openpyxl.load_workbook(file_path)
|
|
|
|
|
|
|
|
|
|
| 99 |
sheet = workbook.active
|
| 100 |
+
text_data = ''
|
| 101 |
|
| 102 |
+
for row in sheet.iter_rows(values_only=True):
|
|
|
|
|
|
|
| 103 |
text_data += (
|
| 104 |
+
' '.join(
|
| 105 |
+
[str(cell) for cell in row if cell is not None]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
+
+ '\n'
|
| 108 |
)
|
| 109 |
|
| 110 |
return text_data
|
| 111 |
|
| 112 |
|
| 113 |
def read_json(file_path):
|
| 114 |
+
with open(file_path, 'r') as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
json_data = json.load(f)
|
| 116 |
|
| 117 |
+
return json.dumps(json_data)
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
def read_html(file_path):
|
| 121 |
+
with open(file_path, 'r') as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
html_content = f.read()
|
| 123 |
|
| 124 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
return soup
|
|
|
|
|
|
|
| 127 |
|
| 128 |
|
| 129 |
def read_xml(file_path):
|
|
|
|
| 132 |
|
| 133 |
return ET.tostring(
|
| 134 |
root,
|
| 135 |
+
encoding='unicode'
|
| 136 |
)
|
| 137 |
|
| 138 |
|
| 139 |
def extract_youtube_video_id(url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
parsed = urlparse(url)
|
| 141 |
+
hostname = (parsed.hostname or '').lower()
|
| 142 |
|
| 143 |
+
if hostname in ('youtu.be', 'www.youtu.be'):
|
| 144 |
+
video_id = parsed.path.lstrip('/').split('/')[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return video_id or None
|
| 146 |
|
| 147 |
+
if hostname.endswith('youtube.com'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
if parsed.path == '/watch':
|
| 150 |
video_ids = parse_qs(
|
| 151 |
parsed.query
|
| 152 |
+
).get('v')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
return video_ids[0] if video_ids else None
|
| 155 |
|
| 156 |
for prefix in (
|
| 157 |
+
'/shorts/',
|
| 158 |
+
'/embed/',
|
| 159 |
+
'/live/'
|
| 160 |
):
|
| 161 |
+
if parsed.path.startswith(prefix):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
video_id = (
|
| 163 |
+
parsed.path[len(prefix):]
|
| 164 |
+
.split('/')[0]
|
|
|
|
|
|
|
|
|
|
| 165 |
)
|
| 166 |
|
| 167 |
return video_id or None
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
def is_youtube_url(url):
|
| 173 |
+
return extract_youtube_video_id(url) is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
def fetch_transcript_text(video_id, lang):
|
| 177 |
+
if hasattr(
|
| 178 |
+
YouTubeTranscriptApi,
|
| 179 |
+
'get_transcript'
|
| 180 |
+
):
|
| 181 |
+
transcript = YouTubeTranscriptApi.get_transcript(
|
| 182 |
+
video_id,
|
| 183 |
+
languages=[lang]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
)
|
| 185 |
|
| 186 |
+
return ' '.join(
|
| 187 |
+
entry['text']
|
| 188 |
+
for entry in transcript
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
)
|
| 190 |
|
| 191 |
+
ytt_api = YouTubeTranscriptApi()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
fetched = ytt_api.fetch(
|
| 194 |
+
video_id,
|
| 195 |
+
languages=[lang]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
)
|
| 197 |
|
| 198 |
+
return ' '.join(
|
| 199 |
+
snippet.text
|
| 200 |
+
for snippet in fetched
|
| 201 |
)
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
def process_youtube_video(
|
| 205 |
+
url,
|
| 206 |
+
languages=['en', 'ar']
|
| 207 |
+
):
|
| 208 |
+
video_id = extract_youtube_video_id(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
if not video_id:
|
| 211 |
+
return (
|
| 212 |
+
'Invalid YouTube video URL. '
|
| 213 |
+
'Please provide a valid YouTube video link.'
|
| 214 |
)
|
| 215 |
|
| 216 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
response = requests.get(
|
| 219 |
+
f'http://img.youtube.com/vi/'
|
| 220 |
+
f'{video_id}/mqdefault.jpg',
|
| 221 |
+
timeout=15
|
| 222 |
+
)
|
| 223 |
|
| 224 |
+
if response.status_code != 200:
|
| 225 |
+
return "Video doesn't exist."
|
| 226 |
|
| 227 |
+
transcript_data = []
|
|
|
|
|
|
|
| 228 |
|
| 229 |
+
for lang in languages:
|
|
|
|
|
|
|
| 230 |
|
| 231 |
+
try:
|
| 232 |
+
transcript_data.append(
|
| 233 |
+
fetch_transcript_text(
|
| 234 |
+
video_id,
|
| 235 |
+
lang
|
| 236 |
)
|
| 237 |
)
|
| 238 |
|
| 239 |
+
except (
|
| 240 |
+
NoTranscriptFound,
|
| 241 |
+
TranscriptsDisabled,
|
| 242 |
+
VideoUnavailable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
):
|
| 244 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
return (
|
| 247 |
+
' '.join(transcript_data)
|
| 248 |
+
if transcript_data
|
| 249 |
+
else (
|
| 250 |
+
'Please choose a YouTube video with '
|
| 251 |
+
'available English or Arabic transcripts.'
|
| 252 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
)
|
| 254 |
|
| 255 |
except Exception as e:
|
| 256 |
+
return f'An error occurred: {e}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
|
| 259 |
def read_web_page(url):
|
| 260 |
+
|
| 261 |
try:
|
| 262 |
|
| 263 |
+
url = url.strip()
|
| 264 |
|
| 265 |
response = requests.get(
|
| 266 |
url,
|
| 267 |
headers={
|
| 268 |
+
'User-Agent': (
|
| 269 |
+
'Mozilla/5.0 '
|
| 270 |
+
'(Windows NT 10.0; Win64; x64) '
|
| 271 |
+
'AppleWebKit/537.36 '
|
| 272 |
+
'(KHTML, like Gecko) '
|
| 273 |
+
'Chrome/151.0.0.0 Safari/537.36'
|
| 274 |
)
|
| 275 |
},
|
| 276 |
timeout=20,
|
|
|
|
| 278 |
)
|
| 279 |
|
| 280 |
if response.status_code >= 400:
|
|
|
|
| 281 |
return (
|
| 282 |
+
f'Unable to access webpage. '
|
| 283 |
+
f'HTTP status: {response.status_code}'
|
| 284 |
)
|
| 285 |
|
| 286 |
content_type = (
|
| 287 |
response.headers
|
| 288 |
+
.get('content-type', '')
|
| 289 |
.lower()
|
| 290 |
)
|
| 291 |
|
| 292 |
+
if 'text/plain' in content_type:
|
| 293 |
return response.text
|
| 294 |
|
| 295 |
+
src = response.text
|
| 296 |
+
|
| 297 |
soup = BeautifulSoup(
|
| 298 |
+
src,
|
| 299 |
+
'html.parser'
|
| 300 |
)
|
| 301 |
|
| 302 |
+
for element in soup([
|
| 303 |
+
'script',
|
| 304 |
+
'style',
|
| 305 |
+
'noscript',
|
| 306 |
+
'svg'
|
| 307 |
+
]):
|
|
|
|
|
|
|
| 308 |
element.decompose()
|
| 309 |
|
| 310 |
text_data = soup.get_text(
|
| 311 |
+
separator='\n'
|
| 312 |
)
|
| 313 |
|
| 314 |
+
text_data = '\n'.join(
|
| 315 |
line.strip()
|
| 316 |
for line in text_data.splitlines()
|
| 317 |
if line.strip()
|
| 318 |
)
|
| 319 |
|
| 320 |
if not text_data:
|
|
|
|
| 321 |
return (
|
| 322 |
+
'The webpage was accessed successfully '
|
| 323 |
+
'but contains no readable text.'
|
| 324 |
)
|
| 325 |
|
| 326 |
return text_data
|
| 327 |
|
| 328 |
except requests.exceptions.Timeout:
|
|
|
|
| 329 |
return (
|
| 330 |
+
'The webpage request timed out. '
|
| 331 |
+
'Please try another link.'
|
| 332 |
)
|
| 333 |
|
| 334 |
except requests.exceptions.RequestException as e:
|
| 335 |
+
return f'Unable to access webpage: {e}'
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
except Exception as e:
|
| 338 |
+
return f'An error occurred while reading the webpage: {e}'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
|
| 341 |
def read_data(
|
| 342 |
file_path_or_url,
|
| 343 |
+
languages=['en', 'ar']
|
| 344 |
):
|
| 345 |
|
| 346 |
if not file_path_or_url:
|
| 347 |
+
return 'Unsupported type or format.'
|
| 348 |
|
| 349 |
file_path_or_url = str(
|
| 350 |
file_path_or_url
|
| 351 |
).strip()
|
| 352 |
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
+
if is_youtube_url(file_path_or_url):
|
| 355 |
return process_youtube_video(
|
| 356 |
+
file_path_or_url,
|
| 357 |
+
languages
|
| 358 |
)
|
| 359 |
|
| 360 |
if file_path_or_url.startswith(
|
| 361 |
+
('http://', 'https://')
|
|
|
|
|
|
|
|
|
|
| 362 |
):
|
|
|
|
| 363 |
return read_web_page(
|
| 364 |
file_path_or_url
|
| 365 |
)
|
| 366 |
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
+
lower_path = file_path_or_url.lower()
|
| 369 |
+
|
| 370 |
+
if lower_path.endswith('.csv'):
|
| 371 |
return read_csv(file_path_or_url)
|
| 372 |
|
| 373 |
+
elif lower_path.endswith('.txt'):
|
| 374 |
return read_text(file_path_or_url)
|
| 375 |
|
| 376 |
+
elif lower_path.endswith('.pdf'):
|
| 377 |
return read_pdf(file_path_or_url)
|
| 378 |
|
| 379 |
+
elif lower_path.endswith('.docx'):
|
| 380 |
return read_docx(file_path_or_url)
|
| 381 |
|
| 382 |
+
elif lower_path.endswith('.pptx'):
|
| 383 |
return read_pptx(file_path_or_url)
|
| 384 |
|
| 385 |
+
elif lower_path.endswith('.xlsx'):
|
| 386 |
return read_xlsx(file_path_or_url)
|
| 387 |
|
| 388 |
+
elif lower_path.endswith('.json'):
|
| 389 |
return read_json(file_path_or_url)
|
| 390 |
|
| 391 |
+
elif lower_path.endswith('.html'):
|
| 392 |
return read_html(file_path_or_url)
|
| 393 |
|
| 394 |
+
elif lower_path.endswith('.xml'):
|
| 395 |
return read_xml(file_path_or_url)
|
| 396 |
|
| 397 |
+
return 'Unsupported type or format.'
|
| 398 |
|
| 399 |
|
| 400 |
def normalize_text(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
if not isinstance(text, str):
|
| 403 |
+
text = str(text)
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
text = re.sub(
|
| 406 |
+
r'\*?',
|
| 407 |
+
'',
|
| 408 |
text
|
| 409 |
)
|
| 410 |
|
| 411 |
+
text = text.lower()
|
| 412 |
+
text = text.strip()
|
| 413 |
|
| 414 |
+
punctuation = r"""!()[]{};:'"\<>/?$%^&*_`~='"""
|
|
|
|
|
|
|
| 415 |
|
| 416 |
for punc in punctuation:
|
| 417 |
text = text.replace(
|
| 418 |
punc,
|
| 419 |
+
''
|
| 420 |
)
|
| 421 |
|
| 422 |
text = re.sub(
|
| 423 |
+
r'[A-Za-z0-9]*@[A-Za-z]*\.?[A-Za-z0-9]*',
|
| 424 |
+
'',
|
| 425 |
text
|
| 426 |
)
|
| 427 |
|
| 428 |
words = word_tokenize(text)
|
| 429 |
|
| 430 |
+
return ' '.join(words)
|
| 431 |
|
| 432 |
|
| 433 |
llm = HuggingFaceEndpoint(
|
| 434 |
+
repo_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 435 |
+
task='text-generation',
|
| 436 |
max_new_tokens=4096,
|
| 437 |
temperature=0.6,
|
| 438 |
top_p=0.9,
|
| 439 |
top_k=40,
|
| 440 |
repetition_penalty=1.2,
|
| 441 |
+
do_sample=True,
|
| 442 |
)
|
| 443 |
|
|
|
|
| 444 |
chat_model = ChatHuggingFace(
|
| 445 |
llm=llm
|
| 446 |
)
|
| 447 |
|
| 448 |
|
| 449 |
+
|
| 450 |
model_name = (
|
| 451 |
+
'sentence-transformers/all-mpnet-base-v2'
|
| 452 |
)
|
| 453 |
|
|
|
|
| 454 |
embedding_llm = HuggingFaceEmbeddings(
|
| 455 |
model_name=model_name
|
| 456 |
)
|
| 457 |
|
|
|
|
| 458 |
db = FAISS.load_local(
|
| 459 |
+
'faiss_index',
|
| 460 |
embedding_llm,
|
| 461 |
allow_dangerous_deserialization=True
|
| 462 |
)
|
| 463 |
|
| 464 |
|
| 465 |
+
|
| 466 |
+
def print_like_dislike(x: gr.LikeData):
|
|
|
|
| 467 |
print(
|
| 468 |
x.index,
|
| 469 |
x.value,
|
|
|
|
| 471 |
)
|
| 472 |
|
| 473 |
|
| 474 |
+
|
| 475 |
def user(
|
| 476 |
user_message,
|
| 477 |
history
|
|
|
|
| 479 |
|
| 480 |
if not len(user_message):
|
| 481 |
raise gr.Error(
|
| 482 |
+
'Chat messages cannot be empty'
|
| 483 |
)
|
| 484 |
|
| 485 |
history.append({
|
| 486 |
+
'role': 'user',
|
| 487 |
+
'content': user_message
|
| 488 |
})
|
| 489 |
|
| 490 |
+
return '', history
|
| 491 |
|
| 492 |
|
| 493 |
def user2(
|
|
|
|
| 496 |
link
|
| 497 |
):
|
| 498 |
|
| 499 |
+
if not len(user_message) or not len(link):
|
|
|
|
|
|
|
|
|
|
| 500 |
raise gr.Error(
|
| 501 |
+
'Chat messages or links cannot be empty'
|
| 502 |
)
|
| 503 |
|
| 504 |
link = str(link).strip()
|
| 505 |
+
user_message = str(user_message).strip()
|
|
|
|
|
|
|
| 506 |
|
| 507 |
combined_message = (
|
| 508 |
+
f'{link}\n{user_message}'
|
|
|
|
| 509 |
)
|
| 510 |
|
| 511 |
history.append({
|
| 512 |
+
'role': 'user',
|
| 513 |
+
'content': combined_message
|
| 514 |
})
|
| 515 |
|
| 516 |
+
return '', history, link
|
| 517 |
|
| 518 |
|
| 519 |
def user3(
|
|
|
|
| 522 |
file_path
|
| 523 |
):
|
| 524 |
|
| 525 |
+
if not len(user_message) or not file_path:
|
|
|
|
|
|
|
|
|
|
| 526 |
raise gr.Error(
|
| 527 |
+
'Chat messages or files cannot be empty'
|
| 528 |
)
|
| 529 |
|
| 530 |
combined_message = (
|
| 531 |
+
f'{file_path}\n{user_message}'
|
|
|
|
| 532 |
)
|
| 533 |
|
| 534 |
history.append({
|
| 535 |
+
'role': 'user',
|
| 536 |
+
'content': combined_message
|
| 537 |
})
|
| 538 |
|
| 539 |
+
return '', history, file_path
|
| 540 |
+
|
| 541 |
+
|
| 542 |
|
| 543 |
|
| 544 |
messages1_state = [
|
| 545 |
SystemMessage(
|
| 546 |
+
content='You are a helpful assistant.'
|
| 547 |
),
|
| 548 |
HumanMessage(
|
| 549 |
+
content='Hi AI, how are you today?'
|
| 550 |
),
|
| 551 |
AIMessage(
|
| 552 |
content=(
|
| 553 |
"I'm great thank you. "
|
| 554 |
"How can I help you?"
|
| 555 |
)
|
| 556 |
+
),
|
| 557 |
]
|
| 558 |
|
| 559 |
|
| 560 |
+
|
| 561 |
def Chat_Message(
|
| 562 |
history,
|
| 563 |
messages1
|
| 564 |
):
|
| 565 |
|
| 566 |
+
user_msg_text = history[-1]['content']
|
| 567 |
|
| 568 |
message = HumanMessage(
|
| 569 |
content=user_msg_text
|
|
|
|
| 588 |
|
| 589 |
except Exception as e:
|
| 590 |
|
| 591 |
+
error_message = str(e)
|
| 592 |
+
|
| 593 |
print(
|
| 594 |
+
'CHAT ERROR:',
|
| 595 |
repr(e)
|
| 596 |
)
|
| 597 |
|
| 598 |
+
start_index = error_message.find(
|
| 599 |
+
'Input validation error:'
|
| 600 |
+
)
|
| 601 |
+
|
| 602 |
+
end_index = error_message.find(
|
| 603 |
+
'and 4096 `max_new_tokens`'
|
| 604 |
+
)
|
| 605 |
+
|
| 606 |
+
if (
|
| 607 |
+
start_index != -1
|
| 608 |
+
and end_index != -1
|
| 609 |
+
):
|
| 610 |
+
raise gr.Error(
|
| 611 |
+
error_message[
|
| 612 |
+
start_index:end_index
|
| 613 |
+
].strip()
|
| 614 |
+
) from e
|
| 615 |
+
|
| 616 |
+
else:
|
| 617 |
+
raise gr.Error(
|
| 618 |
+
'Error occurred during response'
|
| 619 |
+
) from e
|
| 620 |
|
| 621 |
messages1.append(
|
| 622 |
AIMessage(
|
|
|
|
| 625 |
)
|
| 626 |
|
| 627 |
history.append({
|
| 628 |
+
'role': 'assistant',
|
| 629 |
+
'content': ''
|
| 630 |
})
|
| 631 |
|
| 632 |
for character in response.content:
|
| 633 |
|
| 634 |
+
history[-1]['content'] += character
|
| 635 |
|
| 636 |
time.sleep(0.0025)
|
| 637 |
|
| 638 |
yield history, messages1
|
| 639 |
|
| 640 |
|
| 641 |
+
|
| 642 |
def Internet_Search(
|
| 643 |
history,
|
| 644 |
messages2
|
| 645 |
):
|
| 646 |
|
| 647 |
message = str(
|
| 648 |
+
history[-1]['content']
|
| 649 |
)
|
| 650 |
|
| 651 |
if isinstance(
|
|
|
|
| 661 |
|
| 662 |
if similar_docs:
|
| 663 |
|
| 664 |
+
source_knowledge = '\n'.join(
|
| 665 |
[
|
| 666 |
x.page_content
|
| 667 |
for x in similar_docs
|
|
|
|
| 669 |
)
|
| 670 |
|
| 671 |
else:
|
| 672 |
+
source_knowledge = ''
|
| 673 |
|
| 674 |
augmented_prompt = f"""
|
| 675 |
You are an AI designed to help understand
|
|
|
|
| 706 |
|
| 707 |
except Exception as e:
|
| 708 |
|
| 709 |
+
error_message = str(e)
|
| 710 |
+
|
| 711 |
print(
|
| 712 |
+
'INTERNET SEARCH ERROR:',
|
| 713 |
repr(e)
|
| 714 |
)
|
| 715 |
|
| 716 |
+
start_index = error_message.find(
|
| 717 |
+
'Input validation error:'
|
| 718 |
+
)
|
| 719 |
+
|
| 720 |
+
end_index = error_message.find(
|
| 721 |
+
'and 4096 `max_new_tokens`'
|
| 722 |
+
)
|
| 723 |
+
|
| 724 |
+
if (
|
| 725 |
+
start_index != -1
|
| 726 |
+
and end_index != -1
|
| 727 |
+
):
|
| 728 |
+
raise gr.Error(
|
| 729 |
+
error_message[
|
| 730 |
+
start_index:end_index
|
| 731 |
+
].strip()
|
| 732 |
+
) from e
|
| 733 |
+
|
| 734 |
+
else:
|
| 735 |
+
raise gr.Error(
|
| 736 |
+
'Error occurred during response'
|
| 737 |
+
) from e
|
| 738 |
|
| 739 |
messages2.append(
|
| 740 |
AIMessage(
|
|
|
|
| 743 |
)
|
| 744 |
|
| 745 |
history.append({
|
| 746 |
+
'role': 'assistant',
|
| 747 |
+
'content': ''
|
| 748 |
})
|
| 749 |
|
| 750 |
for character in response.content:
|
| 751 |
|
| 752 |
+
history[-1]['content'] += character
|
| 753 |
|
| 754 |
time.sleep(0.0025)
|
| 755 |
|
| 756 |
yield history, messages2
|
| 757 |
|
| 758 |
|
| 759 |
+
|
| 760 |
+
|
| 761 |
def generate_chart_config(
|
| 762 |
description
|
| 763 |
):
|
|
|
|
| 764 |
system_instructions = """
|
| 765 |
You are a Chart.js configuration generator.
|
| 766 |
|
|
|
|
| 769 |
The JSON must have this structure:
|
| 770 |
|
| 771 |
{
|
| 772 |
+
"type": "bar",
|
| 773 |
+
"data": {
|
| 774 |
+
"labels": [],
|
| 775 |
+
"datasets": [
|
| 776 |
+
{
|
| 777 |
+
"label": "",
|
| 778 |
+
"data": []
|
| 779 |
+
}
|
| 780 |
+
]
|
| 781 |
+
},
|
| 782 |
+
"options": {}
|
| 783 |
}
|
| 784 |
|
| 785 |
Rules:
|
|
|
|
| 803 |
),
|
| 804 |
HumanMessage(
|
| 805 |
content=(
|
| 806 |
+
'Create a Chart.js chart for '
|
| 807 |
+
'this request:\n'
|
| 808 |
+
f'{description}'
|
| 809 |
)
|
| 810 |
+
),
|
| 811 |
]
|
| 812 |
|
| 813 |
print(
|
| 814 |
+
'CHART DESCRIPTION:',
|
| 815 |
description
|
| 816 |
)
|
| 817 |
|
|
|
|
| 824 |
).strip()
|
| 825 |
|
| 826 |
print(
|
| 827 |
+
'RAW CHART MODEL RESPONSE:',
|
| 828 |
raw
|
| 829 |
)
|
| 830 |
|
| 831 |
raw = re.sub(
|
| 832 |
+
r'^```json\s*',
|
| 833 |
+
'',
|
| 834 |
raw,
|
| 835 |
flags=re.IGNORECASE
|
| 836 |
)
|
| 837 |
|
| 838 |
raw = re.sub(
|
| 839 |
+
r'^```\s*',
|
| 840 |
+
'',
|
| 841 |
raw
|
| 842 |
)
|
| 843 |
|
| 844 |
raw = re.sub(
|
| 845 |
+
r'\s*```$',
|
| 846 |
+
'',
|
| 847 |
raw
|
| 848 |
)
|
| 849 |
|
| 850 |
raw = raw.strip()
|
| 851 |
|
| 852 |
+
start = raw.find('{')
|
| 853 |
+
end = raw.rfind('}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
|
| 855 |
+
if start == -1 or end == -1:
|
| 856 |
raise ValueError(
|
| 857 |
+
'Model did not return a JSON object.\n'
|
| 858 |
+
f'Raw response:\n{raw}'
|
| 859 |
)
|
| 860 |
|
| 861 |
raw = raw[
|
| 862 |
start:end + 1
|
| 863 |
]
|
| 864 |
|
| 865 |
+
print(
|
| 866 |
+
'CLEANED CHART JSON:',
|
| 867 |
+
raw
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
)
|
| 869 |
|
| 870 |
+
config = json.loads(raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 871 |
|
| 872 |
+
if 'type' not in config:
|
| 873 |
+
raise ValueError(
|
| 874 |
+
"Chart config missing 'type'"
|
|
|
|
| 875 |
)
|
| 876 |
|
| 877 |
+
if 'data' not in config:
|
| 878 |
+
raise ValueError(
|
| 879 |
+
"Chart config missing 'data'"
|
| 880 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
|
| 882 |
+
if 'labels' not in config['data']:
|
| 883 |
+
raise ValueError(
|
| 884 |
+
"Chart config missing 'data.labels'"
|
| 885 |
+
)
|
| 886 |
|
| 887 |
+
if 'datasets' not in config['data']:
|
| 888 |
+
raise ValueError(
|
| 889 |
+
"Chart config missing 'data.datasets'"
|
| 890 |
+
)
|
| 891 |
|
| 892 |
+
if not isinstance(
|
| 893 |
+
config['data']['datasets'],
|
| 894 |
+
list
|
| 895 |
+
):
|
| 896 |
+
raise ValueError(
|
| 897 |
+
"'data.datasets' must be a list"
|
| 898 |
+
)
|
| 899 |
|
| 900 |
+
return config
|
| 901 |
|
|
|
|
| 902 |
|
| 903 |
+
def Chart_Generator(
|
| 904 |
+
history,
|
| 905 |
+
messages3,
|
| 906 |
+
chart_state
|
| 907 |
+
):
|
| 908 |
+
message = str(
|
| 909 |
+
history[-1]['content']
|
| 910 |
+
)
|
| 911 |
|
| 912 |
if isinstance(
|
| 913 |
+
messages3[-1],
|
| 914 |
+
HumanMessage
|
| 915 |
):
|
| 916 |
+
messages3 = messages3[:-1]
|
| 917 |
|
| 918 |
+
if '#chart' in message.lower():
|
| 919 |
+
chart_description = re.split(
|
| 920 |
+
r'#chart',
|
| 921 |
+
message,
|
| 922 |
+
maxsplit=1,
|
| 923 |
+
flags=re.IGNORECASE
|
| 924 |
+
)[1].strip()
|
| 925 |
|
| 926 |
+
if not chart_description:
|
| 927 |
+
combined_content = (
|
| 928 |
+
'Please provide chart details after #chart.'
|
| 929 |
+
)
|
| 930 |
+
chart_state = None
|
| 931 |
+
else:
|
| 932 |
+
try:
|
| 933 |
+
chart_config = generate_chart_config(
|
| 934 |
+
chart_description
|
| 935 |
+
)
|
| 936 |
|
| 937 |
+
print(
|
| 938 |
+
'VALID CHART CONFIG:',
|
| 939 |
+
json.dumps(
|
| 940 |
+
chart_config,
|
| 941 |
+
indent=2
|
|
|
|
| 942 |
)
|
| 943 |
)
|
| 944 |
|
| 945 |
+
chart_state = chart_config
|
| 946 |
|
| 947 |
+
config_json = json.dumps(
|
| 948 |
+
chart_config,
|
| 949 |
+
separators=(',', ':')
|
| 950 |
)
|
| 951 |
|
| 952 |
+
encoded_config = requests.utils.quote(
|
| 953 |
+
config_json,
|
| 954 |
+
safe=''
|
| 955 |
+
)
|
| 956 |
|
| 957 |
+
chart_url = (
|
| 958 |
+
'https://quickchart.io/chart'
|
| 959 |
+
f'?c={encoded_config}'
|
| 960 |
+
'&bkg=white'
|
| 961 |
+
)
|
| 962 |
|
| 963 |
+
print(
|
| 964 |
+
'QUICKCHART URL:',
|
| 965 |
+
chart_url
|
| 966 |
+
)
|
|
|
|
|
|
|
| 967 |
|
| 968 |
+
chart_response = requests.get(
|
| 969 |
+
chart_url,
|
| 970 |
+
timeout=30
|
| 971 |
+
)
|
| 972 |
|
| 973 |
+
print(
|
| 974 |
+
'QUICKCHART STATUS:',
|
| 975 |
+
chart_response.status_code
|
| 976 |
+
)
|
| 977 |
|
| 978 |
+
if chart_response.status_code != 200:
|
| 979 |
+
print(
|
| 980 |
+
'QUICKCHART RESPONSE:',
|
| 981 |
+
chart_response.text[:1000]
|
| 982 |
+
)
|
| 983 |
|
| 984 |
+
combined_content = (
|
| 985 |
+
'QuickChart failed to generate '
|
| 986 |
+
'the chart.\n\n'
|
| 987 |
+
f'HTTP Status: '
|
| 988 |
+
f'{chart_response.status_code}'
|
| 989 |
+
)
|
| 990 |
+
else:
|
| 991 |
+
image_html = (
|
| 992 |
+
f'<img src="{chart_url}" '
|
| 993 |
+
'alt="Generated Chart" '
|
| 994 |
+
'style="display:block; '
|
| 995 |
+
'margin:auto; '
|
| 996 |
+
'max-width:100%; '
|
| 997 |
+
'max-height:100%;">'
|
| 998 |
+
)
|
| 999 |
|
| 1000 |
+
chart_summary_prompt = (
|
| 1001 |
+
'The following Chart.js configuration '
|
| 1002 |
+
'was generated:\n\n'
|
| 1003 |
+
f'{json.dumps(chart_config, indent=2)}\n\n'
|
| 1004 |
+
'Briefly describe what this chart '
|
| 1005 |
+
'represents.'
|
| 1006 |
+
)
|
| 1007 |
|
| 1008 |
+
analysis_messages = [
|
| 1009 |
+
SystemMessage(
|
| 1010 |
+
content=(
|
| 1011 |
+
'You are analyzing chart '
|
| 1012 |
+
'configuration data, not '
|
| 1013 |
+
'an image. Be concise.'
|
| 1014 |
+
)
|
| 1015 |
+
),
|
| 1016 |
+
HumanMessage(
|
| 1017 |
+
content=chart_summary_prompt
|
| 1018 |
+
),
|
| 1019 |
+
]
|
| 1020 |
|
| 1021 |
+
try:
|
| 1022 |
+
response = chat_model.invoke(
|
| 1023 |
+
analysis_messages
|
| 1024 |
+
)
|
| 1025 |
+
analysis_text = response.content
|
| 1026 |
+
except Exception as e:
|
| 1027 |
+
print(
|
| 1028 |
+
'CHART ANALYSIS ERROR:',
|
| 1029 |
+
repr(e)
|
| 1030 |
+
)
|
| 1031 |
+
analysis_text = (
|
| 1032 |
+
'Chart generated successfully.'
|
| 1033 |
+
)
|
| 1034 |
|
| 1035 |
+
combined_content = (
|
| 1036 |
+
f'{image_html}'
|
| 1037 |
+
f'<br>{analysis_text}'
|
| 1038 |
+
)
|
|
|
|
| 1039 |
|
| 1040 |
+
chart_memory = (
|
| 1041 |
+
'Previous generated chart context.\n\n'
|
| 1042 |
+
f'Chart request:\n{chart_description}\n\n'
|
| 1043 |
+
f'Chart.js configuration:\n'
|
| 1044 |
+
f'{json.dumps(chart_config, indent=2)}\n\n'
|
| 1045 |
+
f'Chart analysis:\n{analysis_text}'
|
| 1046 |
+
)
|
| 1047 |
|
| 1048 |
+
messages3.append(
|
| 1049 |
+
HumanMessage(
|
| 1050 |
+
content=
|
| 1051 |
+
f'Generate a chart for: {chart_description}'
|
| 1052 |
+
)
|
| 1053 |
+
)
|
| 1054 |
+
messages3.append(
|
| 1055 |
+
AIMessage(
|
| 1056 |
+
content=chart_memory
|
| 1057 |
+
)
|
| 1058 |
+
)
|
| 1059 |
|
| 1060 |
+
except Exception as e:
|
| 1061 |
+
print(
|
| 1062 |
+
'CHART GENERATION ERROR:',
|
| 1063 |
+
repr(e)
|
| 1064 |
+
)
|
| 1065 |
+
chart_state = None
|
| 1066 |
+
combined_content = (
|
| 1067 |
+
'Chart generation failed.\n\n'
|
| 1068 |
+
f'Error: {str(e)}'
|
| 1069 |
+
)
|
| 1070 |
|
| 1071 |
+
else:
|
| 1072 |
+
if chart_state:
|
| 1073 |
+
chart_context = json.dumps(
|
| 1074 |
+
chart_state,
|
| 1075 |
+
indent=2
|
| 1076 |
+
)
|
| 1077 |
|
| 1078 |
+
followup_prompt = f"""
|
| 1079 |
+
You are continuing a conversation about a previously generated chart.
|
|
|
|
|
|
|
| 1080 |
|
| 1081 |
+
Previous Chart.js configuration:
|
| 1082 |
+
{chart_context}
|
|
|
|
|
|
|
| 1083 |
|
| 1084 |
+
Previous conversation:
|
| 1085 |
+
{chr(10).join(
|
| 1086 |
+
f'{m.__class__.__name__}: {m.content}'
|
| 1087 |
+
for m in messages3[-5:]
|
| 1088 |
+
)}
|
| 1089 |
|
| 1090 |
+
User question:
|
| 1091 |
+
{message}
|
| 1092 |
|
| 1093 |
+
Answer the user's question specifically about the previous chart.
|
| 1094 |
+
Use the previous chart context and conversation when relevant.
|
| 1095 |
+
Use only the labels, datasets, values, and options present in the chart configuration.
|
| 1096 |
+
Do not invent data.
|
| 1097 |
+
Do not claim to visually inspect the chart image.
|
| 1098 |
+
"""
|
| 1099 |
|
| 1100 |
+
try:
|
| 1101 |
+
response = chat_model.invoke(
|
| 1102 |
+
messages3 + [
|
| 1103 |
+
HumanMessage(
|
| 1104 |
+
content=followup_prompt
|
| 1105 |
)
|
| 1106 |
+
]
|
| 1107 |
)
|
| 1108 |
|
| 1109 |
+
combined_content = response.content
|
| 1110 |
|
| 1111 |
+
messages3.append(
|
| 1112 |
+
HumanMessage(
|
| 1113 |
+
content=message
|
| 1114 |
+
)
|
| 1115 |
+
)
|
| 1116 |
+
messages3.append(
|
| 1117 |
+
AIMessage(
|
| 1118 |
+
content=response.content
|
| 1119 |
+
)
|
| 1120 |
)
|
| 1121 |
+
except Exception as e:
|
| 1122 |
+
print(
|
| 1123 |
+
'CHART FOLLOW-UP ERROR:',
|
| 1124 |
+
repr(e)
|
| 1125 |
+
)
|
| 1126 |
+
combined_content = (
|
| 1127 |
+
'Unable to analyze the previous chart.\n\n'
|
| 1128 |
+
f'Error: {str(e)}'
|
| 1129 |
+
)
|
| 1130 |
+
else:
|
| 1131 |
+
combined_content = (
|
| 1132 |
+
'There is no previous chart in this '
|
| 1133 |
+
'conversation. Use #chart followed by '
|
| 1134 |
+
'the chart details first.'
|
| 1135 |
+
)
|
| 1136 |
|
| 1137 |
+
if len(messages3) > 6:
|
| 1138 |
+
system_message = messages3[0]
|
| 1139 |
+
messages3 = [
|
| 1140 |
+
system_message
|
| 1141 |
+
] + messages3[-5:]
|
| 1142 |
|
| 1143 |
+
history.append({
|
| 1144 |
+
'role': 'assistant',
|
| 1145 |
+
'content': ''
|
| 1146 |
+
})
|
| 1147 |
|
| 1148 |
+
for character in combined_content:
|
| 1149 |
+
history[-1]['content'] += character
|
| 1150 |
+
time.sleep(0.0025)
|
| 1151 |
+
yield history, messages3, chart_state
|
|
|
|
|
|
|
| 1152 |
|
|
|
|
| 1153 |
|
| 1154 |
+
def extract_url_from_text(text):
|
| 1155 |
+
if not text:
|
| 1156 |
+
return None
|
| 1157 |
|
| 1158 |
+
text = str(text).strip()
|
|
|
|
|
|
|
|
|
|
| 1159 |
|
| 1160 |
+
markdown_match = re.search(
|
| 1161 |
+
r'\]\((https?://[^)\s]+)\)',
|
| 1162 |
+
text
|
| 1163 |
)
|
| 1164 |
|
| 1165 |
+
if markdown_match:
|
| 1166 |
+
return markdown_match.group(1).rstrip(
|
| 1167 |
+
'.,!?;:)]}'
|
| 1168 |
+
)
|
| 1169 |
|
| 1170 |
+
url_match = re.search(
|
| 1171 |
+
r'https?://[^\s<>"\']+',
|
| 1172 |
+
text
|
|
|
|
| 1173 |
)
|
| 1174 |
|
| 1175 |
+
if url_match:
|
| 1176 |
+
return url_match.group(0).rstrip(
|
| 1177 |
+
'.,!?;:)]}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1178 |
)
|
| 1179 |
|
| 1180 |
+
return None
|
|
|
|
|
|
|
| 1181 |
|
| 1182 |
|
| 1183 |
def Link_Scratch(
|
| 1184 |
history,
|
| 1185 |
messages4
|
| 1186 |
):
|
| 1187 |
+
combined_message = str(
|
| 1188 |
+
history[-1]['content']
|
|
|
|
| 1189 |
)
|
| 1190 |
|
| 1191 |
if isinstance(
|
|
|
|
| 1198 |
combined_message
|
| 1199 |
)
|
| 1200 |
|
| 1201 |
+
user_message = combined_message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1202 |
|
| 1203 |
+
if link:
|
| 1204 |
+
user_message = re.sub(
|
| 1205 |
+
r'\[https?://[^\]]+\]\(https?://[^)]+\)',
|
| 1206 |
+
'',
|
| 1207 |
+
user_message
|
| 1208 |
+
)
|
| 1209 |
+
user_message = user_message.replace(
|
| 1210 |
+
link,
|
| 1211 |
+
''
|
| 1212 |
+
)
|
| 1213 |
+
user_message = user_message.strip()
|
| 1214 |
|
| 1215 |
print(
|
| 1216 |
+
'LINK INPUT:',
|
| 1217 |
repr(link)
|
| 1218 |
)
|
| 1219 |
|
| 1220 |
print(
|
| 1221 |
+
'USER QUERY:',
|
| 1222 |
repr(user_message)
|
| 1223 |
)
|
| 1224 |
|
| 1225 |
if not link:
|
|
|
|
| 1226 |
response_message = (
|
| 1227 |
+
'Please provide a valid URL starting '
|
| 1228 |
+
'with http:// or https://'
|
| 1229 |
)
|
|
|
|
| 1230 |
else:
|
| 1231 |
+
result = read_data(
|
| 1232 |
+
link
|
| 1233 |
+
)
|
| 1234 |
|
| 1235 |
print(
|
| 1236 |
+
'LINK READ RESULT TYPE:',
|
| 1237 |
type(result)
|
| 1238 |
)
|
| 1239 |
|
| 1240 |
print(
|
| 1241 |
+
'LINK READ RESULT PREVIEW:',
|
| 1242 |
+
str(result)[:1000]
|
| 1243 |
+
)
|
| 1244 |
+
|
| 1245 |
+
error_results = [
|
| 1246 |
+
'Unsupported type or format.',
|
| 1247 |
+
'Please provide a valid webpage link',
|
| 1248 |
+
(
|
| 1249 |
+
'Invalid YouTube URL. '
|
| 1250 |
+
'Please provide a valid YouTube link.'
|
| 1251 |
+
),
|
| 1252 |
+
(
|
| 1253 |
+
'Please choose a YouTube video with '
|
| 1254 |
+
'available English or Arabic transcripts.'
|
| 1255 |
+
),
|
| 1256 |
+
(
|
| 1257 |
+
'Invalid YouTube video URL. '
|
| 1258 |
+
'Please provide a valid YouTube video link.'
|
| 1259 |
+
),
|
| 1260 |
+
"Video doesn't exist."
|
| 1261 |
+
]
|
| 1262 |
|
| 1263 |
if (
|
| 1264 |
isinstance(result, str)
|
| 1265 |
and (
|
| 1266 |
+
result in error_results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1267 |
or result.startswith(
|
| 1268 |
+
'Unable to access webpage'
|
| 1269 |
)
|
| 1270 |
or result.startswith(
|
| 1271 |
+
'An error occurred while reading'
|
| 1272 |
)
|
| 1273 |
or result.startswith(
|
| 1274 |
+
'An error occurred:'
|
| 1275 |
)
|
| 1276 |
)
|
| 1277 |
):
|
|
|
|
| 1278 |
response_message = result
|
|
|
|
| 1279 |
else:
|
|
|
|
| 1280 |
content_data = normalize_text(
|
| 1281 |
result
|
| 1282 |
)
|
| 1283 |
|
| 1284 |
if not content_data:
|
|
|
|
| 1285 |
response_message = (
|
| 1286 |
+
'The provided link is empty or '
|
| 1287 |
+
'does not contain any meaningful words.'
|
| 1288 |
)
|
|
|
|
| 1289 |
else:
|
|
|
|
| 1290 |
augmented_prompt = f"""
|
| 1291 |
+
You are an AI designed to help understand and extract information from provided Link Content.
|
|
|
|
| 1292 |
|
| 1293 |
Based on the user's Query, answer using the provided Link Content.
|
| 1294 |
|
| 1295 |
Query:
|
| 1296 |
+
{user_message if user_message else 'Summarize this link.'}
|
| 1297 |
|
| 1298 |
Link Content:
|
| 1299 |
{content_data}
|
| 1300 |
|
| 1301 |
+
Use the Link Content as your primary source.
|
| 1302 |
+
Do not invent information.
|
| 1303 |
+
If the requested information is not present, explicitly say that it is not available.
|
| 1304 |
"""
|
| 1305 |
|
| 1306 |
message = HumanMessage(
|
|
|
|
| 1314 |
messages4 = messages4[-1:]
|
| 1315 |
|
| 1316 |
try:
|
| 1317 |
+
response = chat_model.invoke(
|
| 1318 |
+
messages4
|
|
|
|
|
|
|
|
|
|
| 1319 |
)
|
|
|
|
| 1320 |
except Exception as e:
|
| 1321 |
+
error_message = str(e)
|
| 1322 |
print(
|
| 1323 |
+
'LINK LLM ERROR:',
|
| 1324 |
repr(e)
|
| 1325 |
)
|
| 1326 |
|
| 1327 |
+
start_index = error_message.find(
|
| 1328 |
+
'Input validation error:'
|
| 1329 |
+
)
|
| 1330 |
+
end_index = error_message.find(
|
| 1331 |
+
'and 4096 `max_new_tokens`'
|
| 1332 |
+
)
|
| 1333 |
+
|
| 1334 |
+
if (
|
| 1335 |
+
start_index != -1
|
| 1336 |
+
and end_index != -1
|
| 1337 |
+
):
|
| 1338 |
+
raise gr.Error(
|
| 1339 |
+
error_message[
|
| 1340 |
+
start_index:end_index
|
| 1341 |
+
].strip()
|
| 1342 |
+
) from e
|
| 1343 |
+
else:
|
| 1344 |
+
raise gr.Error(
|
| 1345 |
+
'Error occurred during response'
|
| 1346 |
+
) from e
|
| 1347 |
|
| 1348 |
messages4.append(
|
| 1349 |
AIMessage(
|
|
|
|
| 1351 |
)
|
| 1352 |
)
|
| 1353 |
|
| 1354 |
+
response_message = response.content
|
|
|
|
|
|
|
| 1355 |
|
| 1356 |
history.append({
|
| 1357 |
+
'role': 'assistant',
|
| 1358 |
+
'content': ''
|
| 1359 |
})
|
| 1360 |
|
| 1361 |
for character in response_message:
|
| 1362 |
+
history[-1]['content'] += character
|
|
|
|
|
|
|
| 1363 |
time.sleep(0.0025)
|
|
|
|
| 1364 |
yield history, messages4
|
| 1365 |
|
| 1366 |
|
|
|
|
| 1369 |
every=8
|
| 1370 |
):
|
| 1371 |
|
| 1372 |
+
return '\n'.join(
|
| 1373 |
text[i:i + every]
|
| 1374 |
for i in range(
|
| 1375 |
0,
|
|
|
|
| 1382 |
def display_file_name(file):
|
| 1383 |
|
| 1384 |
supported_extensions = [
|
| 1385 |
+
'.csv',
|
| 1386 |
+
'.txt',
|
| 1387 |
+
'.pdf',
|
| 1388 |
+
'.docx',
|
| 1389 |
+
'.pptx',
|
| 1390 |
+
'.xlsx',
|
| 1391 |
+
'.json',
|
| 1392 |
+
'.html',
|
| 1393 |
+
'.xml'
|
| 1394 |
]
|
| 1395 |
|
| 1396 |
file_extension = os.path.splitext(
|
|
|
|
| 1413 |
)
|
| 1414 |
|
| 1415 |
icon_url = (
|
| 1416 |
+
'https://img.icons8.com/'
|
| 1417 |
+
'ios-filled/50/0000FF/file.png'
|
| 1418 |
)
|
| 1419 |
|
| 1420 |
return (
|
|
|
|
| 1431 |
"</b></div>"
|
| 1432 |
)
|
| 1433 |
|
| 1434 |
+
else:
|
| 1435 |
+
|
| 1436 |
+
raise gr.Error(
|
| 1437 |
+
'( Supported File Types Only : '
|
| 1438 |
+
'PDF , CSV , TXT , DOCX , PPTX , '
|
| 1439 |
+
'XLSX , JSON , HTML , XML )'
|
| 1440 |
+
)
|
| 1441 |
+
|
| 1442 |
|
| 1443 |
|
| 1444 |
def File_Interact(
|
|
|
|
| 1448 |
):
|
| 1449 |
|
| 1450 |
combined_message = str(
|
| 1451 |
+
history[-1]['content']
|
| 1452 |
)
|
| 1453 |
|
| 1454 |
if isinstance(
|
|
|
|
| 1457 |
):
|
| 1458 |
messages5 = messages5[:-2]
|
| 1459 |
|
| 1460 |
+
link = ''
|
| 1461 |
+
user_message = ''
|
| 1462 |
|
| 1463 |
+
if '\n' in combined_message:
|
| 1464 |
|
| 1465 |
+
link, user_message = (
|
| 1466 |
combined_message.split(
|
| 1467 |
+
'\n',
|
| 1468 |
1
|
| 1469 |
)
|
| 1470 |
)
|
| 1471 |
|
| 1472 |
+
user_message = user_message.strip()
|
|
|
|
|
|
|
| 1473 |
|
| 1474 |
+
result = read_data(
|
| 1475 |
+
filepath
|
| 1476 |
+
)
|
| 1477 |
|
| 1478 |
+
if result == 'Unsupported type or format.':
|
| 1479 |
|
| 1480 |
response_message = result
|
| 1481 |
|
|
|
|
| 1488 |
if not content_data:
|
| 1489 |
|
| 1490 |
response_message = (
|
| 1491 |
+
'The file is empty or does not '
|
| 1492 |
+
'contain any meaningful words.'
|
| 1493 |
)
|
| 1494 |
|
| 1495 |
else:
|
|
|
|
| 1532 |
|
| 1533 |
except Exception as e:
|
| 1534 |
|
| 1535 |
+
error_message = str(e)
|
| 1536 |
+
|
| 1537 |
print(
|
| 1538 |
+
'FILE LLM ERROR:',
|
| 1539 |
repr(e)
|
| 1540 |
)
|
| 1541 |
|
| 1542 |
+
start_index = (
|
| 1543 |
+
error_message.find(
|
| 1544 |
+
'Input validation error:'
|
| 1545 |
+
)
|
| 1546 |
+
)
|
| 1547 |
+
|
| 1548 |
+
end_index = (
|
| 1549 |
+
error_message.find(
|
| 1550 |
+
'and 4096 `max_new_tokens`'
|
| 1551 |
+
)
|
| 1552 |
+
)
|
| 1553 |
+
|
| 1554 |
+
if (
|
| 1555 |
+
start_index != -1
|
| 1556 |
+
and end_index != -1
|
| 1557 |
+
):
|
| 1558 |
+
|
| 1559 |
+
raise gr.Error(
|
| 1560 |
+
error_message[
|
| 1561 |
+
start_index:end_index
|
| 1562 |
+
].strip()
|
| 1563 |
+
) from e
|
| 1564 |
+
|
| 1565 |
+
else:
|
| 1566 |
+
|
| 1567 |
+
raise gr.Error(
|
| 1568 |
+
'Error occurred during response'
|
| 1569 |
+
) from e
|
| 1570 |
|
| 1571 |
messages5.append(
|
| 1572 |
AIMessage(
|
|
|
|
| 1579 |
)
|
| 1580 |
|
| 1581 |
history.append({
|
| 1582 |
+
'role': 'assistant',
|
| 1583 |
+
'content': ''
|
| 1584 |
})
|
| 1585 |
|
| 1586 |
for character in response_message:
|
| 1587 |
|
| 1588 |
+
history[-1]['content'] += character
|
| 1589 |
|
| 1590 |
time.sleep(0.0025)
|
| 1591 |
|
| 1592 |
yield history, messages5
|
| 1593 |
|
| 1594 |
|
| 1595 |
+
|
| 1596 |
def Explore_WebSite(
|
| 1597 |
history,
|
| 1598 |
messages6
|
| 1599 |
):
|
| 1600 |
|
| 1601 |
+
message = history[-1]['content']
|
| 1602 |
|
| 1603 |
if isinstance(
|
| 1604 |
messages6[-1],
|
|
|
|
| 1607 |
messages6 = messages6[:-2]
|
| 1608 |
|
| 1609 |
links = [
|
| 1610 |
+
'https://huggingface.co/mou3az'
|
| 1611 |
]
|
| 1612 |
|
| 1613 |
+
result = '\n'.join(
|
| 1614 |
[
|
| 1615 |
read_data(link)
|
| 1616 |
for link in links
|
|
|
|
| 1656 |
|
| 1657 |
except Exception as e:
|
| 1658 |
|
| 1659 |
+
error_message = str(e)
|
| 1660 |
+
|
| 1661 |
print(
|
| 1662 |
+
'WEBSITE ERROR:',
|
| 1663 |
repr(e)
|
| 1664 |
)
|
| 1665 |
|
| 1666 |
+
start_index = error_message.find(
|
| 1667 |
+
'Input validation error:'
|
| 1668 |
+
)
|
| 1669 |
+
|
| 1670 |
+
end_index = error_message.find(
|
| 1671 |
+
'and 4096 `max_new_tokens`'
|
| 1672 |
+
)
|
| 1673 |
+
|
| 1674 |
+
if (
|
| 1675 |
+
start_index != -1
|
| 1676 |
+
and end_index != -1
|
| 1677 |
+
):
|
| 1678 |
+
|
| 1679 |
+
raise gr.Error(
|
| 1680 |
+
error_message[
|
| 1681 |
+
start_index:end_index
|
| 1682 |
+
].strip()
|
| 1683 |
+
) from e
|
| 1684 |
+
|
| 1685 |
+
else:
|
| 1686 |
+
|
| 1687 |
+
raise gr.Error(
|
| 1688 |
+
'Error occurred during response'
|
| 1689 |
+
) from e
|
| 1690 |
|
| 1691 |
messages6.append(
|
| 1692 |
AIMessage(
|
|
|
|
| 1695 |
)
|
| 1696 |
|
| 1697 |
history.append({
|
| 1698 |
+
'role': 'assistant',
|
| 1699 |
+
'content': ''
|
| 1700 |
})
|
| 1701 |
|
| 1702 |
for character in response.content:
|
| 1703 |
|
| 1704 |
+
history[-1]['content'] += character
|
| 1705 |
|
| 1706 |
time.sleep(0.0025)
|
| 1707 |
|
| 1708 |
yield history, messages6
|
| 1709 |
|
| 1710 |
|
| 1711 |
+
|
| 1712 |
with gr.Blocks() as demo:
|
| 1713 |
|
| 1714 |
with gr.Row():
|
|
|
|
| 1735 |
"""
|
| 1736 |
)
|
| 1737 |
|
| 1738 |
+
|
| 1739 |
+
with gr.Tab('Chat-Message'):
|
| 1740 |
|
| 1741 |
messages1 = gr.State(
|
| 1742 |
messages1_state
|
| 1743 |
)
|
| 1744 |
|
| 1745 |
chatbot = gr.Chatbot(
|
| 1746 |
+
elem_id='chatbot',
|
| 1747 |
height=500,
|
| 1748 |
placeholder=(
|
| 1749 |
"<span style='font-weight:bold;"
|
|
|
|
| 1759 |
|
| 1760 |
msg = gr.Textbox(
|
| 1761 |
show_label=False,
|
| 1762 |
+
placeholder='Type a message...',
|
| 1763 |
scale=10,
|
| 1764 |
container=False
|
| 1765 |
)
|
| 1766 |
|
| 1767 |
submit = gr.Button(
|
| 1768 |
+
'➡️Send',
|
| 1769 |
scale=1
|
| 1770 |
)
|
| 1771 |
|
|
|
|
| 1803 |
None
|
| 1804 |
)
|
| 1805 |
|
| 1806 |
+
|
| 1807 |
+
with gr.Tab('Internet-Search'):
|
| 1808 |
|
| 1809 |
messages2 = gr.State(
|
| 1810 |
messages1_state
|
| 1811 |
)
|
| 1812 |
|
| 1813 |
chatbot = gr.Chatbot(
|
| 1814 |
+
elem_id='chatbot',
|
| 1815 |
height=500,
|
| 1816 |
placeholder=(
|
| 1817 |
"<span style='font-weight:bold;"
|
|
|
|
| 1828 |
|
| 1829 |
msg = gr.Textbox(
|
| 1830 |
show_label=False,
|
| 1831 |
+
placeholder='Type a message...',
|
| 1832 |
scale=10,
|
| 1833 |
container=False
|
| 1834 |
)
|
| 1835 |
|
| 1836 |
submit = gr.Button(
|
| 1837 |
+
'➡️Send',
|
| 1838 |
scale=1
|
| 1839 |
)
|
| 1840 |
|
|
|
|
| 1872 |
None
|
| 1873 |
)
|
| 1874 |
|
|
|
|
| 1875 |
|
| 1876 |
+
with gr.Tab('Chart-Generator'):
|
| 1877 |
messages3 = gr.State(
|
| 1878 |
messages1_state
|
| 1879 |
)
|
| 1880 |
+
chart_state = gr.State(None)
|
| 1881 |
|
| 1882 |
chatbot = gr.Chatbot(
|
| 1883 |
+
elem_id='chatbot',
|
| 1884 |
height=500,
|
| 1885 |
placeholder=(
|
| 1886 |
"<span style='font-weight:bold;"
|
|
|
|
| 1894 |
)
|
| 1895 |
|
| 1896 |
with gr.Row():
|
|
|
|
| 1897 |
msg = gr.Textbox(
|
| 1898 |
show_label=False,
|
| 1899 |
placeholder=(
|
| 1900 |
+
'To generate a chart: '
|
| 1901 |
+
'type #chart [your chart description]. '
|
| 1902 |
+
'To discuss the chart: '
|
| 1903 |
+
'type your message directly...'
|
| 1904 |
),
|
| 1905 |
scale=10,
|
| 1906 |
container=False
|
| 1907 |
)
|
| 1908 |
|
| 1909 |
submit = gr.Button(
|
| 1910 |
+
'➡️Send',
|
| 1911 |
scale=1
|
| 1912 |
)
|
| 1913 |
|
| 1914 |
clear = gr.ClearButton([
|
| 1915 |
msg,
|
| 1916 |
chatbot,
|
| 1917 |
+
messages3,
|
| 1918 |
+
chart_state
|
| 1919 |
])
|
| 1920 |
|
| 1921 |
msg.submit(
|
|
|
|
| 1925 |
queue=True
|
| 1926 |
).then(
|
| 1927 |
Chart_Generator,
|
| 1928 |
+
[chatbot, messages3, chart_state],
|
| 1929 |
+
[chatbot, messages3, chart_state]
|
| 1930 |
)
|
| 1931 |
|
| 1932 |
submit.click(
|
|
|
|
| 1936 |
queue=True
|
| 1937 |
).then(
|
| 1938 |
Chart_Generator,
|
| 1939 |
+
[chatbot, messages3, chart_state],
|
| 1940 |
+
[chatbot, messages3, chart_state]
|
| 1941 |
)
|
| 1942 |
|
| 1943 |
chatbot.like(
|
|
|
|
| 1946 |
None
|
| 1947 |
)
|
| 1948 |
|
| 1949 |
+
|
| 1950 |
+
with gr.Tab('Link-Scratch'):
|
| 1951 |
|
| 1952 |
messages4 = gr.State(
|
| 1953 |
messages1_state
|
| 1954 |
)
|
| 1955 |
|
| 1956 |
chatbot = gr.Chatbot(
|
| 1957 |
+
elem_id='chatbot',
|
| 1958 |
height=500,
|
| 1959 |
placeholder=(
|
| 1960 |
"<span style='font-weight:bold;"
|
|
|
|
| 1971 |
|
| 1972 |
msg1 = gr.Textbox(
|
| 1973 |
show_label=False,
|
| 1974 |
+
placeholder='Paste your link...',
|
| 1975 |
scale=4,
|
| 1976 |
container=False
|
| 1977 |
)
|
| 1978 |
|
| 1979 |
msg2 = gr.Textbox(
|
| 1980 |
show_label=False,
|
| 1981 |
+
placeholder='Type a message...',
|
| 1982 |
scale=7,
|
| 1983 |
container=False
|
| 1984 |
)
|
| 1985 |
|
| 1986 |
submit = gr.Button(
|
| 1987 |
+
'➡️Send',
|
| 1988 |
scale=1
|
| 1989 |
)
|
| 1990 |
|
|
|
|
| 2034 |
None
|
| 2035 |
)
|
| 2036 |
|
| 2037 |
+
|
| 2038 |
+
with gr.Tab('File-Interact'):
|
| 2039 |
|
| 2040 |
messages5 = gr.State(
|
| 2041 |
messages1_state
|
| 2042 |
)
|
| 2043 |
|
| 2044 |
chatbot = gr.Chatbot(
|
| 2045 |
+
elem_id='chatbot',
|
| 2046 |
height=500,
|
| 2047 |
placeholder=(
|
| 2048 |
"<span style='font-weight:bold;"
|
|
|
|
| 2062 |
with gr.Row():
|
| 2063 |
|
| 2064 |
filepath = gr.UploadButton(
|
| 2065 |
+
'Upload a file',
|
| 2066 |
+
file_count='single',
|
| 2067 |
scale=1
|
| 2068 |
)
|
| 2069 |
|
| 2070 |
msg = gr.Textbox(
|
| 2071 |
show_label=False,
|
| 2072 |
placeholder=(
|
| 2073 |
+
'Wait until the file has been '
|
| 2074 |
+
'uploaded, then type a message....'
|
| 2075 |
),
|
| 2076 |
scale=7,
|
| 2077 |
container=False
|
| 2078 |
)
|
| 2079 |
|
| 2080 |
submit = gr.Button(
|
| 2081 |
+
'➡️Send',
|
| 2082 |
scale=1
|
| 2083 |
)
|
| 2084 |
|
|
|
|
| 2134 |
None
|
| 2135 |
)
|
| 2136 |
|
| 2137 |
+
|
| 2138 |
+
with gr.Tab('Explore-WebSite'):
|
| 2139 |
|
| 2140 |
messages6 = gr.State(
|
| 2141 |
messages1_state
|
| 2142 |
)
|
| 2143 |
|
| 2144 |
chatbot = gr.Chatbot(
|
| 2145 |
+
elem_id='chatbot',
|
| 2146 |
height=500,
|
| 2147 |
placeholder=(
|
| 2148 |
"<span style='font-weight:bold;"
|
|
|
|
| 2158 |
|
| 2159 |
msg = gr.Textbox(
|
| 2160 |
show_label=False,
|
| 2161 |
+
placeholder='Type a message...',
|
| 2162 |
scale=10,
|
| 2163 |
container=False
|
| 2164 |
)
|
| 2165 |
|
| 2166 |
submit = gr.Button(
|
| 2167 |
+
'➡️Send',
|
| 2168 |
scale=1
|
| 2169 |
)
|
| 2170 |
|
|
|
|
| 2209 |
)
|
| 2210 |
|
| 2211 |
demo.launch(
|
| 2212 |
+
max_file_size='5mb',
|
| 2213 |
max_threads=50,
|
| 2214 |
theme=gr.themes.Soft()
|
| 2215 |
)
|