HarFunMola2 / app.py
WasifAKhan's picture
Update app.py
2171cbc verified
import streamlit as st
# Define appliance options
appliance_options = ["Refrigerator", "Dish Washer", "Coffee Maker", "Air Conditioner",
"Washing Machine", "Laptop", "Oven", "TV", "Soundbar", "Vacuum Cleaner",
"Iron", "Mixer", "Food Processor", "Tooth Brush", "Electric Toaster",
"Citrus Press", "Air Dryer", "Juicer", "Heater", "Ceiling Fan"]
# Define brand dictionaries for each appliance (key: appliance name, value: list of brands)
brand_options = {
"Refrigerator": ["Haier", "Samsung", "Dawlance", "Hitachi", "PEL"],
"Dish Washer": ["Samsung", "Bosch", "Baumeatic"],
"Coffee Maker": ["Black & Decker"],
"Air Conditioner": ["Samsung", "Haier", "GREE"],
"Washing Machine": ["Samsung", "Haier"],
"Laptop": ["Haier"],
"Oven": ["La Vita", "Samsung", "Kenwood", "Haier", "Phillips"],
"TV": ["Samsung", "Haier", "Hisense", "Phillips", "Google"],
"Soundbar": ["Samsung", "Phillips"],
"Vacuum Cleaner": ["Samsung"],
"Iron": ["Phillips"],
"Mixer": ["Phillips"],
"Food Processor": ["Phillips"],
"Tooth Brush": ["Phillips"],
"Electric Toaster": ["Phillips"],
"Citrus Press": ["Phillips"],
"Air Dryer": ["Phillips"],
"Juicer": ["Phillips"],
"Heater": ["Phillips"],
"Ceiling Fan": ["GFC", "Hunter"]
}
# Get user selections for appliance and brand
selected_appliance = st.selectbox("Select Appliance", appliance_options)
selected_brand = None
if selected_appliance:
selected_brand = st.selectbox(f"Select Brand for {selected_appliance}", brand_options[selected_appliance])
# Display user selection
if selected_appliance and selected_brand:
st.write(f"You selected {selected_brand} {selected_appliance}")
import os
import requests
from io import BytesIO
from groq import Groq
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter
from PyPDF2 import PdfReader
from tempfile import NamedTemporaryFile
# Initialize Groq client
client = Groq(api_key="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")
#client = Groq("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")
# Predefined list of Google Drive links
drive_links = [
"https://drive.google.com/file/d/15ep4h17VFarb4coWAgqcEUDUTN7Ek6dr/view?usp=sharing",
"https://drive.google.com/file/d/1ocxUsw4rVc9MNV_9wARvXg9D9SLe2vVi/view?usp=sharing",
"https://drive.google.com/file/d/1yrOTIlOYywjbfeMWcBG1kBK8-LLYGl3t/view?usp=sharing",
"https://drive.google.com/file/d/1otdel-JxaZBvitQQMiXtniTNAsmICrMT/view?usp=sharing",
"https://drive.google.com/file/d/16--HjKNn5Oz1cqX3b-8qvwnD0_yLvdy2/view?usp=sharing",
"https://drive.google.com/file/d/1lnBD-0H1mJJ8N-lNdrhomuzgm8nRFVxA/view?usp=sharing",
"https://drive.google.com/file/d/1ni7IxoI7j2wzCcWjQEEqrE1JO0HsRGrl/view?usp=sharing",
"https://drive.google.com/file/d/1s4QkcbTRsgT8Ju4O23gVpuh4Hy-dsHnb/view?usp=sharing",
"https://drive.google.com/file/d/1eRoAWgYqNsmYHVa90J2Tm0ejIMa_8qvZ/view?usp=sharing",
"https://drive.google.com/file/d/1r8SKClbsyAzYqOTfTEZXcQwf5YUEHPMG/view?usp=sharing",
"https://drive.google.com/file/d/1L-Lc_Yv14Tlu3waA3ufDwd4kzTkZfLHC/view?usp=sharing",
"https://drive.google.com/file/d/1bqz_eQkeo8Qc0OPaghIdQFMbDT9eFS77/view?usp=sharing",
"https://drive.google.com/file/d/1VSSNBc76yh_OtyBzuC_S43nrFMIc8Fw1/view?usp=sharing",
"https://drive.google.com/file/d/1NxQevabv6PPXxeXv5DZyeP8Q9GlHwD2r/view?usp=sharing",
"https://drive.google.com/file/d/1S_XKeDtPI_UBupnIURZTPiddmLbjqnRZ/view?usp=sharing",
"https://drive.google.com/file/d/1Bppf9VgUShBJWnYEw-4ZwDu5-A51nR9R/view?usp=sharing",
"https://drive.google.com/file/d/1s7z4TpFmMpvx1-PpYsaukACOSTrkbcOf/view?usp=sharing",
"https://drive.google.com/file/d/12Rrh0ixkQPvAAhExdnbMeol7c9aAzyTd/view?usp=sharing",
"https://drive.google.com/file/d/1BGq_aacZjmkEfAa2Oyzfw32piEQfBAmZ/view?usp=sharing",
"https://drive.google.com/file/d/1nxFqyFOa3C0TK8BLOYp-V6e_xjYGn0qV/view?usp=sharing",
"https://drive.google.com/file/d/1xsMYVFufaM2gzMPKADNCrDbGyq0rhSV2/view?usp=sharing"
]
# Function to download PDF from Google Drive
def download_pdf_from_drive(drive_link):
file_id = drive_link.split('/d/')[1].split('/')[0]
download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
response = requests.get(download_url)
if response.status_code == 200:
return BytesIO(response.content)
else:
raise Exception("Failed to download the PDF file from Google Drive.")
# Function to extract text from a PDF
def extract_text_from_pdf(pdf_stream):
pdf_reader = PdfReader(pdf_stream)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
return text
# Function to extract text from a PDF
#def extract_text_from_pdf(pdf_file_path):
# pdf_reader = PdfReader(pdf_file_path)
# text = ""
# for page in pdf_reader.pages:
# text += page.extract_text()
# return text
# Function to split text into chunks
def chunk_text(text, chunk_size=500, chunk_overlap=50):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size, chunk_overlap=chunk_overlap
)
return text_splitter.split_text(text)
# Function to create embeddings and store them in FAISS
def create_embeddings_and_store(chunks):
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vector_db = FAISS.from_texts(chunks, embedding=embeddings)
return vector_db
# Function to query the vector database and interact with Groq
def query_vector_db(query, vector_db, selected_appliance):
# Filter documents based on appliance title
filtered_documents = [doc for doc in vector_db.documents if doc.title == selected_appliance]
context = "\n".join([doc.page_content for doc in filtered_documents])
# Interact with Groq API (unchanged)
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": f"Use the following context:\n{context}"},
{"role": "user", "content": query},
],
model="llama3-8b-8192",
)
return chat_completion.choices[0].message.content
# Streamlit app
st.title("RAG-Based ChatBot (Already having Document)")
st.write("Processing the Data links...")
all_chunks = []
# Process each predefined Google Drive link
for link in drive_links:
try:
# st.write(f"Processing link: {link}")
# Download PDF
pdf_stream = download_pdf_from_drive(link)
# st.write("PDF Downloaded Successfully!")
# Extract text
text = extract_text_from_pdf(pdf_stream)
# st.write("PDF Text Extracted Successfully!")
# Chunk text
chunks = chunk_text(text)
# st.write(f"Created {len(chunks)} text chunks.")
all_chunks.extend(chunks)
except Exception as e:
st.write(f"Error processing link {link}: {e}")
if all_chunks:
# Generate embeddings and store in FAISS
vector_db = create_embeddings_and_store(all_chunks)
st.write("Data is Ready Successfully!")
# User query input
user_query = st.text_input("Enter your query:")
if user_query:
response = query_vector_db(user_query, vector_db)
st.write("Response from LLM:")
st.write(response)