import os
import csv
import json
import docx
import pptx
import re
import nltk
import time
import PyPDF2
import tempfile
import openpyxl
import requests
import gradio as gr
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
from nltk.tokenize import word_tokenize
from langchain_community.vectorstores import FAISS
from youtube_transcript_api import YouTubeTranscriptApi
from langchain_community.llms import HuggingFaceEndpoint
from langchain.schema import SystemMessage, HumanMessage, AIMessage
from langchain_community.chat_models.huggingface import ChatHuggingFace
from langchain_community.embeddings import SentenceTransformerEmbeddings
from youtube_transcript_api._errors import NoTranscriptFound, TranscriptsDisabled, VideoUnavailable
nltk.download('punkt')
nltk.download('omw-1.4')
nltk.download('wordnet')
def read_csv(file_path):
with open(file_path, 'r', encoding='utf-8', errors='ignore', newline='') as csvfile:
csv_reader = csv.reader(csvfile)
csv_data = [row for row in csv_reader]
return ' '.join([' '.join(row) for row in csv_data])
def read_text(file_path):
with open(file_path, 'r', encoding='utf-8', errors='ignore', newline='') as f:
return f.read()
def read_pdf(file_path):
text_data = []
with open(file_path, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
for page in pdf_reader.pages:
text_data.append(page.extract_text())
return '\n'.join(text_data)
def read_docx(file_path):
doc = docx.Document(file_path)
return '\n'.join([paragraph.text for paragraph in doc.paragraphs])
def read_pptx(file_path):
ppt = pptx.Presentation(file_path)
text_data = ''
for slide in ppt.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
text_data += shape.text + '\n'
return text_data
def read_xlsx(file_path):
workbook = openpyxl.load_workbook(file_path)
sheet = workbook.active
text_data = ''
for row in sheet.iter_rows(values_only=True):
text_data += ' '.join([str(cell) for cell in row if cell is not None]) + '\n'
return text_data
def read_json(file_path):
with open(file_path, 'r') as f:
json_data = json.load(f)
return json.dumps(json_data)
def read_html(file_path):
with open(file_path, 'r') as f:
html_content = f.read()
soup = BeautifulSoup(html_content, 'html.parser')
return soup
def read_xml(file_path):
tree = ET.parse(file_path)
root = tree.getroot()
return ET.tostring(root, encoding='unicode')
def process_youtube_video(url, languages=['en', 'ar']):
if 'youtube.com/watch' in url or 'youtu.be/' in url:
try:
if "v=" in url:
video_id = url.split("v=")[1].split("&")[0]
elif "youtu.be/" in url:
video_id = url.split("youtu.be/")[1].split("?")[0]
else:
return "Invalid YouTube video URL. Please provide a valid YouTube video link."
response = requests.get(f"http://img.youtube.com/vi/{video_id}/mqdefault.jpg")
if response.status_code != 200:
return "Video doesn't exist."
transcript_data = []
for lang in languages:
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=[lang])
transcript_data.append(' '.join([entry['text'] for entry in transcript]))
except (NoTranscriptFound, TranscriptsDisabled, VideoUnavailable):
continue
return ' '.join(transcript_data) if transcript_data else "Please choose a YouTube video with available English or Arabic transcripts."
except Exception as e:
return f"An error occurred: {e}"
else:
return "Invalid YouTube URL. Please provide a valid YouTube link."
def read_web_page(url):
result = requests.get(url)
if result.status_code == 200:
src = result.text
soup = BeautifulSoup(src, 'html.parser')
text_data = ''
div_elements = soup.find_all('div')
for div in div_elements:
text_data += div.get_text() + '\n'
return text_data
else:
return "Please provide a valid webpage link"
def read_data(file_path_or_url, languages=['en', 'ar']):
if file_path_or_url.endswith('.csv'):
return read_csv(file_path_or_url)
elif file_path_or_url.endswith('.txt'):
return read_text(file_path_or_url)
elif file_path_or_url.endswith('.pdf'):
return read_pdf(file_path_or_url)
elif file_path_or_url.endswith('.docx'):
return read_docx(file_path_or_url)
elif file_path_or_url.endswith('.pptx'):
return read_pptx(file_path_or_url)
elif file_path_or_url.endswith('.xlsx'):
return read_xlsx(file_path_or_url)
elif file_path_or_url.endswith('.json'):
return read_json(file_path_or_url)
elif file_path_or_url.endswith('.html'):
return read_html(file_path_or_url)
elif file_path_or_url.endswith('.xml'):
return read_xml(file_path_or_url)
elif 'youtube.com/watch' in file_path_or_url or 'youtu.be/' in file_path_or_url:
return process_youtube_video(file_path_or_url, languages)
elif file_path_or_url.startswith('http'):
return read_web_page(file_path_or_url)
else:
return "Unsupported type or format."
def normalize_text(text):
text = re.sub("\*?", "", text)
text = text.lower()
text = text.strip()
punctuation = '''!()[]{};:'"\<>/?$%^&*_`~='''
for punc in punctuation:
text = text.replace(punc, "")
text = re.sub(r'[A-Za-z0-9]*@[A-Za-z]*\.?[A-Za-z0-9]*', "", text)
words = word_tokenize(text)
return ' '.join(words)
llm = HuggingFaceEndpoint(
repo_id="HuggingFaceH4/starchat2-15b-v0.1",
task="text-generation",
max_new_tokens=4096,
temperature=0.6,
top_p=0.9,
top_k=40,
repetition_penalty=1.2,
do_sample=True,
)
chat_model = ChatHuggingFace(llm=llm)
model_name = "sentence-transformers/all-mpnet-base-v2"
embedding_llm = SentenceTransformerEmbeddings(model_name=model_name)
db = FAISS.load_local("faiss_index", embedding_llm, allow_dangerous_deserialization=True)
def print_like_dislike(x: gr.LikeData):
print(x.index, x.value, x.liked)
def user(user_message, history):
if not len(user_message):
raise gr.Error("Chat messages cannot be empty")
return "", history + [[user_message, None]]
def user2(user_message, history, link):
if not len(user_message) or not len(link):
raise gr.Error("Chat messages or links cannot be empty")
combined_message = f"{link}\n{user_message}"
return "", history + [[combined_message, None]], link
def user3(user_message, history, file_path):
if not len(user_message) or not file_path:
raise gr.Error("Chat messages or flies cannot be empty")
combined_message = f"{file_path}\n{user_message}"
return "", history + [[combined_message, None]], file_path
messages1_state = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="Hi AI, how are you today?"),
AIMessage(content="I'm great thank you. How can I help you?")]
def Chat_Message(history, messages1):
message=HumanMessage(content=history[-1][0])
messages1.append(message)
if len(messages1) >= 8:
messages1 = messages1[-8:]
try:
response = chat_model.invoke(messages1)
except Exception as e:
error_message = str(e)
index = error_message.find("Input validation error:")
end_index = error_message.find("\nMake sure 'text-generation' task is supported by the model.")
if start_index != -1 and end_index != -1:
raise gr.Error(error_message[start_index:end_index].strip()) from e
else:
raise gr.Error("Error occurred during response") from e
messages1.append(AIMessage(content=response.content))
history[-1][1] = ""
for character in response.content:
history[-1][1] += character
time.sleep(0.0025)
yield history, messages1
def Internet_Search(history, messages2):
message=history[-1][0]
similar_docs = db.similarity_search(message, k=3)
if similar_docs:
source_knowledge = "\n".join([x.page_content for x in similar_docs])
else:
source_knowledge = ""
augmented_prompt = f"""
You are an AI designed to help understand and extract information from provided Search Content. Based on the user's Query, you may need to summarize the text, answer specific questions, or provide guidance.
Query: {message}
Search Content:
{source_knowledge}
#If the query is not related to specific Search Content, engage in general conversation or provide relevant information from other sources.
"""
msg=HumanMessage(content=augmented_prompt)
messages2.append(msg)
if len(messages2) >= 4:
messages2 = messages2[-4:]
try:
response = chat_model.invoke(messages2)
except Exception as e:
error_message = str(e)
index = error_message.find("Input validation error:")
end_index = error_message.find("\nMake sure 'text-generation' task is supported by the model.")
if start_index != -1 and end_index != -1:
raise gr.Error(error_message[start_index:end_index].strip()) from e
else:
raise gr.Error("Error occurred during response") from e
messages2.append(AIMessage(content=response.content))
history[-1][1] = ""
for character in response.content:
history[-1][1] += character
time.sleep(0.0025)
yield history, messages2
def Chart_Generator(history, messages3):
message = history[-1][0]
if '#chart' in message:
message = message.split('#chart', 1)[1].strip()
chart_url = f"https://quickchart.io/natural/{message}"
response = requests.get(chart_url)
if response.status_code == 200:
image_html = f''
message_with_description = f"Describe and analyse the content of this chart: {chart_url}"
prompt = HumanMessage(content=message_with_description)
messages3.append(prompt)
if len(messages3) >= 6:
messages3 = messages3[-6:]
try:
response = chat_model.invoke(messages3)
except Exception as e:
error_message = str(e)
index = error_message.find("Input validation error:")
end_index = error_message.find("\nMake sure 'text-generation' task is supported by the model.")
if start_index != -1 and end_index != -1:
raise gr.Error(error_message[start_index:end_index].strip()) from e
else:
raise gr.Error("Error occurred during response") from e
messages3.append(AIMessage(content=response.content))
combined_content = f'{image_html}
{response.content}'
else:
response_text = "Can't generate this image. Please provide valid chart details."
combined_content = response_text
else:
prompt = HumanMessage(content=message)
messages3.append(prompt)
if len(messages3) >= 6:
messages3 = messages3[-6:]
try:
response = chat_model.invoke(messages3)
except Exception as e:
error_message = str(e)
index = error_message.find("Input validation error:")
end_index = error_message.find("\nMake sure 'text-generation' task is supported by the model.")
if start_index != -1 and end_index != -1:
raise gr.Error(error_message[start_index:end_index].strip()) from e
else:
raise gr.Error("Error occurred during response") from e
messages3.append(AIMessage(content=response.content))
combined_content=response.content
history[-1][1] = ""
for character in combined_content:
history[-1][1] += character
time.sleep(0.0025)
yield history, messages3
def Link_Scratch(history, messages4):
combined_message = history[-1][0]
link = ""
user_message = ""
if "\n" in combined_message:
link, user_message = combined_message.split("\n", 1)
link = link.strip()
user_message = user_message.strip()
result = read_data(link)
if result in ["Unsupported type or format.", "Please provide a valid webpage link",
"Invalid YouTube URL. Please provide a valid YouTube link.",
"Please choose a YouTube video with available English or Arabic transcripts.",
"Invalid YouTube video URL. Please provide a valid YouTube video link."]:
response_message = result
else:
content_data = normalize_text(result)
if not content_data:
response_message = "The provided link is empty or does not contain any meaningful words."
else:
augmented_prompt = f"""
You are an AI designed to help understand and extract information from provided Link Content. Based on the user's Query, you may need to summarize the text, answer specific questions, or provide guidance.
Query: {user_message}
Link Content:
{content_data}
#If the query is not related to specific Link Content, engage in general conversation or provide relevant information from other sources.
"""
message = HumanMessage(content=augmented_prompt)
messages4.append(message)
if len(messages4) >= 2:
messages4 = messages4[-2:]
try:
response = chat_model.invoke(messages4)
except Exception as e:
error_message = str(e)
index = error_message.find("Input validation error:")
end_index = error_message.find("\nMake sure 'text-generation' task is supported by the model.")
if start_index != -1 and end_index != -1:
raise gr.Error(error_message[start_index:end_index].strip()) from e
else:
raise gr.Error("Error occurred during response") from e
messages4.append(AIMessage(content=response.content))
response_message = response.content
history[-1][1] = ""
for character in response_message:
history[-1][1] += character
time.sleep(0.0025)
yield history, messages4
def insert_line_breaks(text, every=8):
return '\n'.join(text[i:i+every] for i in range(0, len(text), every))
def display_file_name(file):
supported_extensions = ['.csv', '.txt', '.pdf', '.docx', '.pptx', '.xlsx', '.json', '.html', '.xml']
file_extension = os.path.splitext(file.name)[1]
if file_extension.lower() in supported_extensions:
file_name = os.path.basename(file.name)
file_name_with_breaks = insert_line_breaks(file_name)
icon_url = "https://img.icons8.com/ios-filled/50/0000FF/file.png"
return f"