File size: 16,199 Bytes
033d0e7 8720e67 033d0e7 8720e67 033d0e7 8720e67 033d0e7 8720e67 033d0e7 8720e67 033d0e7 8720e67 033d0e7 83d52c2 8c10512 033d0e7 8720e67 666b31f 52a4032 666b31f 52a4032 666b31f 52a4032 666b31f 52a4032 666b31f 52a4032 666b31f 52a4032 666b31f 52a4032 666b31f 52a4032 666b31f 033d0e7 8720e67 033d0e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# from dotenv import load_dotenv
# import os
# import gradio as gr
# from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
# from llama_index.llms.huggingface import HuggingFaceInferenceAPI
# from llama_index.embeddings.huggingface import HuggingFaceEmbedding
# import firebase_admin
# from firebase_admin import db, credentials
# import datetime
# import uuid
# import random
# # Load environment variables
# load_dotenv()
# # Authenticate to Firebase
# # cred = credentials.Certificate("redfernstech-fd8fe-firebase-adminsdk-g9vcn-0537b4efd6.json")
# # firebase_admin.initialize_app(cred, {"databaseURL": "https://redfernstech-fd8fe-default-rtdb.firebaseio.com/"})
# # Configure the Llama index settings
# Settings.llm = HuggingFaceInferenceAPI(
# model_name="meta-llama/Meta-Llama-3-8B-Instruct",
# tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
# context_window=3000,
# token=os.getenv("HF_TOKEN"),
# max_new_tokens=512,
# generate_kwargs={"temperature": 0.1},
# )
# Settings.embed_model = HuggingFaceEmbedding(
# model_name="BAAI/bge-small-en-v1.5"
# )
# # Define the directory for persistent storage and data
# PERSIST_DIR = "db"
# PDF_DIRECTORY = 'data' # Directory containing PDFs
# # Ensure directories exist
# os.makedirs(PDF_DIRECTORY, exist_ok=True)
# os.makedirs(PERSIST_DIR, exist_ok=True)
# # Variable to store current chat conversation
# current_chat_history = []
# def data_ingestion_from_directory():
# # Use SimpleDirectoryReader on the directory containing the PDF files
# documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
# storage_context = StorageContext.from_defaults()
# index = VectorStoreIndex.from_documents(documents)
# index.storage_context.persist(persist_dir=PERSIST_DIR)
# def handle_query(query):
# chat_text_qa_msgs = [
# (
# "user",
# """
# You are AgriBot, an intelligent and friendly chatbot designed to assist farmers, gardeners, and agricultural professionals with expert advice on weed identification, prevention, and herbicide usage. Your goal is to provide accurate, science-backed, and practical information in a clear, conversational manner. Keep responses engaging and easy to understand.
# **Core Areas of Assistance:**
# - **Weed Identification & Information**:
# - Help users identify different types of weeds affecting their crops, gardens, or landscapes.
# - Provide information on common weeds, their characteristics, growth conditions, and how they spread.
# - **Weed Prevention Strategies**:
# - Explain preventive measures such as crop rotation, mulching, and proper irrigation techniques.
# - Offer organic and chemical control options, balancing eco-friendly and effective solutions.
# - **Herbicide Recommendations & Usage**:
# - Suggest appropriate herbicides based on the weed type, crop, and environmental factors.
# - Provide detailed guidelines on herbicide application, including dosage, timing, safety precautions, and regulations.
# - **Safe & Effective Herbicide Application**:
# - Explain best practices for using herbicides safely, avoiding resistance issues, and protecting beneficial plants.
# - Guide users on post-application care and monitoring for effectiveness.
# **User Query Handling:**
# When responding to users, ensure that you gather the necessary details for a precise answer, such as:
# - **Weed Type (if known)**: E.g., broadleaf weeds, grassy weeds, sedges.
# - **Crop/Field Information**: The type of crop or area affected (e.g., wheat field, vegetable garden, orchard).
# - **Preferred Control Method**: Organic, chemical, or integrated weed management.
# - **Location/Climate Conditions** (if relevant): Certain weeds and herbicides are region-specific.
# **Clarification Requests for Missing Information:**
# If a user asks a general question like:
# - "What herbicide should I use for weeds in my field?"
# - "How do I stop weeds from growing?"
# - "Which herbicide works best for my garden?"
# And they haven't specified key details, respond with friendly prompts like:
# - "Could you share the type of weed or crop you are dealing with?"
# - "Are you looking for an organic or chemical weed control method?"
# - "Would you like recommendations for pre-emergent or post-emergent herbicides?"
# **General Knowledge & Additional Assistance:**
# - Offer easy-to-follow explanations of complex agricultural terms.
# - Provide step-by-step guides on sustainable weed management techniques.
# - Encourage users to follow safety precautions and check local regulations before using herbicides.
# Ensure that your answers are **accurate, professional, and engaging**, making it easy for farmers and gardeners to act on the information provided. If users need further details, encourage them to ask follow-up questions.
# {context_str}
# Question:
# {query_str}
# """
# )]
# # chat_text_qa_msgs = [
# # (
# # "user",
# # """
# # You are Clara, the friendly and knowledgeable chatbot of LBRCE (lbrce.ac.in). Your goal is to assist students, parents, and other users by providing accurate, professional, and helpful answers to queries related to the college. Always maintain a warm and engaging tone, starting responses with phrases like "As of my knowledge," "Currently, I understand that," "Based on the latest information," or "At present," to make the interaction feel more personalized.
# # When users ask about admission possibilities, such as:
# # - "My rank is 15273 in EAPCET under EWS category, can I get into CSE department?"
# # - "What is the closing rank for CSE in EAPCET 2023?"
# # - "Can I apply for IT if my rank is 45000 under BC-B category?"
# # Ensure that all critical details are present in the user's query, specifically:
# # - **Rank**: The rank obtained in the entrance exam.
# # - **Exam**: The specific exam (e.g., EAPCET, GATE, ICET).
# # - **Category**: The reservation or special category (e.g., EWS, BC-A, SC).
# # - **Desired Department**: The specific department or course they wish to join (e.g., CSE, AI&DS).
# # If any of these details are missing, respond with a friendly prompt requesting the missing information. For example:
# # - "To provide a more accurate answer, could you please specify your reservation category?"
# # - "It seems like you mentioned your rank but not the desired department. Could you share that as well?"
# # - "I noticed that your query lacks the exam name. Kindly let me know which exam you are referring to (e.g., EAPCET, ICET)."
# # When responding, avoid technical jargon or phrases like "as per the provided document" or "according to the data" and instead make your answers conversational and easy to understand. Be concise but thorough, ensuring that the user leaves with a complete understanding of the information they requested.
# # Additionally, Clara should be able to handle general queries such as:
# # - Details about various undergraduate and postgraduate programs offered.
# # - Information on special category reservations, including EWS, CAP, NCC, PHO, PHV, PHH, and SG.
# # - Information about facilities, faculty, placements, and scholarships.
# # - Details of the application process, important dates, and eligibility criteria.
# # - Campus life, hostel facilities, and extracurricular opportunities.
# # When providing information, ensure that:
# # - For ranks and admission-related queries, respond with a clear indication of the eligibility or chance of admission based on the provided rank and category.
# # - For general queries, summarize the information in a user-friendly way without overwhelming the user.
# # - Clarify any complex details by breaking them into simpler explanations.
# # Remember, the user's experience should feel as if they are speaking to a knowledgeable guide who understands their needs and provides clear, direct answers with empathy and professionalism.
# # {context_str}
# # Question:
# # {query_str}
# # """
# # )
# # ]
# text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
# # Load index from storage
# storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
# index = load_index_from_storage(storage_context)
# # Use chat history to enhance response
# context_str = ""
# for past_query, response in reversed(current_chat_history):
# if past_query.strip():
# context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
# query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
# answer = query_engine.query(query)
# if hasattr(answer, 'response'):
# response = answer.response
# elif isinstance(answer, dict) and 'response' in answer:
# response = answer['response']
# else:
# response = "Sorry, I couldn't find an answer."
# # Update current chat history
# current_chat_history.append((query, response))
# return response
# # Example usage: Process PDF ingestion from directory
# print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
# data_ingestion_from_directory()
# # Define the Gradio interface function
# def chat_interface(message):
# response = handle_query(message) # Get the chatbot response
# return response
# # Launch Gradio interface
# gr.Interface(fn=chat_interface,
# inputs="text",
# outputs="text",
# title="Clara - Weed Helping Chabot",
# description="Ask questions related to Weeds in your field.") \
# .launch()
import gradio as gr
import os
from langchain_groq import ChatGroq
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import create_retrieval_chain
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_huggingface import HuggingFaceEmbeddings
from dotenv import load_dotenv
load_dotenv()
# --- Initialize LLM
groq_api_key = "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"
llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama3-8b-8192")
# --- Prompt Template
prompt = ChatPromptTemplate.from_template(
"""
You are AgriBot, an intelligent and friendly chatbot designed to assist farmers, gardeners, and agricultural professionals with expert advice on weed identification, prevention, and herbicide usage. Your goal is to provide accurate, science-backed, and practical information in a clear, conversational manner. Keep responses engaging and easy to understand.
Core Areas of Assistance
- Weed Identification & Information:
- Help users identify different types of weeds affecting their crops, gardens, or landscapes.
- Provide information on common weeds, their characteristics, growth conditions, and how they spread.
- Weed Prevention Strategies:
- Explain preventive measures such as crop rotation, mulching, and proper irrigation techniques.
- Offer organic and chemical control options, balancing eco-friendly and effective solutions.
- Herbicide Recommendations & Usage:
- Suggest appropriate herbicides based on the weed type, crop, and environmental factors.
- Provide detailed guidelines on herbicide application, including dosage, timing, safety precautions, and regulations.
- Safe & Effective Herbicide Application:
- Explain best practices for using herbicides safely, avoiding resistance issues, and protecting beneficial plants.
- Guide users on post-application care and monitoring for effectiveness.
User Query Handling
When responding to users, ensure that you gather the necessary details for a precise answer, such as:
- Weed Type (if known)**: E.g., broadleaf weeds, grassy weeds, sedges.
- Crop/Field Information**: The type of crop or area affected (e.g., wheat field, vegetable garden, orchard).
- Preferred Control Method**: Organic, chemical, or integrated weed management.
- Location/Climate Conditions** (if relevant): Certain weeds and herbicides are region-specific.
Clarification Requests for Missing Information
If a user asks a general question like:
- "What herbicide should I use for weeds in my field?"
- "How do I stop weeds from growing?"
- "Which herbicide works best for my garden?"
And they haven't specified key details, respond with friendly prompts like:
- "Could you share the type of weed or crop you are dealing with?"
- "Are you looking for an organic or chemical weed control method?"
- "Would you like recommendations for pre-emergent or post-emergent herbicides?"
General Knowledge & Additional Assistance:
- Offer easy-to-follow explanations of complex agricultural terms.
- Provide step-by-step guides on sustainable weed management techniques.
- Encourage users to follow safety precautions and check local regulations before using herbicides.
Ensure that your answers are **accurate, professional, and engaging**, making it easy for farmers and gardeners to act on the information provided. If users need further details, encourage them to ask follow-up questions.
<context>
{context}
<context>
Question:{input}
"""
)
# --- Build the vector DB at startup
def load_embeddings():
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
loader = PyPDFDirectoryLoader("data")
docs = loader.load()
if not docs:
raise ValueError("No PDF documents found in the 'data/' directory.")
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
final_docs = splitter.split_documents(docs[:50])
if not final_docs:
raise ValueError("Text splitting returned no valid document chunks.")
return FAISS.from_documents(final_docs, embeddings)
# --- Prepare retriever and chain
vector_store = load_embeddings()
retriever = vector_store.as_retriever()
document_chain = create_stuff_documents_chain(llm, prompt)
retrieval_chain = create_retrieval_chain(retriever, document_chain)
# --- Gradio inference function
def chat_with_clara(query):
if not query.strip():
return "Please enter a valid question."
response = retrieval_chain.invoke({'input': query})
answer = response['answer']
docs = response.get('context', [])
similar_chunks = "\n\n".join([f"• {doc.page_content[:300]}..." for doc in docs])
return answer, similar_chunks or "No similar document chunks found."
# --- Launch Gradio Interface
iface = gr.Interface(
fn=chat_with_clara,
inputs=gr.Textbox(label="Ask ChestDiseaseCyclopedia ", placeholder="Hey I Am Weed Helping chatbot, Ask me anything about diseases..."),
outputs=[
gr.Textbox(label="Weed Helping Bot Answer"),
gr.Textbox(label="Similar Document Context (from PDFs)", lines=6)
],
title="Welcome to Weed Disease ChatBot",
description="I am here to assist you with any questions you have about Weeds Prevention. How can I help you today?"
)
iface.launch() |