Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import datetime | |
import src.constants as constants_utils | |
import src.kkms_kssw as kkms_kssw | |
import warnings | |
warnings.filterwarnings('ignore') | |
class DomState: | |
def __init__( | |
self, | |
index_type, | |
load_from_existing_index_file | |
): | |
self.index_type = index_type | |
self.load_from_existing_index_file = load_from_existing_index_file | |
self.relevant_paragraphs = '' | |
self.sources_relevant_paragraphs = '' | |
self.answer = '' | |
self.summary = '' | |
self.mandi_price = '' | |
self.mandi_from_date = (datetime.datetime.now() - datetime.timedelta(days=5)).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_translation = '' | |
# Initialize index (vector store) - This will create a new index from scratch if load_from_existing_index_file == False | |
self.kkms_kssw_obj = kkms_kssw.KKMS_KSSW() | |
self.kkms_kssw_obj.load_create_index() | |
def click_handler_for_get_relevant_paragraphs( | |
self, | |
question, | |
question_category='general' | |
): | |
self.relevant_paragraphs = self.kkms_kssw_obj.query( | |
question=question, | |
question_category=question_category | |
) | |
if self.index_type in ['FAISS', 'Chroma']: | |
self.sources_relevant_paragraphs = [doc.metadata for doc in self.relevant_paragraphs] | |
self.relevant_paragraphs = [doc.page_content.replace('\n', '').replace('\t', ' ') for doc in self.relevant_paragraphs] | |
return self.relevant_paragraphs | |
def click_handler_for_relevant_paragraphs_source( | |
self, | |
relevant_paragraphs | |
): | |
return self.sources_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_load_files_urls( | |
self, | |
doc_type, | |
files_or_urls, | |
index_category='general' | |
): | |
self.kkms_kssw_obj.upload_data( | |
doc_type=constants_utils.DATA_SOURCES[doc_type], | |
files_or_urls=files_or_urls, | |
index_category=index_category | |
) | |
def click_handler_for_get_indic_translation( | |
self, | |
eng_ans, | |
language='Hindi' | |
): | |
self.indic_translation = self.kkms_kssw_obj.translator_utils_obj.get_indic_google_translate(eng_ans, language) | |
return self.indic_translation | |
def _upload_file(self, files): | |
file_paths = [file.name for file in files] | |
return file_paths | |
def select_widget( | |
self, | |
choice | |
): | |
if choice == "General": | |
return [ | |
gr.update(visible=True), | |
gr.update(visible=False), | |
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), | |
gr.update(visible=False), | |
] | |
elif choice == "Weather": | |
return [ | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=True), | |
gr.update(visible=False), | |
] | |
elif choice == "Load Custom Data": | |
return [ | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=True) | |
] | |
else: | |
return gr.update(visible=False) | |
def select_files_urls( | |
self, | |
choice | |
): | |
if choice == "PDF": | |
return [ | |
gr.update(visible=True), | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=False), | |
] | |
elif choice == "Online PDF": | |
return [ | |
gr.update(visible=False), | |
gr.update(visible=True), | |
gr.update(visible=False), | |
gr.update(visible=False), | |
] | |
elif choice == "Text File": | |
return [ | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=True), | |
gr.update(visible=False), | |
] | |
elif choice == "URLs": | |
return [ | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=True), | |
] | |
else: | |
return [ | |
gr.update(visible=True), | |
gr.update(visible=False), | |
gr.update(visible=False), | |
gr.update(visible=False), | |
] | |
with gr.Blocks(title='KKMS-KSSW Demo') as demo: | |
dom = DomState( | |
index_type=constants_utils.INDEX_TYPE, | |
load_from_existing_index_file=constants_utils.LOAD_FROM_EXISTING_INDEX_STORE | |
) | |
widgets = gr.Radio( | |
[ | |
"General", | |
"Mandi Price", | |
"Weather", | |
"Load Custom Data" | |
], | |
label="Query related to", | |
value="General" | |
) | |
############################################################################# | |
# 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): | |
with gr.Tab(label='Sources of relevant paragraphs'): | |
# Get the Sources of relevant paragraphs | |
sources_relevant_paragraphs = gr.Textbox(label="Sources of relevant paragraphs are:", interactive=False) | |
b_sources_relevant_paragraphs = gr.Button("Get Sources of relevant paragraphs").style(size='sm') | |
b_sources_relevant_paragraphs.click(fn=dom.click_handler_for_relevant_paragraphs_source, inputs=relevant_paragraphs, outputs=[sources_relevant_paragraphs]) | |
# NOTE: Don't show extractive summary unless requested by FTA. | |
# 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.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.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_translation, 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_translation, 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(constants_utils.MANDI_PRICE_STATES, 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: | |
########### 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( | |
constants_utils.WEATHER_FORECAST_STATES, | |
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]) | |
# Covert the weather forcast summary in Indian language | |
with gr.Column(scale=1, min_width=600): | |
with gr.Tab(label='Weather Forecast Summary in selected language'): | |
# Select the language | |
language = gr.Dropdown( | |
['English', 'Hindi', 'Gujarati', 'Marathi', 'Kannada', 'Bengali', 'Panjabi', 'Telugu', 'Tamil', 'Malayalam'], | |
label="Select language") | |
indic_weather_forecast_summary = gr.Textbox(label="Weather Forecast Summary in the selected language is:", value=dom.indic_translation, interactive=False) | |
b_indic_weather_forecast_summary = gr.Button("Get answer in selected language").style(size='sm') | |
b_indic_weather_forecast_summary.click(fn=dom.click_handler_for_get_indic_translation, inputs=[weather_forecast_summary, language], outputs=[indic_weather_forecast_summary]) | |
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]) | |
############################################################################# | |
# Widget to load and process from the custom data source | |
with gr.Row(visible=False) as rowLoadCustomData: | |
with gr.Column(scale=1, min_width=600): | |
with gr.Tab(label='Load Custom Data'): | |
doc_type = gr.Radio( | |
list(constants_utils.DATA_SOURCES.keys()), | |
label="Select data source (Supports uploading multiple Files/URLs)", | |
value="PDF" | |
) | |
with gr.Row(visible=True) as rowUploadPdf: | |
with gr.Column(scale=1, min_width=600): | |
file_output = gr.File() | |
upload_button = gr.UploadButton( | |
"Click to Upload PDF Files", | |
file_types=['.pdf'], | |
file_count="multiple" | |
) | |
upload_button.upload(dom._upload_file, upload_button, file_output) | |
b_files = gr.Button("Load PDF Files").style(size='sm') | |
b_files.click( | |
fn=dom.click_handler_for_load_files_urls, | |
inputs=[doc_type, upload_button] | |
) | |
with gr.Row(visible=False) as rowUploadOnlinePdf: | |
with gr.Column(scale=1, min_width=600): | |
urls = gr.Textbox(label="Enter URLs for Online PDF (Supports uploading from multiple URLs. Enter the URLs in comma (,) separated format)", placeholder='Type the URLs here') | |
b_urls = gr.Button("Load Online PDFs").style(size='sm') | |
b_urls.click(fn=dom.click_handler_for_load_files_urls, inputs=[doc_type, urls]) | |
with gr.Row(visible=False) as rowUploadTextFile: | |
with gr.Column(scale=1, min_width=600): | |
file_output = gr.File() | |
upload_button = gr.UploadButton( | |
"Click to Upload Text Files", | |
file_types=['.txt'], | |
file_count="multiple" | |
) | |
upload_button.upload(dom._upload_file, upload_button, file_output) | |
b_files = gr.Button("Load Text Files").style(size='sm') | |
b_files.click( | |
fn=dom.click_handler_for_load_files_urls, | |
inputs=[doc_type, file_output] | |
) | |
with gr.Row(visible=False) as rowUploadUrls: | |
with gr.Column(scale=1, min_width=600): | |
urls = gr.Textbox(label="Enter URLs (Supports uploading from multiple URLs. Enter the URLs in comma (,) separated format)", placeholder='Type the URLs here') | |
b_urls = gr.Button("Load URLs").style(size='sm') | |
b_urls.click(fn=dom.click_handler_for_load_files_urls, inputs=[doc_type, urls]) | |
doc_type.change( | |
fn=dom.select_files_urls, | |
inputs=doc_type, | |
outputs=[ | |
rowUploadPdf, | |
rowUploadOnlinePdf, | |
rowUploadTextFile, | |
rowUploadUrls, | |
], | |
) | |
widgets.change( | |
fn=dom.select_widget, | |
inputs=widgets, | |
outputs=[ | |
rowGeneral, | |
rowMandiPrice, | |
rowWeather, | |
rowLoadCustomData, | |
], | |
) | |
demo.launch(share=False) | |