Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
from pinecone import Pinecone
|
| 5 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 6 |
+
from langchain_pinecone import PineconeVectorStore
|
| 7 |
+
from langchain_core.documents import Document
|
| 8 |
+
from uuid import uuid4
|
| 9 |
+
from langchain.chains import RetrievalQA
|
| 10 |
+
from langchain.llms import HuggingFaceHub
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Get API keys from environment variables
|
| 16 |
+
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 17 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 18 |
+
huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 19 |
+
|
| 20 |
+
# Check if API keys are available
|
| 21 |
+
if not pinecone_api_key or not google_api_key or not huggingfacehub_api_token:
|
| 22 |
+
st.error("API keys not found. Please set PINECONE_API_KEY, GOOGLE_API_KEY, and HUGGINGFACEHUB_API_TOKEN in your .env file.")
|
| 23 |
+
st.stop()
|
| 24 |
+
|
| 25 |
+
# Initialize Pinecone
|
| 26 |
+
pc = Pinecone(api_key=pinecone_api_key, environment="us-east1-gcp") # Replace with your environment if needed
|
| 27 |
+
index_name = "online-rag"
|
| 28 |
+
index = pc.Index(index_name)
|
| 29 |
+
|
| 30 |
+
# Initialize embeddings
|
| 31 |
+
os.environ['GOOGLE_API_KEY'] = google_api_key
|
| 32 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
|
| 33 |
+
|
| 34 |
+
# Initialize vector store
|
| 35 |
+
vector_store = PineconeVectorStore(index=index, embedding=embeddings)
|
| 36 |
+
|
| 37 |
+
# Initialize LLaMA 30B model from Hugging Face
|
| 38 |
+
llm = HuggingFaceHub(repo_id="meta-llama/Llama-2-30b-chat-hf", huggingfacehub_api_token=huggingfacehub_api_token)
|
| 39 |
+
|
| 40 |
+
# Streamlit app
|
| 41 |
+
st.title("LLaMA 30B RAG Chatbot")
|
| 42 |
+
|
| 43 |
+
# Upload document
|
| 44 |
+
uploaded_file = st.file_uploader("Upload a document", type=["txt", "pdf"])
|
| 45 |
+
|
| 46 |
+
if uploaded_file is not None:
|
| 47 |
+
# Read the file
|
| 48 |
+
file_details = {"filename": uploaded_file.name, "filetype": uploaded_file.type, "filesize": uploaded_file.size}
|
| 49 |
+
st.write(file_details)
|
| 50 |
+
file_content = uploaded_file.read().decode("utf-8")
|
| 51 |
+
|
| 52 |
+
# Create a document
|
| 53 |
+
document = Document(page_content=file_content, metadata={"source": uploaded_file.name})
|
| 54 |
+
|
| 55 |
+
# Add document to vector store
|
| 56 |
+
uuids = [str(uuid4()) for _ in range(1)]
|
| 57 |
+
vector_store.add_documents(documents=[document], ids=uuids)
|
| 58 |
+
st.write("Document added to Pinecone.")
|
| 59 |
+
|
| 60 |
+
# Query the chatbot
|
| 61 |
+
query = st.text_input("Enter your query:")
|
| 62 |
+
|
| 63 |
+
if query:
|
| 64 |
+
try:
|
| 65 |
+
# Perform similarity search
|
| 66 |
+
results = vector_store.similarity_search(query, k=2)
|
| 67 |
+
st.write("Search Results:")
|
| 68 |
+
for res in results:
|
| 69 |
+
st.write(f"* {res.page_content} [{res.metadata}]")
|
| 70 |
+
|
| 71 |
+
# Create a RetrievalQA chain
|
| 72 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_store.as_retriever())
|
| 73 |
+
|
| 74 |
+
# Get the answer
|
| 75 |
+
answer = qa_chain.run(query)
|
| 76 |
+
st.write("Chatbot Response:")
|
| 77 |
+
st.write(answer)
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
st.error(f"An error occurred: {e}")
|