File size: 9,553 Bytes
c8d3120
 
 
 
 
 
bb54a91
c8d3120
 
 
 
bb54a91
c8d3120
ad0ef65
416b3c8
ad0ef65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f839055
 
3d9cf23
f839055
 
 
 
c8d3120
ad0ef65
c8d3120
 
 
 
 
 
 
 
 
 
 
 
 
f839055
 
49a4b57
f839055
 
3d9cf23
f839055
 
796e19e
adf5c3a
c8d3120
f839055
 
 
 
 
 
 
493b1d3
f839055
c8d3120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad0ef65
c8d3120
 
 
 
 
 
 
 
 
 
 
01ca356
7ae835e
01ca356
7ae835e
01ca356
 
7ae835e
 
4bd1ca5
36ff196
2d1b8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bd1ca5
7ae835e
 
 
01ca356
 
 
493b1d3
01ca356
 
493b1d3
 
01ca356
 
 
 
493b1d3
 
bb54a91
 
7ae835e
bb54a91
 
 
7ae835e
4bd1ca5
36ff196
2d1b8d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bd1ca5
7ae835e
 
 
bb54a91
 
 
 
c8d3120
 
 
 
f839055
c8d3120
 
 
 
 
 
 
 
 
bb54a91
 
 
 
 
 
c8d3120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb54a91
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
import streamlit as st
import os
from dotenv import load_dotenv
import time
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from langchain_groq import ChatGroq
from langchain.chains import RetrievalQA
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import LLMChain

# Set persistent storage path
PERSISTENT_DIR = "vector_db"

def initialize_vector_db():
    # Check if vector database already exists in persistent storage
    if os.path.exists(PERSISTENT_DIR) and os.listdir(PERSISTENT_DIR):
        embeddings = HuggingFaceEmbeddings()
        vector_db = Chroma(persist_directory=PERSISTENT_DIR, embedding_function=embeddings)
        return None, vector_db
    
    base_dir = os.path.dirname(os.path.abspath(__file__))
    pdf_files = [f for f in os.listdir(base_dir) if f.endswith('.pdf')]
    loaders = [PyPDFLoader(os.path.join(base_dir, fn)) for fn in pdf_files]
    
    documents = []
    for loader in loaders:
        documents.extend(loader.load())
    
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,
        chunk_overlap=200,
        length_function=len,
        separators=["\n\n", "\n", " ", ""]
    )
    texts = text_splitter.split_documents(documents)
    
    embeddings = HuggingFaceEmbeddings()
    vector_db = Chroma.from_documents(
        texts, 
        embeddings, 
        persist_directory=PERSISTENT_DIR
    )
    vector_db.persist()
    return documents, vector_db

# System instructions for the LLM
system_prompt = """You are an expert Agro-Homeopathy doctor. When providing remedies:
1. Always specify medicine potency as 6c unless the uploaded text mentions some other value explicitly
3. Provide comprehensive diagnosis and treatment plans
4. Base recommendations on homeopathic principles
"""

api_key1 = os.getenv("api_key")

start_time = time.time()
st.set_page_config(page_title="Dr. Radha: The Agro-Homeopath", page_icon="🚀", layout="wide")

st.markdown("""
<style>
    #the-title {
        text-align: center;
    }
</style>
""", unsafe_allow_html=True)

st.title("📚 Ask Dr. Radha - World's First AI based Agro-Homeopathy Doctor")

# Add information request message
st.markdown("""
Please provide complete details about the issue, including:
- Detailed description of plant symptoms
- Current weather conditions
- Current Temperature
""")

human_image = "human.png"
robot_image = "bot.jpg"

# Set up Groq API with temperature 0.7
llm = ChatGroq(
    api_key=api_key1, 
    max_tokens=None, 
    timeout=None, 
    max_retries=2, 
    temperature=0.7,
    model="llama-3.1-70b-versatile"
)

embeddings = HuggingFaceEmbeddings()
end_time = time.time()
print(f"Setting up Groq LLM & Embeddings took {end_time - start_time:.4f} seconds")

# Initialize session state
if "documents" not in st.session_state:
    st.session_state["documents"] = None
if "vector_db" not in st.session_state:  
    st.session_state["vector_db"] = None
if "query" not in st.session_state:
    st.session_state["query"] = ""
    
start_time = time.time()
if st.session_state["documents"] is None or st.session_state["vector_db"] is None:
    with st.spinner("Loading data..."):
        documents, vector_db = initialize_vector_db()
        st.session_state["documents"] = documents  
        st.session_state["vector_db"] = vector_db
else:
    documents = st.session_state["documents"]
    vector_db = st.session_state["vector_db"]

end_time = time.time()
print(f"Loading and processing PDFs & vector database took {end_time - start_time:.4f} seconds")

start_time = time.time()
retriever = vector_db.as_retriever()

prompt_template = """You are an expert Agro-Homeopathy doctor. Analyze the following context and question to provide a clear, structured response.

Context: {context}

Question: {question}

Provide your response in the following format:
Diagnosis: Analyze the described plant condition
Treatment: Recommend specific homeopathic medicine(s) with exact potency and repetition frequency. Do not suggest more than 5 medicines for any single problem. 
Instructions for Use:
Small Plots or Gardens: Make sure your dispensing equipment is not contaminated with
other chemicals or fertilisers as these may antidote the energetic effects of the treatment—
rinse well with hot water before use if necessary. Add one pill to each 200 ml of water, shake
vigorously, and then spray or water your plants. Avoid using other chemicals or fertilisers for
10 days following treatment so that the energetic effects of the treatment are not antidoted.
(One vial of 100 pills makes 20 litres. Plants remain insect or disease free for up to 3 months
following one treatment.)
Large Plots or Farms: Add the remedy to water and apply with the dispensing device of
your choice: watering can, backpack sprayer, boomspray, reticulation systems (add to tanks
or pumps). Make sure your dispensing equipment is not contaminated with other chemicals
or fertilisers as these may antidote the energetic effects of the treatment—rinse with hot
water or steam clean before use if necessary. Avoid using other chemicals or fertilisers for
10 days following treatment.
Dosage rates are approximate and may vary according to different circumstances and
experiences. Suggested doses are:
A: 10-50 pills or 10ml/10 litre on small areas
B: 500 pills or 125ml/500l per hectare
C: 1000 pills or 250ml/500l per hectare
D: 2500 pills or 500ml/500l per hectare
Add pills or liquid to your water and mix (with a stick if necessary for large containers).

Recommendations: Provide couple of key pertinent recommendations based on the query

Remember to maintain a professional, clear tone and ensure all medicine recommendations include specific potency.

Answer:"""

# Create the QA chain with correct variables
qa = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=retriever,
    chain_type_kwargs={
        "prompt": PromptTemplate(
            template=prompt_template,
            input_variables=["context", "question"]
        )
    }
)

# Create a separate LLMChain for fallback
fallback_template = """As an expert Agro-Homeopathy doctor, provide a structured response to the following question:

Question: {question}

Format your response as follows:
Diagnosis: Analyze the described plant condition
Treatment: Recommend specific homeopathic medicine(s) with exact potency and repetition frequency. Do not suggest more than 5 medicines for any single problem.
Instructions for Use:
Small Plots or Gardens: Make sure your dispensing equipment is not contaminated with
other chemicals or fertilisers as these may antidote the energetic effects of the treatment—
rinse well with hot water before use if necessary. Add one pill to each 200 ml of water, shake
vigorously, and then spray or water your plants. Avoid using other chemicals or fertilisers for
10 days following treatment so that the energetic effects of the treatment are not antidoted.
(One vial of 100 pills makes 20 litres. Plants remain insect or disease free for up to 3 months
following one treatment.)
Large Plots or Farms: Add the remedy to water and apply with the dispensing device of
your choice: watering can, backpack sprayer, boomspray, reticulation systems (add to tanks
or pumps). Make sure your dispensing equipment is not contaminated with other chemicals
or fertilisers as these may antidote the energetic effects of the treatment—rinse with hot
water or steam clean before use if necessary. Avoid using other chemicals or fertilisers for
10 days following treatment.
Dosage rates are approximate and may vary according to different circumstances and
experiences. Suggested doses are:
A: 10-50 pills or 10ml/10 litre on small areas
B: 500 pills or 125ml/500l per hectare
C: 1000 pills or 250ml/500l per hectare
D: 2500 pills or 500ml/500l per hectare
Add pills or liquid to your water and mix (with a stick if necessary for large containers).

Recommendations: Provide couple of key pertinent recommendations based on the query

Maintain a professional tone and ensure all medicine recommendations include specific potency.

Answer:"""

fallback_prompt = PromptTemplate(template=fallback_template, input_variables=["question"])
fallback_chain = LLMChain(llm=llm, prompt=fallback_prompt)

chat_container = st.container()

with st.form(key='query_form'):
    query = st.text_input("Ask your question:", value="")
    submit_button = st.form_submit_button(label='Submit')

end_time = time.time()
print(f"Setting up retrieval chain took {end_time - start_time:.4f} seconds")
start_time = time.time()

if submit_button and query:
    with st.spinner("Generating response..."):
        result = qa({"query": query})  
        if result['result'].strip() == "":
            # If no result from PDF, use fallback chain
            fallback_result = fallback_chain.run(query)
            response = fallback_result
        else:
            response = result['result']

    col1, col2 = st.columns([1, 10])
    with col1:
        st.image(human_image, width=80)
    with col2:
        st.markdown(f"{query}")
    col1, col2 = st.columns([1, 10])
    with col1:
        st.image(robot_image, width=80)
    with col2:
        st.markdown(f"{response}")
    
    st.markdown("---")
    
    st.session_state["query"] = ""

end_time = time.time()
print(f"Actual query took {end_time - start_time:.4f} seconds")