Spaces:
Running
Running
Upload 4 files
Browse files- .gitattributes +1 -0
- app.py +80 -0
- faiss_index_datamodel/index.faiss +3 -0
- faiss_index_datamodel/index.pkl +3 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
faiss_index_datamodel/index.faiss filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
from langchain.vectorstores import FAISS
|
| 4 |
+
from langchain.chat_models import ChatOpenAI
|
| 5 |
+
from langchain.prompts import ChatPromptTemplate
|
| 6 |
+
from langchain_groq import ChatGroq
|
| 7 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
+
|
| 9 |
+
model_name = "sentence-transformers/all-mpnet-base-v2"
|
| 10 |
+
hf_embeddings = HuggingFaceEmbeddings(
|
| 11 |
+
model_name = model_name,
|
| 12 |
+
model_kwargs = {'device':'cpu'},
|
| 13 |
+
encode_kwargs = {'normalize_embeddings': False}
|
| 14 |
+
)
|
| 15 |
+
from langchain_groq import ChatGroq
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_resources():
|
| 18 |
+
vectorstore = FAISS.load_local("faiss_index_datamodel", hf_embeddings, allow_dangerous_deserialization=True)
|
| 19 |
+
llm = ChatGroq(
|
| 20 |
+
temperature=0,
|
| 21 |
+
groq_api_key = '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',
|
| 22 |
+
model_name = "llama-3.1-8b-instant"
|
| 23 |
+
)
|
| 24 |
+
return vectorstore, llm
|
| 25 |
+
|
| 26 |
+
vectorstore, llm = load_resources()
|
| 27 |
+
|
| 28 |
+
prompt_template = ChatPromptTemplate.from_template(
|
| 29 |
+
"""You are a knowledgeable and helpful agricultural expert. Your task is to provide clear, concise, and accurate answers to questions on various agricultural topics.
|
| 30 |
+
|
| 31 |
+
**Guidelines:**
|
| 32 |
+
|
| 33 |
+
1. **Thoroughly address all aspects of the question**, using information from the provided context **and your own expertise**.
|
| 34 |
+
2. **Include specific examples and details** to illustrate key points, such as:
|
| 35 |
+
- Names of states, regions, or countries.
|
| 36 |
+
- Specific crops, livestock, or agricultural practices.
|
| 37 |
+
- Tools, technologies, or services (e.g., online platforms, government programs).
|
| 38 |
+
3. Provide **practical, actionable advice** that the questioner can implement.
|
| 39 |
+
4. **Stay directly relevant** to the question, avoiding unnecessary or unrelated information.
|
| 40 |
+
5. Respond in a **natural, conversational tone** as if speaking directly to the person asking the question.
|
| 41 |
+
6. Keep your response **focused and concise**, ideally within 4-5 sentences.
|
| 42 |
+
7. If the question cannot be answered accurately, **acknowledge the limitations gently** and provide the best possible response.
|
| 43 |
+
8. **Avoid using phrases** like "Based on the context" or "From the given information" in your response.
|
| 44 |
+
|
| 45 |
+
**Context:**
|
| 46 |
+
{context}
|
| 47 |
+
|
| 48 |
+
**Question:**
|
| 49 |
+
{question}
|
| 50 |
+
|
| 51 |
+
**Answer:**"""
|
| 52 |
+
)
|
| 53 |
+
chain = prompt_template | llm
|
| 54 |
+
|
| 55 |
+
st.title("AgriChat")
|
| 56 |
+
|
| 57 |
+
if "messages" not in st.session_state:
|
| 58 |
+
st.session_state.messages = []
|
| 59 |
+
|
| 60 |
+
for message in st.session_state.messages:
|
| 61 |
+
with st.chat_message(message["role"]):
|
| 62 |
+
st.markdown(message["content"])
|
| 63 |
+
|
| 64 |
+
if prompt := st.chat_input("Ask your agricultural question:"):
|
| 65 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 66 |
+
|
| 67 |
+
relevant_documents = vectorstore.similarity_search_with_score(prompt, k=4)
|
| 68 |
+
context = "\n\n".join([doc[0].page_content for doc in relevant_documents])
|
| 69 |
+
|
| 70 |
+
response = chain.invoke({"context": context, "question": prompt})
|
| 71 |
+
|
| 72 |
+
st.session_state.messages.append({"role": "assistant", "content": response.content})
|
| 73 |
+
|
| 74 |
+
st.rerun()
|
| 75 |
+
|
| 76 |
+
st.sidebar.header("Instructions")
|
| 77 |
+
st.sidebar.markdown("""
|
| 78 |
+
- Type your question in the input box and press Enter.
|
| 79 |
+
- The assistant will provide answers based on relevant agricultural contexts.
|
| 80 |
+
""")
|
faiss_index_datamodel/index.faiss
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:34d27d1347a851c0f775dcec8d004e68fa98b84983a453649f3623ae16841391
|
| 3 |
+
size 71224365
|
faiss_index_datamodel/index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a63ea5b3ee4c9e4e4ce652b13c3df674e75bef471cd7ba4aa8dbf8c7b6692228
|
| 3 |
+
size 25540079
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain
|
| 3 |
+
langchain-community
|
| 4 |
+
langchain-groq
|
| 5 |
+
faiss-cpu
|
| 6 |
+
sentence-transformers
|