agentsay commited on
Commit
c71fef6
·
verified ·
1 Parent(s): e3e4cc6

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +35 -0
  2. api.py +197 -0
  3. config.py +1 -0
  4. crop_disease_qa.json +154 -0
  5. requirements.txt +9 -0
Dockerfile ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install system dependencies
6
+ RUN apt-get update && apt-get install -y --no-install-recommends \
7
+ libgl1 \
8
+ libglib2.0-0 \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ # Create directories
12
+ RUN mkdir -p chroma_crop_rag /app/data && chmod -R 777 chroma_crop_rag /app/data
13
+
14
+ # Set HF_HOME
15
+ ENV HF_HOME=/app/cache
16
+
17
+ # Copy data folder and other files
18
+ COPY chroma_crop_rag /app/data
19
+
20
+ COPY requirements.txt /app/
21
+ COPY api.py /app/
22
+ # COPY engine.py /app/
23
+ # COPY futureWeather.py /app/
24
+ COPY config.py /app/
25
+ COPY crop_disease_qa.json /app/
26
+
27
+ # Install dependencies
28
+ RUN pip install python-multipart
29
+ RUN pip install --no-cache-dir -r requirements.txt
30
+
31
+ # Debug: List files to verify
32
+ RUN ls -la /app/data
33
+
34
+ EXPOSE 7860
35
+ CMD ["python", "api.py"]
api.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import collections
2
+ from collections.abc import MutableMapping
3
+ collections.MutableMapping = MutableMapping # Patch for deprecated MutableMapping
4
+
5
+ import os
6
+ import shutil
7
+ import json
8
+ import logging
9
+ from contextlib import asynccontextmanager
10
+ from typing import Dict
11
+
12
+ from fastapi import FastAPI, HTTPException
13
+ from fastapi.responses import JSONResponse
14
+ from fastapi.middleware.cors import CORSMiddleware
15
+ from pydantic import BaseModel
16
+
17
+ from langchain.chat_models import init_chat_model
18
+ from langchain_core.documents import Document
19
+ from langchain_core.prompts import PromptTemplate
20
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
21
+ from langchain_community.vectorstores import Chroma
22
+ from langchain_huggingface import HuggingFaceEmbeddings
23
+ from langchain.chains import RetrievalQA
24
+
25
+ import config # Ensure config.py has GROQ_API_KEY
26
+
27
+ # Set environment variable for Groq API key
28
+ os.environ["GROQ_API_KEY"] = config.GROQ_API_KEY
29
+
30
+ # Setup logging
31
+ logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
32
+ logger = logging.getLogger(__name__)
33
+
34
+ # Global variables for RAG components
35
+ rag_chain = None
36
+ retriever = None
37
+ session_states: Dict[str, str] = {} # Store last_disease per session_id
38
+
39
+
40
+ @asynccontextmanager
41
+ async def lifespan(app: FastAPI):
42
+ global rag_chain, retriever
43
+ persist_directory = "/data/chroma_crop_rag"
44
+
45
+ # Clear existing ChromaDB collection
46
+ if os.path.exists(persist_directory):
47
+ try:
48
+ shutil.rmtree(persist_directory)
49
+ logger.debug("Cleared existing ChromaDB directory: %s", persist_directory)
50
+ except Exception as e:
51
+ logger.error("Error clearing ChromaDB directory: %s", str(e))
52
+ raise
53
+
54
+ # Load JSON QA Knowledge Base
55
+ try:
56
+ with open("crop_disease_qa.json", "r", encoding="utf-8") as f:
57
+ data = json.load(f)
58
+ logger.debug("JSON loaded, length: %d", len(data))
59
+ except Exception as e:
60
+ logger.error("Error loading JSON: %s", str(e))
61
+ raise
62
+
63
+ # Convert to Documents
64
+ documents = [
65
+ Document(page_content=item["answer"], metadata={"question": item["question"]})
66
+ for item in data
67
+ ]
68
+ logger.debug("Documents created: %d", len(documents))
69
+
70
+ # Chunk Documents
71
+ splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=100)
72
+ docs = splitter.split_documents(documents)
73
+ logger.debug("Documents after splitting: %d", len(docs))
74
+
75
+ # Embedding + Vectorstore
76
+ try:
77
+ embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
78
+ logger.debug("Embedding model initialized")
79
+ db = Chroma.from_documents(docs, embedding_model, persist_directory=persist_directory)
80
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 6})
81
+ logger.debug("ChromaDB initialized")
82
+ except Exception as e:
83
+ logger.error("ChromaDB/Embedding error: %s", str(e))
84
+ raise
85
+
86
+ # Groq LLM
87
+ try:
88
+ llm = init_chat_model(
89
+ "llama3-8b-8192",
90
+ model_provider="groq",
91
+ temperature=0.5
92
+ )
93
+ logger.debug("Groq LLM initialized")
94
+ except Exception as e:
95
+ logger.error("Groq LLM initialization error: %s", str(e))
96
+ raise
97
+
98
+ # Prompt Template
99
+ prompt_template = PromptTemplate(
100
+ input_variables=["context", "question"],
101
+ template="""
102
+ You're a friendly agricultural expert helping farmers with crop health.
103
+ Answer in a warm, conversational tone, like you're chatting with a neighbor.
104
+ Keep it clear, engaging, and avoid overly technical terms.
105
+ Use the provided context from the knowledge base to ensure accuracy.
106
+ If the question is a follow-up (e.g., 'how to treat them?', 'how to fix it?', 'what medicines should I use?'),
107
+ assume it refers to the previously discussed disease or crop (e.g., Early blight in Potato) unless specified otherwise.
108
+ If the context doesn't cover the question, provide a practical, general response with actionable tips.
109
+ make sure the aize of the answer is not more than 100 words.
110
+ Context: {context}
111
+
112
+ Question: {question}
113
+
114
+ Answer:
115
+ """
116
+ )
117
+
118
+ # RAG Chain
119
+ try:
120
+ rag_chain = RetrievalQA.from_chain_type(
121
+ llm=llm,
122
+ retriever=retriever,
123
+ chain_type="stuff",
124
+ chain_type_kwargs={"prompt": prompt_template}
125
+ )
126
+ logger.debug("RAG chain initialized")
127
+ except Exception as e:
128
+ logger.error("RAG chain initialization error: %s", str(e))
129
+ raise
130
+
131
+ yield # FastAPI is now running
132
+
133
+
134
+ # Initialize FastAPI with lifespan
135
+ app = FastAPI(title="Crop Health Assistant API", lifespan=lifespan)
136
+
137
+ # Add CORS middleware
138
+ app.add_middleware(
139
+ CORSMiddleware,
140
+ allow_origins=["*"], # Allows all origins
141
+ allow_credentials=True,
142
+ allow_methods=["*"], # Allows all methods
143
+ allow_headers=["*"], # Allows all headers
144
+ )
145
+
146
+
147
+ # Pydantic request model
148
+ class QueryRequest(BaseModel):
149
+ query: str
150
+ session_id: str = "default"
151
+
152
+
153
+ # Query endpoint
154
+ @app.post("/query")
155
+ async def query_crop_health(request: QueryRequest):
156
+ global session_states
157
+ query = request.query
158
+ session_id = request.session_id
159
+
160
+ if query.lower() == "exit":
161
+ session_states.pop(session_id, None)
162
+ return JSONResponse(content={"message": "Session ended"})
163
+
164
+ # Handle follow-up queries
165
+ modified_query = query
166
+ last_disease = session_states.get(session_id)
167
+ if last_disease and query.lower() in [
168
+ "how to treat them?", "how to fix it?",
169
+ "how to manage it?", "what medicines should i use?"
170
+ ]:
171
+ modified_query = f"What medicines or treatments for {last_disease}?"
172
+
173
+ try:
174
+ response = rag_chain.invoke({"query": modified_query})["result"]
175
+ # Simple heuristic to update last disease
176
+ if "blight" in query.lower() or "potato" in query.lower():
177
+ session_states[session_id] = "Early blight in Potato"
178
+
179
+ return JSONResponse(content={"question": query, "answer": response})
180
+ except Exception as e:
181
+ logger.error("RAG chain execution error for query '%s': %s", query, str(e))
182
+ raise HTTPException(status_code=500, detail=f"Error processing query: {str(e)}")
183
+
184
+
185
+ # Session reset endpoint
186
+ @app.delete("/reset-session/{session_id}")
187
+ async def reset_session(session_id: str):
188
+ global session_states
189
+ session_states.pop(session_id, None)
190
+ return JSONResponse(content={"message": f"Session {session_id} reset"})
191
+
192
+
193
+ # Run FastAPI with Uvicorn
194
+ if __name__ == "__main__":
195
+ import uvicorn
196
+ print("Starting FastAPI server")
197
+ uvicorn.run(app, host="0.0.0.0", port=7860)
config.py ADDED
@@ -0,0 +1 @@
 
 
1
+ GROQ_API_KEY="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"
crop_disease_qa.json ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "question": "What is Apple scab in Apple?",
4
+ "answer": "Apple scab is a disease that affects Apple crops. Proper identification and treatment is essential to manage it."
5
+ },
6
+ {
7
+ "question": "What is Black rot in Apple?",
8
+ "answer": "Black rot is a disease that affects Apple crops. Proper identification and treatment is essential to manage it."
9
+ },
10
+ {
11
+ "question": "What is Cedar apple rust in Apple?",
12
+ "answer": "Cedar apple rust is a disease that affects Apple crops. Proper identification and treatment is essential to manage it."
13
+ },
14
+ {
15
+ "question": "How to keep Apple crops healthy?",
16
+ "answer": "To maintain healthy Apple crops, ensure good soil, irrigation, pest control, and timely fertilization."
17
+ },
18
+ {
19
+ "question": "How to keep Blueberry crops healthy?",
20
+ "answer": "To maintain healthy Blueberry crops, ensure good soil, irrigation, pest control, and timely fertilization."
21
+ },
22
+ {
23
+ "question": "How to keep Cherry including sour Powdery mildew crops healthy?",
24
+ "answer": "To maintain healthy Cherry including sour Powdery mildew crops, ensure good soil, irrigation, pest control, and timely fertilization."
25
+ },
26
+ {
27
+ "question": "How to keep Cherry including sour healthy crops healthy?",
28
+ "answer": "To maintain healthy Cherry including sour healthy crops, ensure good soil, irrigation, pest control, and timely fertilization."
29
+ },
30
+ {
31
+ "question": "How to keep Corn maize Cercospora leaf spot Gray leaf spot crops healthy?",
32
+ "answer": "To maintain healthy Corn maize Cercospora leaf spot Gray leaf spot crops, ensure good soil, irrigation, pest control, and timely fertilization."
33
+ },
34
+ {
35
+ "question": "How to keep Corn maizeCommon rust crops healthy?",
36
+ "answer": "To maintain healthy Corn maizeCommon rust crops, ensure good soil, irrigation, pest control, and timely fertilization."
37
+ },
38
+ {
39
+ "question": "How to keep Corn maize Northern Leaf Blight crops healthy?",
40
+ "answer": "To maintain healthy Corn maize Northern Leaf Blight crops, ensure good soil, irrigation, pest control, and timely fertilization."
41
+ },
42
+ {
43
+ "question": "How to keep Corn maize healthy crops healthy?",
44
+ "answer": "To maintain healthy Corn maize healthy crops, ensure good soil, irrigation, pest control, and timely fertilization."
45
+ },
46
+ {
47
+ "question": "What is Black rot in Grape?",
48
+ "answer": "Black rot is a disease that affects Grape crops. Proper identification and treatment is essential to manage it."
49
+ },
50
+ {
51
+ "question": "What is Esca(Black Measles) in Grape?",
52
+ "answer": "Esca(Black Measles) is a disease that affects Grape crops. Proper identification and treatment is essential to manage it."
53
+ },
54
+ {
55
+ "question": "What is Leaf blight(Isariopsis Leaf Spot) in Grape?",
56
+ "answer": "Leaf blight(Isariopsis Leaf Spot) is a disease that affects Grape crops. Proper identification and treatment is essential to manage it."
57
+ },
58
+ {
59
+ "question": "How to keep Grape crops healthy?",
60
+ "answer": "To maintain healthy Grape crops, ensure good soil, irrigation, pest control, and timely fertilization."
61
+ },
62
+ {
63
+ "question": "What is Haunglongbing(Citrus greening) in Orange?",
64
+ "answer": "Haunglongbing(Citrus greening) is a disease that affects Orange crops. Proper identification and treatment is essential to manage it."
65
+ },
66
+ {
67
+ "question": "What is Bacterial spot in Peach?",
68
+ "answer": "Bacterial spot is a disease that affects Peach crops. Proper identification and treatment is essential to manage it."
69
+ },
70
+ {
71
+ "question": "How to keep Peach crops healthy?",
72
+ "answer": "To maintain healthy Peach crops, ensure good soil, irrigation, pest control, and timely fertilization."
73
+ },
74
+ {
75
+ "question": "What is Bacterial spot in Pepper,bell?",
76
+ "answer": "Bacterial spot is a disease that affects Pepper,bell crops. Proper identification and treatment is essential to manage it."
77
+ },
78
+ {
79
+ "question": "How to keep Pepper,bell crops healthy?",
80
+ "answer": "To maintain healthy Pepper,bell crops, ensure good soil, irrigation, pest control, and timely fertilization."
81
+ },
82
+ {
83
+ "question": "What is Early blight in Potato?",
84
+ "answer": "Early blight is a disease that affects Potato crops. Proper identification and treatment is essential to manage it."
85
+ },
86
+ {
87
+ "question": "What is Late blight in Potato?",
88
+ "answer": "Late blight is a disease that affects Potato crops. Proper identification and treatment is essential to manage it."
89
+ },
90
+ {
91
+ "question": "How to keep Potato crops healthy?",
92
+ "answer": "To maintain healthy Potato crops, ensure good soil, irrigation, pest control, and timely fertilization."
93
+ },
94
+ {
95
+ "question": "How to keep Raspberry crops healthy?",
96
+ "answer": "To maintain healthy Raspberry crops, ensure good soil, irrigation, pest control, and timely fertilization."
97
+ },
98
+ {
99
+ "question": "How to keep Soybean crops healthy?",
100
+ "answer": "To maintain healthy Soybean crops, ensure good soil, irrigation, pest control, and timely fertilization."
101
+ },
102
+ {
103
+ "question": "What is Powdery mildew in Squash?",
104
+ "answer": "Powdery mildew is a disease that affects Squash crops. Proper identification and treatment is essential to manage it."
105
+ },
106
+ {
107
+ "question": "What is Leaf scorch in Strawberry?",
108
+ "answer": "Leaf scorch is a disease that affects Strawberry crops. Proper identification and treatment is essential to manage it."
109
+ },
110
+ {
111
+ "question": "How to keep Strawberry crops healthy?",
112
+ "answer": "To maintain healthy Strawberry crops, ensure good soil, irrigation, pest control, and timely fertilization."
113
+ },
114
+ {
115
+ "question": "What is Bacterial spot in Tomato?",
116
+ "answer": "Bacterial spot is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
117
+ },
118
+ {
119
+ "question": "What is Early blight in Tomato?",
120
+ "answer": "Early blight is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
121
+ },
122
+ {
123
+ "question": "What is Late blight in Tomato?",
124
+ "answer": "Late blight is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
125
+ },
126
+ {
127
+ "question": "What is Leaf Mold in Tomato?",
128
+ "answer": "Leaf Mold is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
129
+ },
130
+ {
131
+ "question": "What is Septoria leaf spot in Tomato?",
132
+ "answer": "Septoria leaf spot is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
133
+ },
134
+ {
135
+ "question": "What is Spider mites Two-spotted spider mite in Tomato?",
136
+ "answer": "Spider mites Two-spotted spider mite is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
137
+ },
138
+ {
139
+ "question": "What is Target Spot in Tomato?",
140
+ "answer": "Target Spot is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
141
+ },
142
+ {
143
+ "question": "What is Tomato Yellow Leaf Curl Virus in Tomato?",
144
+ "answer": "Tomato Yellow Leaf Curl Virus is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
145
+ },
146
+ {
147
+ "question": "What is Tomato mosaic virus in Tomato?",
148
+ "answer": "Tomato mosaic virus is a disease that affects Tomato crops. Proper identification and treatment is essential to manage it."
149
+ },
150
+ {
151
+ "question": "How to keep Tomato crops healthy?",
152
+ "answer": "To maintain healthy Tomato crops, ensure good soil, irrigation, pest control, and timely fertilization."
153
+ }
154
+ ]
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ fastapi==0.116.1
2
+ langchain==0.3.27
3
+ langchain_community==0.3.27
4
+ langchain_core==0.3.72
5
+ langchain_google_genai==2.1.8
6
+ langchain_huggingface==0.3.1
7
+ langchain_text_splitters==0.3.9
8
+ pydantic==2.11.7
9
+ uvicorn==0.35.0