File size: 9,923 Bytes
a447435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa840a1
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
import gradio as gr
import os
import datetime
import kkms_kssw as kkms_kssw
import utils.constants as constants_utils


# Create and launch Gradio Web UI
class DomState:
	def __init__(self):
		self.relevant_paragraphs = ''
		self.answer = ''
		self.summary = ''
		self.mandi_price = ''
		self.mandi_from_date = (datetime.datetime.now() - datetime.timedelta(days=2)).strftime('%Y-%m-%d')
		self.mandi_to_date = datetime.datetime.now().strftime('%Y-%m-%d')
		self.weather_info = ''
		self.weather_forecast = ''
		self.weather_forecast_summary = ''
		self.indic_lang_answer = ''


		# Initialize index (vector store)
		self.kkms_kssw_obj = kkms_kssw.KKMS_KSSW()
		self.kkms_kssw_obj.initialize_index(constants_utils.INDEX_FILENAME, index_type='GPTSimpleVectorIndex')
		
	
	def click_handler_for_get_relevant_paragraphs(self,
									 question,
									 mode='default',
									 response_mode='default',
									 similarity_top_k=2,
		):
		self.relevant_paragraphs = self.kkms_kssw_obj.query(question,
							mode=mode,
							response_mode=response_mode,
							similarity_top_k=similarity_top_k,
							# required_keywords=required_keywords_list,
							# exclude_keywords=exclude_keywords_list,
		)
		return self.relevant_paragraphs
	
	
	def click_handler_for_summary(self, answer):
		self.sumamry = self.kkms_kssw_obj.langchain_utils_obj.get_textual_summary(answer)
		return self.sumamry
	
	
	def click_handler_for_get_answer(self,
									 relevant_paragraphs,
									 question
		):
		self.answer = self.kkms_kssw_obj.langchain_utils_obj.get_answer_from_para(relevant_paragraphs, question)
		return self.answer
	
	
	def click_handler_for_mandi_price(self, 
									  state_name,
									  apmc_name,
									  commodity_name,
									  from_date,
									  to_date
									 ):
		if state_name and apmc_name and commodity_name and from_date and to_date:
			self.mandi_price = self.kkms_kssw_obj.mandi_utils_obj.get_mandi_price(state_name, apmc_name, commodity_name, from_date, to_date)
		return self.mandi_price
	
	
	def click_handler_for_get_weather(self, city):
		time, info, temperature = self.kkms_kssw_obj.weather_utils_obj.get_weather(city)
		self.weather_info = f'Weather in {city.capitalize()} on {time} is {temperature} with {info}.'
		return self.weather_info
	
	
	def click_handler_for_get_weather_forecast(self, state, district):
		self.weather_forecast = self.kkms_kssw_obj.weather_utils_obj.get_weather_forecast(state, district)
		return self.weather_forecast
	
	
	def click_handler_for_weather_forecast_summary(self, weather_forecast):
		self.weather_forecast_summary = self.kkms_kssw_obj.langchain_utils_obj.get_weather_forecast_summary(weather_forecast)
		return self.weather_forecast_summary
	
	
	def click_handler_for_get_indic_answer(self, eng_ans, language='Hindi'):
		self.indic_lang_answer = self.kkms_kssw_obj.translator_utils_obj.get_indic_google_translate(eng_ans, language)
		return self.indic_lang_answer
	
	
	def select_widget(self, choice):
		if choice == "General":
			return [
				gr.update(visible=True),
				gr.update(visible=False),
				gr.update(visible=False)
			]
		
		elif choice == "Mandi Price":
			return [
				gr.update(visible=False),
				gr.update(visible=True),
				gr.update(visible=False)
			]
		
		elif choice == "Weather":
			return [
				gr.update(visible=False),
				gr.update(visible=False),
				gr.update(visible=True)
			]
		
		else:
			return gr.update(visible=False)


with gr.Blocks(title='KKMS-KSSW Demo') as demo:
	dom = DomState()
	
	radio = gr.Radio(
		["General", "Mandi Price", "Weather"], label="Query related to"
	)
	
	########################### Widget for Govt. Policy #################################################
	with gr.Row(visible=True) as rowGeneral:
		with gr.Column(scale=1, min_width=600):
			with gr.Tab(label='Relevant paragraphs'):
				question = gr.Textbox(label="Enter your question", placeholder='Type the question here')
				# Get the Relevant paragraphs for the question asked
				relevant_paragraphs = gr.Textbox(label="Relevant paragraphs are:", value=dom.relevant_paragraphs, interactive=False)
				b_relevant_paragraphs = gr.Button("Get Relevant paragraphs").style(size='sm')
				b_relevant_paragraphs.click(fn=dom.click_handler_for_get_relevant_paragraphs, inputs=question, outputs=[relevant_paragraphs])

		with gr.Column(scale=1, min_width=600):
			with gr.Tab(label='Extractive Summary'):
				# Get the extractive text summary from the retrieved Relevant paragraphs
				summary = gr.Textbox(label="Extractive Summary is:", value=dom.summary, interactive=False)
				b_summary = gr.Button("Extract Summary").style(size='sm')
				b_summary.click(fn=dom.click_handler_for_summary, inputs=relevant_paragraphs, outputs=[summary])
		
		# Get the exact answer for the question asked from the retrieved Relevant paragraphs
		with gr.Row():
			with gr.Column(scale=1, min_width=600):
				with gr.Tab(label='Answer'):
					answer = gr.Textbox(label="Answer is:", value=dom.answer, interactive=False)
					b_answer = gr.Button("Get Answer").style(size='sm')
					b_answer.click(fn=dom.click_handler_for_get_answer, inputs=[relevant_paragraphs, question], outputs=[answer])
		
		# Covert the answer to Indian language
		with gr.Row():
			with gr.Column(scale=1, min_width=600):
				with gr.Tab(label='Answer in selected language'):
					# Select the language
					language = gr.Dropdown(
						['English', 'Hindi', 'Gujarati', 'Marathi', 'Kannada', 'Bengali', 'Panjabi', 'Telugu', 'Tamil', 'Malayalam'],
						label="Select language")
					indic_lang_answer = gr.Textbox(label="Answer in the selected language is:", value=dom.indic_lang_answer, interactive=False)
					b_indic_lang_answer = gr.Button("Get answer in selected language").style(size='sm')
					b_indic_lang_answer.click(fn=dom.click_handler_for_get_indic_answer, inputs=[answer, language], outputs=[indic_lang_answer])
	
	
	########################## Widget for Mandi Price ###################################################
	with gr.Row(visible=False) as rowMandiPrice:
		with gr.Column(scale=1, min_width=600):
			# Select State
			state_name = gr.Dropdown(['ANDAMAN AND NICOBAR ISLANDS', 'ANDHRA PRADESH', 'ASSAM', 'BIHAR', 'CHANDIGARH', 'CHHATTISGARH', 'GOA', 'GUJARAT', 'HARYANA', 'HIMACHAL PRADESH', 'JAMMU AND KASHMIR', 'JHARKHAND', 'KARNATAKA', 'KERALA', 'MADHYA PRADESH', 'MAHARASHTRA', 'NAGALAND', 'ODISHA', 'PUDUCHERRY', 'PUNJAB', 'RAJASTHAN', 'TAMIL NADU', 'TELANGANA', 'TRIPURA', 'UTTAR PRADESH', 'UTTARAKHAND', 'WEST BENGAL'], label="Select state")
			
			# APMC name
			apmc_name = gr.Textbox(label="Enter APMC name", placeholder='Type the APMC name here')
			
			# APMC name
			commodity_name = gr.Textbox(label="Enter Commodity name", placeholder='Type the Commodity name here')
					  
			# From/To date in yyyy-mm-dd format
			from_date = gr.Textbox(label="From date?", value=dom.mandi_from_date, placeholder='Please enter the From date here in yyyy-mm-dd format')
			to_date = gr.Textbox(label="To date?", value=dom.mandi_to_date, placeholder='Please enter the To date here in yyyy-mm-dd format')
		
		with gr.Column(scale=1, min_width=600):
			mandi_price = gr.Textbox(label=f"Mandi Price is:", value=dom.mandi_price, interactive=False)
			b_summary = gr.Button("Get Mandi Price").style(size='sm')
			b_summary.click(fn=dom.click_handler_for_mandi_price, inputs=[state_name, apmc_name, commodity_name, from_date, to_date], outputs=[mandi_price])
	
	
	########################## Widget for Weather Info ###################################################
	with gr.Row(visible=False) as rowWeather:
		with gr.Column(scale=1, min_width=600):
			with gr.Tab(label='Weather Info'):
				city = gr.Textbox(label="Enter city name", placeholder='Type the city name here')
				weather = gr.Textbox(label=f"Current weather is:", value=dom.weather_info, interactive=False)
				b_weather = gr.Button("Get weather info").style(size='sm')
				b_weather.click(fn=dom.click_handler_for_get_weather, inputs=city, outputs=[weather])
		
		########### Weather Forecast ###########
		with gr.Column(scale=1, min_width=600):
			with gr.Tab(label='Weather Forecast for next 5 days'):
				# Select the State
				state = gr.Dropdown(
					['Andaman-Nicobar', 'Andhra-Pradesh', 'Arunachal-Pradesh', 'Assam', 'Bihar', 'Chandigarh', 'Chhattisgarh', 'Dadra-and-Nagar-Haveli', 'Daman-and-Diu', 'Delhi', 'Goa', 'Gujarat', 'Haryana', 'Himachal-Pradesh', 'Jammu-Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Lakshadweep', 'Madhya-Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Pondicherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamilnadu', 'Telangana', 'Tripura', 'Uttar-Pradesh', 'Uttarakhand', 'West-Bengal'],
					label="Select state"
				)

				# # Select district
				# district = gr.Dropdown(
				#     weather_utils.STATES.get(state, {}),
				#     label="Select district"
				# )

				district = gr.Textbox(label="Enter district name", placeholder='Type the district name here')
				district_weather = gr.Textbox(label=f"Weather forecast is:", value=dom.weather_forecast, interactive=False)
				bd_weather = gr.Button("Get weather forecast").style(size='sm')
				bd_weather.click(fn=dom.click_handler_for_get_weather_forecast, inputs=[state, district], outputs=[district_weather])
				
		
		with gr.Column(scale=1, min_width=600):
			with gr.Tab(label='Weather Forecast Summary'):
				# Get the summary of the weather forecast
				weather_forecast_summary = gr.Textbox(label="Weather Forecast Summary is:", value=dom.weather_forecast_summary, interactive=False)
				b_weather_forecast_summary = gr.Button("Get Weather Forecast Summary").style(size='sm')
				b_weather_forecast_summary.click(fn=dom.click_handler_for_weather_forecast_summary, inputs=district_weather, outputs=[weather_forecast_summary])

	
	radio.change(fn=dom.select_widget, inputs=radio, outputs=[rowGeneral, rowMandiPrice, rowWeather])


demo.launch(share=False)