Almaatla/Standard_Intelligence_Dev
0
1import subprocess2 3subprocess.run(["pip", "uninstall", "pdfminer"])4subprocess.run(["pip", "install", "pdfminer.six==20231228"])5 6 7 8import gradio as gr9from scrape_3gpp import *10from excel_chat import *11from split_files_to_excel import *12from classification import *13from chart_generation import *14from charts_advanced import *15from users_management import *16from code_df_custom import *17 18 19global value20value = set()21 22def list_attributes_and_values():23 global value24 attr = 'temp_files'25 new_value = getattr(fi_config, attr)26 print(f"value: {value}\nnew value: {new_value}")27 tmp = list(new_value - value)[0]28 value = set(new_value)29 html_script = f"""30 <!DOCTYPE html>31 <html lang="en">32 <head>33 <meta charset="UTF-8">34 <meta name="viewport" content="width=device-width, initial-scale=1.0">35 <meta http-equiv="refresh" content="0; url=https://organizedprogrammers-standard-intelligence-dev.hf.space/file={tmp}">36 <title>Redirecting to Google</title>37 </head>38 <body>39 <p>If you are not redirected automatically, please <a href="https://organizedprogrammers-standard-intelligence-dev.hf.space/file={tmp}">click here</a>.</p>40 </body>41 </html>42 """43 return html_script44 45def retrieve_checkpoint(user):46 return user["save_name"]47 48with gr.Blocks() as demo:49 50 with gr.Row():51 with gr.Column():52 gr.Markdown("## Extraction, Classification and AI tool")53 with gr.Column():54 md_username = gr.Markdown(value='## Hi Guest!')55 btn_logout = gr.Button("Logout")56 with gr.Accordion(label="**Login** to keep user preferences", open=False):57 st_user = gr.State(value={"name":"Guest", "hashed_password":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "history": { "keywords": [ "value1", "value3", "value4"], "prompts": [] }, "save_name" : "7783002652599471029.xlsx"})58 with gr.Column():59 tb_user = gr.Textbox(label='Username')60 tb_pwd = gr.Textbox(label='Password', type='password')61 with gr.Row():62 btn_login = gr.Button('Login')63 64 with gr.Tab("File extraction"):65 gr.Markdown("### This part aims to extract the most relevant content and information about every contribution from a 3gpp meeting")66 gr.Markdown(" Put either just a link, or a link and an excel file with an 'Actions' column")67 with gr.Row():68 dd_url = gr.Dropdown(label="(e.g. https://www.3gpp.org/ftp/TSG_SA/WG1_Serv/TSGS1_105_Athens/Docs)", multiselect=False, value="https://www.3gpp.org/ftp/", allow_custom_value=True, scale=9)69 btn_search = gr.Button("Search")70 with gr.Accordion("Filter by file status", open=False):71 with gr.Row():72 dd_status = gr.Dropdown(label="Status to look for (Optional)", allow_custom_value=False, multiselect=True, scale=7)73 btn_search_status = gr.Button("Search for status", scale=2)74 btn_extract = gr.Button("Extract excel from URL")75 76 77 with gr.Tab("Split Files"):78 gr.Markdown("### Upload your standard documentation (pdf, doc, docx) to split it into paragraphs in an Excel file")79 radio = gr.Radio(80 ["Intelligent split", "Intelligent split by keywords", "Non intelligent split"], label="Choose your selection", value = "Intelligent split"81 )82 dropdown_split = gr.Dropdown(["introduction", "objective", "conclusion", "summary"], multiselect=True, visible=False, allow_custom_value=True, label="Select or add keywords")83 nb_split = gr.Number(label="First pages to keep (0 for all)", value=2, interactive=True, visible=True)84 85 fi_input = gr.File(file_count='multiple')86 btn_split = gr.Button("Split")87 88 with gr.Tab("Ask LLM"):89 gr.Markdown("### This section utilizes Large Language Models (LLMs) to query rows in an Excel file")90 dd_source_ask = gr.Dropdown(label="Source Column(s)", multiselect=True)91 tb_destcol = gr.Textbox(label="Destination column label (e.g. Summary, ELI5, PAB)")92 dd_prompt = gr.Dropdown(label="Prompt", allow_custom_value=True, multiselect=True, max_choices=1)93 dd_llm = gr.Dropdown(["Mistral Tiny","Mistral Small","Mistral Medium", "Claude Sonnet", "Claude Opus", "GPT 4o" , "Groq Mixtral", "Groq Llama3 70b", "Groq Llama3 8b", "Perplexity Llama3 70b", "Perplexity Llama3 8b", "Perplexity Llama3 Sonar Small", "Perplexity Llama3 Sonar Large"],value="Groq Llama3 70b", label="Choose your LLM")94 with gr.Accordion("Filters", open=False):95 with gr.Row():96 dd_searchcol = gr.Dropdown(label="Column to look into (Optional)", value='[ALL]', multiselect=False, scale=4)97 dd_keywords = gr.Dropdown(label="Words to look for (Optional)", multiselect=True, allow_custom_value=True, scale=5)98 mist_button = gr.Button("Ask AI")99 fi_checkpoint = gr.File()100 btn_checkpoint = gr.Button("Retrieve checkpoint file")101 102 with gr.Tab("Classification by topic"):103 gr.Markdown("### This section will categories each contribution in your own personalized categories")104 with gr.Row():105 dd_source_class = gr.Dropdown(label="Source Column", multiselect=False, scale=7)106 sl_treshold = gr.Slider(minimum=0, maximum=1, value=0.45, step=0.05, label='Similarity Treshold')107 gr.Markdown("### The predefined categories can be modified at any time")108 109 dd_filter = gr.Dropdown(choices=df_cat_filter, label = "Choose your filters here", multiselect=True, allow_custom_value=True)110 111 btn_filter = gr.Button("Filter")112 113 df_category = gr.DataFrame(label='categories', value=df_cate, interactive=True)114 df_category_hidden = gr.DataFrame(value=df_cate, visible=False)115 116 with gr.Row():117 btn_dl_cate = gr.Button('Download Categories', scale=1)118 fi_categories = gr.File(visible=False, scale=9)119 with gr.Row():120 btn_reset_df = gr.Button("Reset categories")121 btn_classif = gr.Button("Categorize")122 btn_add_categories = gr.Button("Add categories")123 124 125 with gr.Tab(" Personalised Charts Generation"):126 gr.Markdown("### This section will create a chart using two columns of your choice")127 with gr.Row():128 dd_label1 = gr.Dropdown(label="Label 1", multiselect=False)129 dd_label2 = gr.Dropdown(label="Label 2", value="", multiselect=False)130 btn_chart = gr.Button("Generate Bar Plot")131 plt_figure = gr.Plot()132 133 with gr.Tab("Meeting Report (charts)"):134 gr.Markdown("### This section will create a report using multiple charts with your columns")135 gr.Markdown("Make sure you have an 'Expert', 'Source' and 'Status' column")136 with gr.Tab("Overall"):137 btn_overall = gr.Button("Overall Review")138 with gr.Tab("By Expert"):139 dd_exp=gr.Dropdown(label="Experts", multiselect=False, allow_custom_value=True,)140 btn_expert = gr.Button("Top 10 by expert")141 with gr.Tab("By Company"):142 tb_com=gr.Textbox(label="Company Name",info="You can write 1, 2 or 3 company names at the same time")143 btn_type = gr.Button("Company info")144 with gr.Row():145 plt_chart = gr.Plot(label="Graphique")146 plt_chart2 = gr.Plot(label="Graphique")147 plt_chart3 = gr.Plot(label="Graphique")148 149 with gr.Tab("Code on your file"):150 gr.Markdown("### This section lets you add your own code to add functions and filters to edit the files")151 with gr.Accordion("Input DataFrame Preview", open=False):152 df_input = gr.DataFrame(interactive=False)153 gr.Markdown("```python\ndf = pd.read_excel(YOUR_FILE)\n```")154 cd_code = gr.Code(value="# Create a copy of the original DataFrame\nnew_df = df.copy()\n\n# Add a new column to the copy\nnew_df['NewColumn'] = 'New Value'", language='python')155 gr.Markdown("```python\nnew_df.to_excel(YOUR_NEW_FILE)\nreturn YOUR_NEW_FILE\n```")156 157 btn_run_code = gr.Button()158 error_display = gr.Markdown()159 df_output_code = gr.DataFrame(interactive=False)160 btn_export_df = gr.Button('Export df as excel')161 st_filename = gr.State()162 163 with gr.Accordion("Excel Preview", open=False):164 df_output = gr.DataFrame()165 fi_excel = gr.File(label="Excel File")166 167 ht_dl = gr.HTML()168 global fi_config169 fi_config = gr.File(type='binary', visible=False)170 171 # authentication172 btn_login.click(auth_user, inputs=[tb_user, tb_pwd], outputs=[st_user, md_username, dd_prompt, dd_keywords])173 tb_pwd.submit(auth_user, inputs=[tb_user, tb_pwd], outputs=[st_user, md_username, dd_prompt, dd_keywords])174 btn_logout.click(logout, inputs=None, outputs=[st_user, md_username, dd_prompt, dd_keywords])175 176 # 3GPP scraping177 btn_search_status.click(extract_statuses, inputs=dd_url, outputs=dd_status)178 btn_search.click(browse_folder, inputs=dd_url, outputs=dd_url)179 dd_url.change(browse_folder, inputs=dd_url, outputs=dd_url)180 #fi_excel.change(get_expert,inputs=fi_excel, outputs=dd_exp)181 fi_excel.change(get_columns, inputs=[fi_excel], outputs=[dd_source_ask, dd_source_class, dd_label1, dd_label2, dd_searchcol, df_output,st_filename, df_input])182 183 btn_extract.click(extractionPrincipale, inputs=[dd_url, fi_excel, dd_status], outputs=[fi_excel])184 185 186 # Split files187 #fi_input.upload(split_in_df, inputs=fi_input, outputs=fi_excel)188 fi_input.upload(function_split_call, inputs=[fi_input, dropdown_split, radio, nb_split], outputs=fi_excel)189 btn_split.click(function_split_call, inputs=[fi_input, dropdown_split, radio, nb_split], outputs=fi_excel)190 radio.change(fn=change_textbox, inputs=[dropdown_split,radio], outputs=[dropdown_split, nb_split])191 192 #llm193 mist_button.click(chat_with_mistral, inputs=[dd_source_ask, tb_destcol, dd_prompt, fi_excel, dd_url, dd_searchcol, dd_keywords, dd_llm, st_user], outputs=[fi_excel, df_output, dd_prompt, dd_keywords, st_user, fi_config])194 btn_checkpoint.click(retrieve_checkpoint, inputs=st_user, outputs=fi_checkpoint)195 196 #classification197 btn_classif.click(classification, inputs=[dd_source_class, fi_excel, df_category, sl_treshold], outputs=[fi_excel, df_output])198 btn_reset_df.click(reset_cate, inputs=df_category, outputs=df_category)199 btn_filter.click(filter_by_topics, inputs=[dd_filter, df_category_hidden], outputs=df_category)200 btn_add_categories.click(add_categories, inputs=[df_category, df_category_hidden],outputs=[dd_filter,df_category_hidden]) 201 btn_dl_cate.click(download_cate, inputs=df_category, outputs=fi_categories)202 203 #charts204 btn_chart.click(create_bar_plot, inputs=[fi_excel, dd_label1, dd_label2], outputs=[plt_figure])205 206 #json download207 fi_config.change(list_attributes_and_values, inputs=None, outputs=ht_dl)208 209 btn_run_code.click(run_code, inputs=[fi_excel, cd_code], outputs=[df_output_code, error_display])210 btn_export_df.click(export_df, inputs=[df_output_code, st_filename], outputs=fi_excel)211 212 213 btn_overall.click(generate_company_chart,inputs=[fi_excel], outputs=[plt_chart])214 btn_overall.click(status_chart,inputs=[fi_excel], outputs=[plt_chart2])215 btn_overall.click(category_chart,inputs=[fi_excel], outputs=[plt_chart3])216 btn_expert.click(chart_by_expert,inputs=[fi_excel,dd_exp], outputs=[plt_chart])217 btn_type.click(company_document_type,inputs=[fi_excel,tb_com], outputs=[plt_chart])218 # dd_label1.change(update_label, inputs=[dd_label1], outputs=[dd_label2])219 220 221demo.launch(debug=True)