clean folder structure
Browse files- app/Dockerfile.api +0 -5
- app/main.py +0 -25
- app/rag.py +0 -56
- app/requirements.txt +0 -7
- app/streamlit_app.py +0 -73
- app/templates/index.html +0 -16
- archive/RAG.ipynb +0 -0
- {src → notebooks}/experiment.ipynb +0 -0
app/Dockerfile.api
DELETED
|
@@ -1,5 +0,0 @@
|
|
| 1 |
-
FROM python:3.10
|
| 2 |
-
WORKDIR /app
|
| 3 |
-
COPY . /app
|
| 4 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 5 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/main.py
DELETED
|
@@ -1,25 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from langchain_community.vectorstores import FAISS
|
| 3 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
-
from langchain.chains import RetrievalQA
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 7 |
-
|
| 8 |
-
app = FastAPI()
|
| 9 |
-
|
| 10 |
-
vectorstore = FAISS.load_local(
|
| 11 |
-
"./vectorstore/",
|
| 12 |
-
embeddings=HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf") # Or llama-3
|
| 16 |
-
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
|
| 17 |
-
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
|
| 18 |
-
llm = HuggingFacePipeline(pipeline=pipe)
|
| 19 |
-
|
| 20 |
-
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 21 |
-
rag_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever, chain_type="stuff")
|
| 22 |
-
|
| 23 |
-
@app.get("/query/")
|
| 24 |
-
def query_rag(question: str):
|
| 25 |
-
return {"response": rag_chain.run(question)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/rag.py
DELETED
|
@@ -1,56 +0,0 @@
|
|
| 1 |
-
# source:https://python.langchain.com/api_reference/langchain/chains/langchain.chains.retrieval_qa.base.RetrievalQA.html
|
| 2 |
-
|
| 3 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
-
from langchain_community.vectorstores import FAISS
|
| 5 |
-
from langchain.prompts import ChatPromptTemplate
|
| 6 |
-
from langchain.chains import create_retrieval_chain
|
| 7 |
-
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 8 |
-
from langchain_community.llms import Ollama
|
| 9 |
-
from fastapi import FastAPI
|
| 10 |
-
import requests
|
| 11 |
-
from pydantic import BaseModel
|
| 12 |
-
from langchain.chains import create_retrieval_chain
|
| 13 |
-
from dotenv import load_dotenv
|
| 14 |
-
import os
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
load_dotenv()
|
| 18 |
-
token= os.getenv("TOKEN")
|
| 19 |
-
app = FastAPI()
|
| 20 |
-
|
| 21 |
-
class QueryInput(BaseModel):
|
| 22 |
-
query: str
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# build the retrieval and augmented generator chain here
|
| 26 |
-
|
| 27 |
-
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 28 |
-
db = FAISS.load_local("./vectorstore/agriquery_faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 29 |
-
llm = Ollama(model="llama3", base_url="http://localhost:11434")
|
| 30 |
-
|
| 31 |
-
retriever = db.as_retriever()
|
| 32 |
-
|
| 33 |
-
system_prompt = (
|
| 34 |
-
"You are an agriultural research assistant."
|
| 35 |
-
"Use the given context to answer the question."
|
| 36 |
-
"If you don't know the answer, say you don't know."
|
| 37 |
-
"Context: {context}"
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
prompt = ChatPromptTemplate.from_messages(
|
| 41 |
-
[
|
| 42 |
-
("system", system_prompt),
|
| 43 |
-
("human", "{input}"),
|
| 44 |
-
]
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
question_answer_chain = create_stuff_documents_chain(llm,prompt)
|
| 48 |
-
chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
@app.post("/query")
|
| 52 |
-
async def query_handler(input: QueryInput):
|
| 53 |
-
result = chain.invoke({"input": input.query})
|
| 54 |
-
answer = result['answer'].replace("\\n", "\n").strip()
|
| 55 |
-
|
| 56 |
-
return {"answer": answer}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/requirements.txt
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
fastapi
|
| 2 |
-
uvicorn
|
| 3 |
-
transformers
|
| 4 |
-
sentence-transformers
|
| 5 |
-
faiss-cpu
|
| 6 |
-
langchain
|
| 7 |
-
langchain_community
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/streamlit_app.py
DELETED
|
@@ -1,73 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from langchain.chains import RetrievalQA
|
| 3 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 4 |
-
from transformers import pipeline
|
| 5 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
-
from langchain_community.vectorstores import FAISS
|
| 7 |
-
from langchain.prompts import ChatPromptTemplate
|
| 8 |
-
from langchain.chains import create_retrieval_chain
|
| 9 |
-
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 10 |
-
from langchain_community.llms import Ollama
|
| 11 |
-
import os
|
| 12 |
-
import torch
|
| 13 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# ----------------------
|
| 17 |
-
system_prompt = (
|
| 18 |
-
"You are an agriultural research assistant."
|
| 19 |
-
"Use the given context to answer the question."
|
| 20 |
-
"If you don't know the answer, say you don't know."
|
| 21 |
-
"Context: {context}"
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
prompt = ChatPromptTemplate.from_messages(
|
| 25 |
-
[
|
| 26 |
-
("system", system_prompt),
|
| 27 |
-
("human", "{input}"),
|
| 28 |
-
]
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# Initialize embeddings & documents
|
| 32 |
-
@st.cache_resource
|
| 33 |
-
def load_retriever():
|
| 34 |
-
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 35 |
-
db = FAISS.load_local("./vectorstore/agriquery_faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 36 |
-
retriever = db.as_retriever()
|
| 37 |
-
return retriever
|
| 38 |
-
|
| 39 |
-
# Load a lightweight model via HuggingFace pipeline
|
| 40 |
-
@st.cache_resource
|
| 41 |
-
def load_llm():
|
| 42 |
-
# pipe = pipeline("text-generation", model="google/flan-t5-small", max_new_tokens=256)
|
| 43 |
-
|
| 44 |
-
# load the tokenizer and model on cpu/gpu
|
| 45 |
-
model_name = "meta-llama/Llama-2-7b-chat-hf"
|
| 46 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 47 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
| 48 |
-
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=256)
|
| 49 |
-
return HuggingFacePipeline(pipeline=pipe)
|
| 50 |
-
|
| 51 |
-
# Setup RAG Chain
|
| 52 |
-
@st.cache_resource
|
| 53 |
-
def setup_qa():
|
| 54 |
-
|
| 55 |
-
retriever = load_retriever()
|
| 56 |
-
llm = load_llm()
|
| 57 |
-
question_answer_chain = create_stuff_documents_chain(llm,prompt)
|
| 58 |
-
chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 59 |
-
|
| 60 |
-
# qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
| 61 |
-
return chain
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Streamlit App UI
|
| 65 |
-
st.title("🌾 AgriQuery: RAG-Based Q&A Assistant")
|
| 66 |
-
|
| 67 |
-
query = st.text_input("Ask a question related to agriculture:")
|
| 68 |
-
|
| 69 |
-
if query:
|
| 70 |
-
qa = setup_qa()
|
| 71 |
-
with st.spinner("Thinking..."):
|
| 72 |
-
result = qa.invoke({"input": query})
|
| 73 |
-
st.success(result['answer'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/templates/index.html
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
<!DOCTYPE html>
|
| 3 |
-
<html lang="en">
|
| 4 |
-
<head>
|
| 5 |
-
<meta charset="UTF-8">
|
| 6 |
-
<title>AgriQuery</title>
|
| 7 |
-
</head>
|
| 8 |
-
<body>
|
| 9 |
-
<h1>AgriQuery</h1>
|
| 10 |
-
<form action="/query" method="get">
|
| 11 |
-
<label for="q">Enter your question:</label><br>
|
| 12 |
-
<input type="text" id="q" name="q" required><br><br>
|
| 13 |
-
<input type="submit" value="Ask">
|
| 14 |
-
</form>
|
| 15 |
-
</body>
|
| 16 |
-
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
archive/RAG.ipynb
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
{src → notebooks}/experiment.ipynb
RENAMED
|
File without changes
|