Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import streamlit as st
|
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
| 9 |
from langchain.chains import ConversationChain
|
| 10 |
from langchain.llms import OpenAI
|
|
|
|
| 11 |
from langchain.vectorstores import Chroma
|
| 12 |
from langchain.document_loaders import TextLoader
|
| 13 |
from langchain.embeddings import OpenAIEmbeddings
|
|
@@ -20,6 +21,14 @@ st.set_page_config(
|
|
| 20 |
layout="wide",
|
| 21 |
initial_sidebar_state="expanded",
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Session State Initialization
|
| 25 |
if "detected_label" not in st.session_state:
|
|
@@ -90,11 +99,15 @@ def initialize_llm():
|
|
| 90 |
documents = loader.load()
|
| 91 |
|
| 92 |
# Create embeddings and vector store
|
| 93 |
-
embeddings = OpenAIEmbeddings(api_key=
|
| 94 |
vectorstore = Chroma.from_documents(documents, embeddings)
|
| 95 |
|
| 96 |
-
# Initialize
|
| 97 |
-
llm =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
memory = ConversationBufferMemory(return_messages=True)
|
| 99 |
conversation = ConversationChain(llm=llm, memory=memory, verbose=False)
|
| 100 |
return conversation
|
|
@@ -165,7 +178,7 @@ if uploaded_file:
|
|
| 165 |
st.session_state.show_diagnosis = True
|
| 166 |
|
| 167 |
if st.session_state.show_diagnosis:
|
| 168 |
-
query = f"Provide details about {st.session_state.detected_label}. Format: Detected Disease, Causes, Treatment, Precautions
|
| 169 |
with st.spinner("Fetching diagnosis..."):
|
| 170 |
diagnosis = st.session_state.conversation.predict(input=query)
|
| 171 |
st.subheader(f"Diagnosis for: {st.session_state.detected_label}")
|
|
@@ -176,19 +189,19 @@ if uploaded_file:
|
|
| 176 |
col1, col2, col3, col4 = st.columns(4)
|
| 177 |
|
| 178 |
with col1:
|
| 179 |
-
if st.button("
|
| 180 |
-
with st.spinner("Fetching
|
| 181 |
pesticides_info = st.session_state.conversation.predict(
|
| 182 |
-
input=f"
|
| 183 |
)
|
| 184 |
-
st.subheader("
|
| 185 |
st.markdown(pesticides_info)
|
| 186 |
|
| 187 |
with col2:
|
| 188 |
if st.button("Detailed Causes and Effects"):
|
| 189 |
with st.spinner("Fetching detailed causes and effects..."):
|
| 190 |
detailed_info = st.session_state.conversation.predict(
|
| 191 |
-
input=f"
|
| 192 |
)
|
| 193 |
st.subheader("Detailed Causes and Effects")
|
| 194 |
st.markdown(detailed_info)
|
|
@@ -197,7 +210,7 @@ if uploaded_file:
|
|
| 197 |
if st.button("Prevention Methods"):
|
| 198 |
with st.spinner("Fetching prevention methods..."):
|
| 199 |
prevention_info = st.session_state.conversation.predict(
|
| 200 |
-
input=f"
|
| 201 |
)
|
| 202 |
st.subheader("Prevention Methods")
|
| 203 |
st.markdown(prevention_info)
|
|
@@ -206,7 +219,7 @@ if uploaded_file:
|
|
| 206 |
if st.button("Treatment Options"):
|
| 207 |
with st.spinner("Fetching treatment options..."):
|
| 208 |
treatment_info = st.session_state.conversation.predict(
|
| 209 |
-
input=f"
|
| 210 |
)
|
| 211 |
st.subheader("Treatment Options")
|
| 212 |
st.markdown(treatment_info)
|
|
|
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
| 9 |
from langchain.chains import ConversationChain
|
| 10 |
from langchain.llms import OpenAI
|
| 11 |
+
from langchain.chat_models import ChatOpenAI
|
| 12 |
from langchain.vectorstores import Chroma
|
| 13 |
from langchain.document_loaders import TextLoader
|
| 14 |
from langchain.embeddings import OpenAIEmbeddings
|
|
|
|
| 21 |
layout="wide",
|
| 22 |
initial_sidebar_state="expanded",
|
| 23 |
)
|
| 24 |
+
# Function to get content of Data.txt
|
| 25 |
+
def get_data_txt_content():
|
| 26 |
+
file_path = "Data.txt"
|
| 27 |
+
if os.path.exists(file_path):
|
| 28 |
+
with open(file_path, "r") as file:
|
| 29 |
+
return file.read()
|
| 30 |
+
else:
|
| 31 |
+
return "Data.txt content is unavailable."
|
| 32 |
|
| 33 |
# Session State Initialization
|
| 34 |
if "detected_label" not in st.session_state:
|
|
|
|
| 99 |
documents = loader.load()
|
| 100 |
|
| 101 |
# Create embeddings and vector store
|
| 102 |
+
embeddings = OpenAIEmbeddings(api_key="sk-0iMBF0ndiFS-qsKzwMyY7E4q5U6-wM2Si3pXB3p-GNT3BlbkFJDu_xmtFGDTSpwg34CUjQC6_DTyEW-SQTH1pI08DtYA")
|
| 103 |
vectorstore = Chroma.from_documents(documents, embeddings)
|
| 104 |
|
| 105 |
+
# Initialize ChatOpenAI and memory
|
| 106 |
+
llm = ChatOpenAI(
|
| 107 |
+
model="gpt-4", # Use gpt-4 or gpt-4o depending on your needs
|
| 108 |
+
temperature=0.7,
|
| 109 |
+
api_key="sk-0iMBF0ndiFS-qsKzwMyY7E4q5U6-wM2Si3pXB3p-GNT3BlbkFJDu_xmtFGDTSpwg34CUjQC6_DTyEW-SQTH1pI08DtYA"
|
| 110 |
+
)
|
| 111 |
memory = ConversationBufferMemory(return_messages=True)
|
| 112 |
conversation = ConversationChain(llm=llm, memory=memory, verbose=False)
|
| 113 |
return conversation
|
|
|
|
| 178 |
st.session_state.show_diagnosis = True
|
| 179 |
|
| 180 |
if st.session_state.show_diagnosis:
|
| 181 |
+
query = f"Provide details about {st.session_state.detected_label}. Format: Detected Disease, Causes, Treatment, Precautions"
|
| 182 |
with st.spinner("Fetching diagnosis..."):
|
| 183 |
diagnosis = st.session_state.conversation.predict(input=query)
|
| 184 |
st.subheader(f"Diagnosis for: {st.session_state.detected_label}")
|
|
|
|
| 189 |
col1, col2, col3, col4 = st.columns(4)
|
| 190 |
|
| 191 |
with col1:
|
| 192 |
+
if st.button("Usable Pesticides"):
|
| 193 |
+
with st.spinner("Fetching list of pesticides..."):
|
| 194 |
pesticides_info = st.session_state.conversation.predict(
|
| 195 |
+
input=f"How do the pesticides for this {st.session_state.detected_label} disease vary based on environmental conditions and regional agricultural practices?"
|
| 196 |
)
|
| 197 |
+
st.subheader("List of Pesticides")
|
| 198 |
st.markdown(pesticides_info)
|
| 199 |
|
| 200 |
with col2:
|
| 201 |
if st.button("Detailed Causes and Effects"):
|
| 202 |
with st.spinner("Fetching detailed causes and effects..."):
|
| 203 |
detailed_info = st.session_state.conversation.predict(
|
| 204 |
+
input=f"What are the long-term ecological and economic impacts of this {st.session_state.detected_label} disease on farming communities, and how can they be mitigated?"
|
| 205 |
)
|
| 206 |
st.subheader("Detailed Causes and Effects")
|
| 207 |
st.markdown(detailed_info)
|
|
|
|
| 210 |
if st.button("Prevention Methods"):
|
| 211 |
with st.spinner("Fetching prevention methods..."):
|
| 212 |
prevention_info = st.session_state.conversation.predict(
|
| 213 |
+
input=f"What innovative strategies, beyond traditional methods, can be adopted to prevent the recurrence of this {st.session_state.detected_label} disease in staple crops?"
|
| 214 |
)
|
| 215 |
st.subheader("Prevention Methods")
|
| 216 |
st.markdown(prevention_info)
|
|
|
|
| 219 |
if st.button("Treatment Options"):
|
| 220 |
with st.spinner("Fetching treatment options..."):
|
| 221 |
treatment_info = st.session_state.conversation.predict(
|
| 222 |
+
input=f"How can the integration of biological, chemical, and technological treatments improve the sustainability and effectiveness of managing this {st.session_state.detected_label} disease?"
|
| 223 |
)
|
| 224 |
st.subheader("Treatment Options")
|
| 225 |
st.markdown(treatment_info)
|