Spaces:
Runtime error
Runtime error
Chintan Donda
commited on
Commit
·
2cd07d5
1
Parent(s):
edddd52
Added widget AgGPT for General queries, Creating index for Crops, Fruits, Vegetables
Browse files- app.py +37 -5
- src/constants.py +3 -2
- src/langchain_utils.py +47 -0
app.py
CHANGED
@@ -5,6 +5,8 @@ import datetime
|
|
5 |
import src.constants as constants_utils
|
6 |
import src.kkms_kssw as kkms_kssw
|
7 |
|
|
|
|
|
8 |
import warnings
|
9 |
warnings.filterwarnings('ignore')
|
10 |
|
@@ -142,16 +144,27 @@ class DomState:
|
|
142 |
self,
|
143 |
choice
|
144 |
):
|
145 |
-
if choice == "
|
146 |
return [
|
147 |
gr.update(visible=True),
|
148 |
gr.update(visible=False),
|
149 |
gr.update(visible=False),
|
150 |
gr.update(visible=False),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
]
|
152 |
|
153 |
elif choice == "Mandi Price":
|
154 |
return [
|
|
|
155 |
gr.update(visible=False),
|
156 |
gr.update(visible=True),
|
157 |
gr.update(visible=False),
|
@@ -160,6 +173,7 @@ class DomState:
|
|
160 |
|
161 |
elif choice == "Weather":
|
162 |
return [
|
|
|
163 |
gr.update(visible=False),
|
164 |
gr.update(visible=False),
|
165 |
gr.update(visible=True),
|
@@ -171,6 +185,7 @@ class DomState:
|
|
171 |
gr.update(visible=False),
|
172 |
gr.update(visible=False),
|
173 |
gr.update(visible=False),
|
|
|
174 |
gr.update(visible=True)
|
175 |
]
|
176 |
|
@@ -232,18 +247,19 @@ with gr.Blocks(title='KKMS-KSSW Demo') as demo:
|
|
232 |
|
233 |
widgets = gr.Radio(
|
234 |
[
|
235 |
-
"
|
|
|
236 |
"Mandi Price",
|
237 |
"Weather",
|
238 |
"Load Custom Data"
|
239 |
],
|
240 |
label="Query related to",
|
241 |
-
value="
|
242 |
)
|
243 |
|
244 |
#############################################################################
|
245 |
-
# Widget for
|
246 |
-
with gr.Row(visible=True) as
|
247 |
with gr.Column(scale=1, min_width=600):
|
248 |
with gr.Tab(label='Relevant paragraphs'):
|
249 |
question = gr.Textbox(label="Enter your question", placeholder='Type the question here')
|
@@ -290,6 +306,21 @@ with gr.Blocks(title='KKMS-KSSW Demo') as demo:
|
|
290 |
b_indic_lang_answer.click(fn=dom.click_handler_for_get_indic_translation, inputs=[answer, language], outputs=[indic_lang_answer])
|
291 |
|
292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
#############################################################################
|
294 |
# Widget for Mandi Price
|
295 |
with gr.Row(visible=False) as rowMandiPrice:
|
@@ -429,6 +460,7 @@ with gr.Blocks(title='KKMS-KSSW Demo') as demo:
|
|
429 |
fn=dom.select_widget,
|
430 |
inputs=widgets,
|
431 |
outputs=[
|
|
|
432 |
rowGeneral,
|
433 |
rowMandiPrice,
|
434 |
rowWeather,
|
|
|
5 |
import src.constants as constants_utils
|
6 |
import src.kkms_kssw as kkms_kssw
|
7 |
|
8 |
+
os.environ["CURL_CA_BUNDLE"] = ""
|
9 |
+
|
10 |
import warnings
|
11 |
warnings.filterwarnings('ignore')
|
12 |
|
|
|
144 |
self,
|
145 |
choice
|
146 |
):
|
147 |
+
if choice == "Custom Query":
|
148 |
return [
|
149 |
gr.update(visible=True),
|
150 |
gr.update(visible=False),
|
151 |
gr.update(visible=False),
|
152 |
gr.update(visible=False),
|
153 |
+
gr.update(visible=False),
|
154 |
+
]
|
155 |
+
|
156 |
+
elif choice == "General (AgGPT)":
|
157 |
+
return [
|
158 |
+
gr.update(visible=False),
|
159 |
+
gr.update(visible=True),
|
160 |
+
gr.update(visible=False),
|
161 |
+
gr.update(visible=False),
|
162 |
+
gr.update(visible=False),
|
163 |
]
|
164 |
|
165 |
elif choice == "Mandi Price":
|
166 |
return [
|
167 |
+
gr.update(visible=False),
|
168 |
gr.update(visible=False),
|
169 |
gr.update(visible=True),
|
170 |
gr.update(visible=False),
|
|
|
173 |
|
174 |
elif choice == "Weather":
|
175 |
return [
|
176 |
+
gr.update(visible=False),
|
177 |
gr.update(visible=False),
|
178 |
gr.update(visible=False),
|
179 |
gr.update(visible=True),
|
|
|
185 |
gr.update(visible=False),
|
186 |
gr.update(visible=False),
|
187 |
gr.update(visible=False),
|
188 |
+
gr.update(visible=False),
|
189 |
gr.update(visible=True)
|
190 |
]
|
191 |
|
|
|
247 |
|
248 |
widgets = gr.Radio(
|
249 |
[
|
250 |
+
"Custom Query",
|
251 |
+
"General (AgGPT)",
|
252 |
"Mandi Price",
|
253 |
"Weather",
|
254 |
"Load Custom Data"
|
255 |
],
|
256 |
label="Query related to",
|
257 |
+
value="Custom Query"
|
258 |
)
|
259 |
|
260 |
#############################################################################
|
261 |
+
# Widget for Custom Queries
|
262 |
+
with gr.Row(visible=True) as rowCustomQuery:
|
263 |
with gr.Column(scale=1, min_width=600):
|
264 |
with gr.Tab(label='Relevant paragraphs'):
|
265 |
question = gr.Textbox(label="Enter your question", placeholder='Type the question here')
|
|
|
306 |
b_indic_lang_answer.click(fn=dom.click_handler_for_get_indic_translation, inputs=[answer, language], outputs=[indic_lang_answer])
|
307 |
|
308 |
|
309 |
+
#############################################################################
|
310 |
+
# Widget for General Query using AgGPT
|
311 |
+
with gr.Row(visible=False) as rowGeneral:
|
312 |
+
with gr.Column(scale=1, min_width=600):
|
313 |
+
chatbot = gr.Chatbot()
|
314 |
+
msg = gr.Textbox()
|
315 |
+
submit = gr.Button("Submit")
|
316 |
+
clear = gr.Button("Clear")
|
317 |
+
submit.click(
|
318 |
+
dom.kkms_kssw_obj.langchain_utils_obj.user, [msg, chatbot], [msg, chatbot]
|
319 |
+
).then(dom.kkms_kssw_obj.langchain_utils_obj.bot, chatbot, chatbot)
|
320 |
+
clear.click(
|
321 |
+
dom.kkms_kssw_obj.langchain_utils_obj.clear_history, None, chatbot, queue=False)
|
322 |
+
|
323 |
+
|
324 |
#############################################################################
|
325 |
# Widget for Mandi Price
|
326 |
with gr.Row(visible=False) as rowMandiPrice:
|
|
|
460 |
fn=dom.select_widget,
|
461 |
inputs=widgets,
|
462 |
outputs=[
|
463 |
+
rowCustomQuery,
|
464 |
rowGeneral,
|
465 |
rowMandiPrice,
|
466 |
rowWeather,
|
src/constants.py
CHANGED
@@ -16,12 +16,13 @@ if not os.path.exists(OUTPUT_PATH):
|
|
16 |
# Index categories (There would be an index for each category. On asking the query, App will search for the relevant docs/information only from the respective index category.)
|
17 |
INDEX_CATEGORY = [
|
18 |
'crops',
|
19 |
-
|
20 |
# 'pest_management',
|
21 |
# 'govt_policy',
|
22 |
# 'insurance',
|
23 |
# 'soil',
|
24 |
'general',
|
|
|
25 |
]
|
26 |
|
27 |
# Doctype of the master index of each index category. Master index for each index category would be stored under this key.
|
@@ -36,7 +37,7 @@ DATA_SOURCES = {
|
|
36 |
'PDF': 'pdf',
|
37 |
'Text File': 'textfile',
|
38 |
'Online PDF': 'online_pdf', # web_crawler_utils.get_ipm_packages_pdfs_urls()[:1]
|
39 |
-
'URLs': 'urls',
|
40 |
}
|
41 |
|
42 |
# LangChain related constants
|
|
|
16 |
# Index categories (There would be an index for each category. On asking the query, App will search for the relevant docs/information only from the respective index category.)
|
17 |
INDEX_CATEGORY = [
|
18 |
'crops',
|
19 |
+
'fruits',
|
20 |
# 'pest_management',
|
21 |
# 'govt_policy',
|
22 |
# 'insurance',
|
23 |
# 'soil',
|
24 |
'general',
|
25 |
+
'vegetables',
|
26 |
]
|
27 |
|
28 |
# Doctype of the master index of each index category. Master index for each index category would be stored under this key.
|
|
|
37 |
'PDF': 'pdf',
|
38 |
'Text File': 'textfile',
|
39 |
'Online PDF': 'online_pdf', # web_crawler_utils.get_ipm_packages_pdfs_urls()[:1]
|
40 |
+
# 'URLs': 'urls',
|
41 |
}
|
42 |
|
43 |
# LangChain related constants
|
src/langchain_utils.py
CHANGED
@@ -7,6 +7,7 @@ from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTex
|
|
7 |
from langchain.chains.summarize import load_summarize_chain
|
8 |
from langchain.docstore.document import Document
|
9 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
|
|
10 |
from langchain.vectorstores import Chroma
|
11 |
import chromadb
|
12 |
from langchain.chains.question_answering import load_qa_chain
|
@@ -67,6 +68,14 @@ class LANGCHAIN_UTILS:
|
|
67 |
# Initialize embeddings (we can also use other embeddings)
|
68 |
self.embeddings = OpenAIEmbeddings(openai_api_key=os.getenv('OPENAI_API_KEY'))
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
def generate_prompt_template(
|
72 |
self,
|
@@ -90,6 +99,41 @@ class LANGCHAIN_UTILS:
|
|
90 |
return prompt_template
|
91 |
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
def get_textual_summary(
|
95 |
self,
|
@@ -744,6 +788,9 @@ class LANGCHAIN_UTILS:
|
|
744 |
Creates a new index if index_category-doc_type index is not present.
|
745 |
Also updates the master index for the given index_category.
|
746 |
"""
|
|
|
|
|
|
|
747 |
logger.info(f'Upserting index for: {index_category}-{doc_type}')
|
748 |
|
749 |
if not self.index_category_doc_type_wise_index.get(index_category, None):
|
|
|
7 |
from langchain.chains.summarize import load_summarize_chain
|
8 |
from langchain.docstore.document import Document
|
9 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
10 |
+
import openai
|
11 |
from langchain.vectorstores import Chroma
|
12 |
import chromadb
|
13 |
from langchain.chains.question_answering import load_qa_chain
|
|
|
68 |
# Initialize embeddings (we can also use other embeddings)
|
69 |
self.embeddings = OpenAIEmbeddings(openai_api_key=os.getenv('OPENAI_API_KEY'))
|
70 |
|
71 |
+
# Global history for AgGPT widget
|
72 |
+
self.global_history = [
|
73 |
+
{
|
74 |
+
"role": "assistant",
|
75 |
+
"content": "Hi, I am a chatbot. I can converse in English. I can answer your questions about farming in India. Ask me anything!"
|
76 |
+
}
|
77 |
+
]
|
78 |
+
|
79 |
|
80 |
def generate_prompt_template(
|
81 |
self,
|
|
|
99 |
return prompt_template
|
100 |
|
101 |
|
102 |
+
def user(
|
103 |
+
self,
|
104 |
+
user_message,
|
105 |
+
history
|
106 |
+
):
|
107 |
+
history = history + [[user_message, None]]
|
108 |
+
self.global_history = self.global_history + [{"role": "user", "content": user_message}]
|
109 |
+
return "", history
|
110 |
+
|
111 |
+
|
112 |
+
def get_chatgpt_response(
|
113 |
+
self,
|
114 |
+
history
|
115 |
+
):
|
116 |
+
output = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=history)
|
117 |
+
history.append({"role": "assistant", "content": output.choices[0].message.content})
|
118 |
+
return output.choices[0].message.content, history
|
119 |
+
|
120 |
+
|
121 |
+
def bot(
|
122 |
+
self,
|
123 |
+
history
|
124 |
+
):
|
125 |
+
response, self.global_history = self.get_chatgpt_response(self.global_history)
|
126 |
+
history[-1][1] = response
|
127 |
+
return history
|
128 |
+
|
129 |
+
|
130 |
+
def clear_history(
|
131 |
+
self,
|
132 |
+
lang="English"
|
133 |
+
):
|
134 |
+
self.global_history = [{"role": "assistant", "content": "Hi, I am a chatbot. I can converse in {}. I can answer your questions about farming in India. Ask me anything!".format(lang)}]
|
135 |
+
return None
|
136 |
+
|
137 |
|
138 |
def get_textual_summary(
|
139 |
self,
|
|
|
788 |
Creates a new index if index_category-doc_type index is not present.
|
789 |
Also updates the master index for the given index_category.
|
790 |
"""
|
791 |
+
if not self.index:
|
792 |
+
return
|
793 |
+
|
794 |
logger.info(f'Upserting index for: {index_category}-{doc_type}')
|
795 |
|
796 |
if not self.index_category_doc_type_wise_index.get(index_category, None):
|