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