Spaces:
Sleeping
Sleeping
File size: 35,087 Bytes
1ef648a 76bfb75 bddf29f a2fcce4 6a89968 2d48be5 7f91fc3 8cac918 7f91fc3 8cac918 7f91fc3 8cac918 7f91fc3 8cac918 7f91fc3 8cac918 7f91fc3 6a89968 7f91fc3 6a89968 7f91fc3 7e1f943 4119a04 6a89968 a2fcce4 4119a04 61e6b62 6a89968 a2fcce4 443053b 8c3b0f0 39dbf03 8c3b0f0 8b497ae 8c3b0f0 7f8700c 8c3b0f0 39dbf03 8b497ae 8c3b0f0 ea95a7e 50515cb b7709fc 7386d73 b7709fc 7386d73 b7709fc a2fcce4 b7709fc 35e172a 8c3b0f0 35e172a 8c3b0f0 35e172a 8c3b0f0 8c34617 b7709fc 452c821 8c34617 b7709fc 8c3b0f0 a2fcce4 8c3b0f0 35e172a 2e7a421 35e172a 2e7a421 0e7ae0f b4b6a14 35e172a ee949ff 3a168c3 e46b418 9f9f9bd 6a89968 b4b6a14 2e7a421 b4b6a14 2e7a421 b4b6a14 0e7ae0f 2e7a421 b4b6a14 35e172a 0e7ae0f 2e7a421 6a89968 a55daf9 887a7c1 35e172a 8c3b0f0 5a0f2dc efb3f07 89ee992 efb3f07 89ee992 8c3b0f0 5a0f2dc 4259c64 e46b418 9f9f9bd 4259c64 6a89968 5a0f2dc 4259c64 6a89968 a55daf9 5a0f2dc 8c3b0f0 efb3f07 3a814a1 b59ee01 2b42392 b59ee01 9f9f9bd b59ee01 e40f174 9f9f9bd e40f174 9f9f9bd e40f174 9f9f9bd e40f174 9f9f9bd e40f174 9f9f9bd e40f174 9f9f9bd e40f174 e46b418 e40f174 a0f12e7 3a814a1 6a89968 3a814a1 a0f12e7 af1e983 3a814a1 6a89968 aab8d10 3a814a1 6a89968 aab8d10 f97b86a e46b418 9f9f9bd 3a814a1 a0f12e7 e46b418 a0f12e7 e46b418 9f9f9bd 6a89968 b59ee01 e46b418 b59ee01 9f9f9bd e46b418 b59ee01 9f9f9bd e46b418 0449345 af1e983 f97b86a af1e983 f97b86a 3a814a1 a0f12e7 3a814a1 f97b86a 3a814a1 a2fcce4 6a89968 b446f1b 8c3b0f0 a2fcce4 35e172a 6a89968 35e172a cb90440 6a89968 b7709fc e636253 35e172a e636253 a55daf9 6a89968 e636253 6a89968 e636253 35e172a 6a89968 efb3f07 5a0f2dc f7c68fc 5a0f2dc a55daf9 6a89968 5a0f2dc 6a89968 efb3f07 2fbc9fe 4169bb8 bd96e0d 3a814a1 6d406c0 bd96e0d 3f53103 b59ee01 bd96e0d 3a814a1 6a89968 bd96e0d 5a0f2dc 3a814a1 8c3b0f0 df711e3 8c3b0f0 a2fcce4 6a89968 bddf29f 6a89968 df711e3 e4d07f2 f1eb71f 6a89968 bd96e0d af1e983 3e19acf af1e983 3e19acf af1e983 3e19acf af1e983 3e19acf af1e983 0449345 af1e983 3d103e2 0449345 3a814a1 af1e983 3e19acf 0449345 a55daf9 38b2a27 3e19acf 38b2a27 3e19acf f80d2db e636253 f80d2db 6a89968 3a814a1 443053b e4d07f2 8b497ae e636253 6a89968 e636253 8b497ae bddf29f 8b497ae 1398651 5130254 0449345 5130254 21e8468 de38b62 bddf29f 99fe899 1398651 0b34e59 f64fef3 abe9790 3e19acf f64fef3 0b34e59 5c716b2 1bf26c9 4119a04 1bf26c9 4119a04 5c957ad 5946b43 81c73a1 de38b62 81c73a1 5946b43 4119a04 294c24c 4119a04 fa36f72 db05398 e9c524f 5c957ad 8c3b0f0 60cbd2f 5946b43 8c3b0f0 5c957ad 77698e2 7f91fc3 4119a04 d95655c 76bfb75 0b34e59 1446dbe 7e1f943 21e8468 |
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
import csv
import sys
# Increase CSV field size limit
csv.field_size_limit(sys.maxsize)
import gradio as gr
import pandas as pd
def data_pre_processing(file_responses):
consoleMessage_and_Print("Starting data pre-processing...")
# Financial Weights can be anything (ultimately the row-wise weights are aggregated and the corresponding fractions are obtained from that rows' total tax payed)
try: # Define the columns to be processed
# Developing Numeric Columns
# Convert columns to numeric and fill NaN values with 0
file_responses['Personal_TaxDirection_1_TaxWeightageAllocated'] = pd.to_numeric(file_responses['Personal_TaxDirection_1_TaxWeightageAllocated'], errors='coerce').fillna(0)
file_responses['Personal_TaxDirection_2_TaxWeightageAllocated'] = pd.to_numeric(file_responses['Personal_TaxDirection_2_TaxWeightageAllocated'], errors='coerce').fillna(0)
file_responses['Personal_TaxDirection_3_TaxWeightageAllocated'] = pd.to_numeric(file_responses['Personal_TaxDirection_3_TaxWeightageAllocated'], errors='coerce').fillna(0)
file_responses['Latest estimated Tax payment?'] = pd.to_numeric(file_responses['Latest estimated Tax payment?'], errors='coerce').fillna(0)
# Adding a new column 'TotalWeightageAllocated' by summing specific columns by their names
file_responses['TotalWeightageAllocated'] = file_responses['Personal_TaxDirection_1_TaxWeightageAllocated'] + file_responses['Personal_TaxDirection_2_TaxWeightageAllocated'] + file_responses['Personal_TaxDirection_3_TaxWeightageAllocated']
# Creating Datasets (we assume everything has been provided to us in English, or the translations have been done already)
# Renaming the datasets into similar column headings
initial_dataset_1 = file_responses.rename(columns={
'Personal_TaxDirection_1_Wish': 'Problem_Description',
'Personal_TaxDirection_1_GeographicalLocation': 'Geographical_Location',
'Personal_TaxDirection_1_TaxWeightageAllocated': 'Financial_Weight'
})[['Problem_Description', 'Geographical_Location', 'Financial_Weight']]
initial_dataset_2 = file_responses.rename(columns={
'Personal_TaxDirection_2_Wish': 'Problem_Description',
'Personal_TaxDirection_2_GeographicalLocation': 'Geographical_Location',
'Personal_TaxDirection_2_TaxWeightageAllocated': 'Financial_Weight'
})[['Problem_Description', 'Geographical_Location', 'Financial_Weight']]
initial_dataset_3 = file_responses.rename(columns={
'Personal_TaxDirection_3_Wish': 'Problem_Description',
'Personal_TaxDirection_3_GeographicalLocation': 'Geographical_Location',
'Personal_TaxDirection_3_TaxWeightageAllocated': 'Financial_Weight'
})[['Problem_Description', 'Geographical_Location', 'Financial_Weight']]
# Calculating the actual TaxAmount to be allocated against each WISH (by overwriting the newly created columns)
initial_dataset_1['Financial_Weight'] = file_responses['Personal_TaxDirection_1_TaxWeightageAllocated'] * file_responses['Latest estimated Tax payment?'] / file_responses['TotalWeightageAllocated']
initial_dataset_2['Financial_Weight'] = file_responses['Personal_TaxDirection_2_TaxWeightageAllocated'] * file_responses['Latest estimated Tax payment?'] / file_responses['TotalWeightageAllocated']
initial_dataset_3['Financial_Weight'] = file_responses['Personal_TaxDirection_3_TaxWeightageAllocated'] * file_responses['Latest estimated Tax payment?'] / file_responses['TotalWeightageAllocated']
# Removing useless rows # Drop rows where Problem_Description is NaN or an empty string
initial_dataset_1 = initial_dataset_1.dropna(subset=['Problem_Description'], axis=0)
initial_dataset_2 = initial_dataset_2.dropna(subset=['Problem_Description'], axis=0)
initial_dataset_3 = initial_dataset_3.dropna(subset=['Problem_Description'], axis=0)
# Convert 'Problem_Description' column to string type
initial_dataset_1['Problem_Description'] = initial_dataset_1['Problem_Description'].astype(str)
initial_dataset_2['Problem_Description'] = initial_dataset_2['Problem_Description'].astype(str)
initial_dataset_3['Problem_Description'] = initial_dataset_3['Problem_Description'].astype(str)
# Merging the Datasets # Vertically concatenating (merging) the 3 DataFrames
merged_dataset = pd.concat([initial_dataset_1, initial_dataset_2, initial_dataset_3], ignore_index=True)
# Different return can be used to check the processing
consoleMessage_and_Print("Data pre-processing completed.")
return merged_dataset
except Exception as e:
consoleMessage_and_Print(f"Error during data pre-processing: {str(e)}")
return None
import spacy
from transformers import AutoTokenizer, AutoModel
import torch
# Load SpaCy model
# Install the 'en_core_web_sm' model if it isn't already installed
try:
nlp = spacy.load('en_core_web_sm')
except OSError:
# Instead of this try~catch, we could also include this < https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0.tar.gz > in the requirements.txt to directly load it
from spacy.cli import download
download('en_core_web_sm')
nlp = spacy.load('en_core_web_sm')
# Load Hugging Face Transformers model
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-mpnet-base-v2")
model = AutoModel.from_pretrained("sentence-transformers/all-mpnet-base-v2")
import re
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
# Download necessary NLTK data
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('averaged_perceptron_tagger')
import numpy as np
import sentencepiece as sp
from transformers import pipeline
# Load a summarization model
summarizer = pipeline("summarization")
def Summarized_text(passed_text):
try:
# Summarization
summarize_text = summarizer(passed_text, max_length=70, min_length=30, do_sample=False)[0]['summary_text']
return summarize_text
except Exception as e:
print(f"Summarization failed: {e}")
return passed_text
###### Will uncomment Summarization during final deployment... as it takes a lot of time
def Lemmatize_text(text):
# Text Cleaning
text = re.sub(r'[^\w\s]', '', text)
text = re.sub(r'\d+', '', text)
text = re.sub(r'http\S+', '', text) # Remove https URLs
text = re.sub(r'www\.\S+', '', text) # Remove www URLs
# Tokenize and remove stopwords
tokens = word_tokenize(text.lower())
stop_words = set(stopwords.words('english'))
custom_stopwords = {'example', 'another'} # Add custom stopwords
tokens = [word for word in tokens if word not in stop_words and word not in custom_stopwords]
# NER - Remove named entities
doc = nlp(' '.join(tokens))
tokens = [token.text for token in doc if not token.ent_type_]
# POS Tagging (optional)
pos_tags = nltk.pos_tag(tokens)
tokens = [word for word, pos in pos_tags if pos in ['NN', 'NNS']] # Filter nouns
# Lemmatize tokens using SpaCy
doc = nlp(' '.join(tokens))
lemmatized_text = ' '.join([token.lemma_ for token in doc])
return lemmatized_text # Return the cleaned and lemmatized text
from random import random
def text_processing_for_domain(text):
# First, get the summarized text
summarized_text = ""
# summarized_text = Summarized_text(text)
# Then, lemmatize the original text
lemmatized_text = ""
lemmatized_text = Lemmatize_text(text)
if lemmatized_text and summarized_text:
# Join both the summarized and lemmatized text
if random() > 0.5:
combined_text = summarized_text + " " + lemmatized_text
else:
combined_text = lemmatized_text + " " + summarized_text
return combined_text
elif summarized_text:
return summarized_text
elif lemmatized_text:
return lemmatized_text
else:
return "Sustainability and Longevity" # Default FailSafe
from sentence_transformers import SentenceTransformer
from sklearn.cluster import AgglomerativeClustering, KMeans
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import silhouette_score
from bertopic import BERTopic
from collections import Counter
def extract_problem_domains(df,
text_column='Processed_ProblemDescription_forDomainExtraction',
cluster_range=(6, 8),
top_words=7):
consoleMessage_and_Print("Extracting Problem Domains...")
# Sentence Transformers approach
model = SentenceTransformer('all-mpnet-base-v2')
embeddings = model.encode(df[text_column].tolist())
# Perform hierarchical clustering with Silhouette Analysis
silhouette_scores = []
for n_clusters in range(cluster_range[0], cluster_range[1] + 1):
clustering = AgglomerativeClustering(n_clusters=n_clusters)
cluster_labels = clustering.fit_predict(embeddings)
silhouette_avg = silhouette_score(embeddings, cluster_labels)
silhouette_scores.append(silhouette_avg)
# Determine the optimal number of clusters
optimal_n_clusters = cluster_range[0] + silhouette_scores.index(max(silhouette_scores))
# Perform clustering with the optimal number of clusters
clustering = AgglomerativeClustering(n_clusters=optimal_n_clusters)
cluster_labels = clustering.fit_predict(embeddings)
# Get representative words for each cluster
cluster_representations = {}
for i in range(optimal_n_clusters):
cluster_words = df.loc[cluster_labels == i, text_column].str.cat(sep=' ').split()
cluster_representations[i] = [word for word, _ in Counter(cluster_words).most_common(top_words)]
# Map cluster labels to representative words
df["Problem_Cluster"] = cluster_labels
df['Problem_Category_Words'] = [cluster_representations[label] for label in cluster_labels]
consoleMessage_and_Print("Problem Domain Extraction completed. Returning from Problem Domain Extraction function.")
return df, optimal_n_clusters, cluster_representations
def Extract_Location(text):
doc = nlp(text)
locations = [ent.text for ent in doc.ents if ent.label_ in ['GPE', 'LOC']]
return ' '.join(locations)
def text_processing_for_location(text):
# Extract locations
locations_text = Extract_Location(text)
# Perform further text cleaning if necessary
processed_locations_text = Lemmatize_text(locations_text)
# Remove special characters, digits, and punctuation
processed_locations_text = re.sub(r'[^a-zA-Z\s]', '', processed_locations_text)
# Tokenize and remove stopwords
tokens = word_tokenize(processed_locations_text.lower())
stop_words = set(stopwords.words('english'))
tokens = [word for word in tokens if word not in stop_words]
# Join location words into a single string
final_locations_text = ' '.join(tokens)
return final_locations_text if final_locations_text else "India"
def extract_location_clusters(df,
text_column1='Processed_LocationText_forClustering', # Extracted through NLP
text_column2='Geographical_Location', # User Input
cluster_range=(3, 5),
top_words=3):
# Combine the two text columns
text_column = "Combined_Location_Text"
df[text_column] = df[text_column1] + ' ' + df[text_column2]
consoleMessage_and_Print("Extracting Location Clusters...")
# Sentence Transformers approach for embeddings
model = SentenceTransformer('all-mpnet-base-v2')
embeddings = model.encode(df[text_column].tolist())
# Perform hierarchical clustering with Silhouette Analysis
silhouette_scores = []
for n_clusters in range(cluster_range[0], cluster_range[1] + 1):
clustering = AgglomerativeClustering(n_clusters=n_clusters)
cluster_labels = clustering.fit_predict(embeddings)
silhouette_avg = silhouette_score(embeddings, cluster_labels)
silhouette_scores.append(silhouette_avg)
# Determine the optimal number of clusters
optimal_n_clusters = cluster_range[0] + silhouette_scores.index(max(silhouette_scores))
# Perform clustering with the optimal number of clusters
clustering = AgglomerativeClustering(n_clusters=optimal_n_clusters)
cluster_labels = clustering.fit_predict(embeddings)
# Get representative words for each cluster
cluster_representations = {}
for i in range(optimal_n_clusters):
cluster_words = df.loc[cluster_labels == i, text_column].str.cat(sep=' ').split()
cluster_representations[i] = [word for word, _ in Counter(cluster_words).most_common(top_words)]
# Map cluster labels to representative words
df["Location_Cluster"] = cluster_labels
df['Location_Category_Words'] = [cluster_representations[label] for label in cluster_labels]
df = df.drop(text_column, axis=1)
consoleMessage_and_Print("Location Clustering completed.")
return df, optimal_n_clusters, cluster_representations
def create_cluster_dataframes(processed_df):
# Create a dataframe for Financial Weights
budget_cluster_df = processed_df.pivot_table(
values='Financial_Weight',
index='Location_Cluster',
columns='Problem_Cluster',
aggfunc='sum',
fill_value=0)
# Create a dataframe for Problem Descriptions
problem_cluster_df = processed_df.groupby(['Location_Cluster', 'Problem_Cluster'])['Problem_Description'].apply(list).unstack()
return budget_cluster_df, problem_cluster_df
from transformers import GPTNeoForCausalLM, GPT2Tokenizer
def generate_project_proposal(prompt):
print("Trying to access gpt-neo-1.3B")
print("prompt: \t", prompt)
# Generate the proposal
model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-1.3B")
tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B")
try:
# input_ids = tokenizer.encode(prompt, return_tensors="pt")
# Truncate the prompt to fit within the model's input limits
max_input_length = 2048 # Adjust as per your model's limit
input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True, max_length=max_input_length)
print("Input IDs shape:", input_ids.shape)
output = model.generate(
input_ids,
# max_length=300,
max_new_tokens=500,
num_return_sequences=1,
no_repeat_ngram_size=2,
temperature=0.5,
pad_token_id=tokenizer.eos_token_id # Ensure padding with EOS token
)
print("Output shape:", output.shape)
proposal = tokenizer.decode(output[0], skip_special_tokens=True)
if "Project Proposal:" in proposal:
proposal = proposal.split("Project Proposal:", 1)[1].strip()
else:
proposal = proposal.strip()
# print("Successfully accessed gpt-neo-1.3B and returning")
print("Generated Proposal: ", proposal)
return proposal
except Exception as e:
print("Error generating proposal:", str(e))
return "Hyper-local Sustainability Projects would lead to Longevity of the self and Prosperity of the community. Therefore UNSDGs coupled with Longevity initiatives should be focused upon."
import copy
def create_project_proposals(budget_cluster_df, problem_cluster_df, location_clusters, problem_clusters):
consoleMessage_and_Print("\n Starting function: create_project_proposals")
proposals = {}
sanban_debug = False
for loc in budget_cluster_df.index:
consoleMessage_and_Print(f"\n loc: {loc}")
for prob in budget_cluster_df.columns:
consoleMessage_and_Print(f"\n prob: {prob}")
location = ", ".join([item.strip() for item in location_clusters[loc] if item]) # Clean and join
problem_domain = ", ".join([item.strip() for item in problem_clusters[prob] if item]) # Clean and join
shuffled_descriptions = copy.deepcopy(problem_cluster_df.loc[loc, prob])
# Create a deep copy of the problem descriptions, shuffle it, and join the first 10
print("location: ", location)
print("problem_domain: ", problem_domain)
print("problem_descriptions: ", shuffled_descriptions)
# Check if problem_descriptions is valid (not NaN and not an empty list)
if isinstance(shuffled_descriptions, list) and shuffled_descriptions:
# print(f"\nGenerating proposal for location: {location}, problem domain: {problem_domain}")
consoleMessage_and_Print(f"Generating PP")
random.shuffle(shuffled_descriptions)
# Prepare the prompt
# problems_summary = "; \n".join(problem_descriptions) # Join all problem descriptions
# problems_summary = "; \n".join(problem_descriptions[:3]) # Limit to first 3 for brevity
problems_summary = "; \n".join(shuffled_descriptions[:3])
# prompt = f"Generate a solution oriented project proposal for the following:\n\nLocation: {location}\nProblem Domain: {problem_domain}\nProblems: {problems_summary}\n\nProject Proposal:"
# prompt = f"Generate a solution-oriented project proposal for the following public problem (only output the proposal):\n\n Geographical/Digital Location: {location}\nProblem Category: {problem_domain}\nProblems: {problems_summary}\n\nProject Proposal:"
prompt = f"Generate a single solution-oriented project proposal bespoke to the following Location~Domain cluster of public problems:\n\n Geographical/Digital Location: {location}\nProblem Domain: {problem_domain}\nProblems: {problems_summary}\n\nProject Proposal: <only output this proposal>"
proposal = generate_project_proposal(prompt)
# Check if proposal is valid
if isinstance(proposal, str) and proposal.strip(): # Valid string that's not empty
proposals[(loc, prob)] = proposal
sanban_debug = True
break
else:
print(f"Skipping empty problem descriptions for location: {location}, problem domain: {problem_domain}")
if sanban_debug:
break
return proposals
# def create_project_proposals(budget_cluster_df, problem_cluster_df, location_clusters, problem_clusters):
# print("\n Starting function: create_project_proposals")
# console_messages.append("\n Starting function: create_project_proposals")
# proposals = {}
# for loc in budget_cluster_df.index:
# print("\n loc: ", loc)
# console_messages.append(f"\n loc: {loc}")
# for prob in budget_cluster_df.columns:
# console_messages.append(f"\n prob: {prob}")
# print("\n prob: ", prob)
# location = ", ".join([item.strip() for item in location_clusters[loc] if item]) # Clean and join
# problem_domain = ", ".join([item.strip() for item in problem_clusters[prob] if item]) # Clean and join
# problem_descriptions = problem_cluster_df.loc[loc, prob]
# print("location: ",location)
# print("problem_domain: ",problem_domain)
# print("problem_descriptions: ",problem_descriptions)
# if problem_descriptions:# and not pd.isna(problem_descriptions):
# print(f"\nGenerating proposal for location: {location}, problem domain: {problem_domain}")
# # console_messages.append(f"\nGenerating proposal for location: {location}, problem domain: {problem_domain}")
# # Prepare the prompt
# problems_summary = "; \n".join(problem_descriptions[:3]) # Limit to first 3 for brevity
# # problems_summary = "; ".join(problem_descriptions)
# # prompt = f"Generate a project proposal for the following:\n\nLocation: {location}\nProblem Domain: {problem_domain}\nProblems: {problems_summary}\nBudget: ${financial_weight:.2f}\n\nProject Proposal:"
# prompt = f"Generate a solution oriented project proposal for the following:\n\nLocation: {location}\nProblem Domain: {problem_domain}\nProblems: {problems_summary}\n\nProject Proposal:"
# proposal = generate_project_proposal(prompt)
# proposals[(loc, prob)] = proposal
# print("Generated Proposal: ", proposal)
# else:
# print(f"Skipping empty problem descriptions for location: {location}, problem domain: {problem_domain}")
# return proposals
# def create_project_proposals(budget_cluster_df, problem_cluster_df, location_clusters, problem_clusters):
# print("\n Starting function: create_project_proposals")
# console_messages.append("\n Starting function: create_project_proposals")
# proposals = {}
# for loc in budget_cluster_df.index:
# for prob in budget_cluster_df.columns:
# location = ", ".join(location_clusters[loc])
# problem_domain = ", ".join(problem_clusters[prob])
# problem_descriptions = problem_cluster_df.loc[loc, prob]
# if problem_descriptions:
# proposal = generate_project_proposal(
# problem_descriptions,
# location,
# problem_domain)
# proposals[(loc, prob)] = proposal
# console_messages.append("\n Exiting function: create_project_proposals")
# return proposals
def nlp_pipeline(original_df):
consoleMessage_and_Print("Starting NLP pipeline...")
# Data Preprocessing
processed_df = data_pre_processing(original_df) # merged_dataset
# Starting the Pipeline for Domain Extraction
consoleMessage_and_Print("Executing Text processing function for Domain identification")
# Apply the text_processing_for_domain function to the DataFrame
processed_df['Processed_ProblemDescription_forDomainExtraction'] = processed_df['Problem_Description'].apply(text_processing_for_domain)
consoleMessage_and_Print("Removing entries which could not be allocated to any Problem Domain")
# processed_df = processed_df.dropna(subset=['Processed_ProblemDescription_forDomainExtraction'], axis=0)
# Drop rows where 'Processed_ProblemDescription_forDomainExtraction' contains empty arrays
processed_df = processed_df[processed_df['Processed_ProblemDescription_forDomainExtraction'].apply(lambda x: len(x) > 0)]
# Domain Clustering
try:
processed_df, optimal_n_clusters, problem_clusters = extract_problem_domains(processed_df)
consoleMessage_and_Print(f"Optimal clusters for Domain extraction: {optimal_n_clusters}")
except Exception as e:
consoleMessage_and_Print(f"Error in extract_problem_domains: {str(e)}")
consoleMessage_and_Print("NLP pipeline for Problem Domain extraction completed.")
consoleMessage_and_Print("Starting NLP pipeline for Location extraction with text processing.")
# Apply the text_processing_for_location function to the DataFrame
processed_df['Processed_LocationText_forClustering'] = processed_df['Problem_Description'].apply(text_processing_for_location)
# processed_df['Processed_LocationText_forClustering'], processed_df['Extracted_Locations'] = zip(*processed_df.apply(text_processing_for_location, axis=1))
# Location Clustering
try:
processed_df, optimal_n_clusters, location_clusters = extract_location_clusters(processed_df)
consoleMessage_and_Print(f"Optimal clusters for Location extraction: {optimal_n_clusters}")
except Exception as e:
consoleMessage_and_Print(f"Error in extract_location_clusters: {str(e)}")
consoleMessage_and_Print("NLP pipeline for location extraction completed.")
# Create cluster dataframes
budget_cluster_df, problem_cluster_df = create_cluster_dataframes(processed_df)
print("Clustering Done...")
# return processed_df, budget_cluster_df, problem_cluster_df, location_clusters, problem_clusters
print("\n location_clusters_1: ", location_clusters)
print("\n problem_clusters_1: ", problem_clusters)
# # Generate project proposals
# location_clusters = dict(enumerate(processed_df['Location_Category_Words'].unique()))
# problem_clusters = dict(enumerate(processed_df['Problem_Category_Words'].unique()))
# print("\n location_clusters_2: ", location_clusters)
# print("\n problem_clusters_2: ", problem_clusters)
project_proposals = create_project_proposals(budget_cluster_df, problem_cluster_df, location_clusters, problem_clusters)
consoleMessage_and_Print("NLP pipeline completed.")
return processed_df, budget_cluster_df, problem_cluster_df, project_proposals, location_clusters, problem_clusters
console_messages = []
def consoleMessage_and_Print(some_text = ""):
console_messages.append(some_text)
print(some_text)
def process_excel(file):
consoleMessage_and_Print("Processing starts. Reading the uploaded Excel file...")
# Ensure the file path is correct
file_path = file.name if hasattr(file, 'name') else file
# Read the Excel file
df = pd.read_excel(file_path)
try:
# Process the DataFrame
consoleMessage_and_Print("Processing the DataFrame...")
processed_df, budget_cluster_df, problem_cluster_df, project_proposals, location_clusters, problem_clusters = nlp_pipeline(df)
# processed_df, budget_cluster_df, problem_cluster_df, location_clusters, problem_clusters = nlp_pipeline(df)
consoleMessage_and_Print("Error was here")
#This code first converts the dictionary to a DataFrame with a single column for the composite key.
#Then, it splits the composite key into separate columns for Location_Cluster and Problem_Cluster.
#Finally, it reorders the columns and writes the DataFrame to an Excel sheet.
try: # Meta AI Solution
# Convert project_proposals dictionary to DataFrame
project_proposals_df = pd.DataFrame(list(project_proposals.items()), columns=['Location_Cluster_Problem_Cluster', 'Solutions Proposed'])
# consoleMessage_and_Print("CheckPoint 1")
# Split the composite key into separate columns
project_proposals_df[['Location_Cluster', 'Problem_Cluster']] = project_proposals_df['Location_Cluster_Problem_Cluster'].apply(pd.Series)
# consoleMessage_and_Print("CheckPoint 2")
# Drop the composite key column
project_proposals_df.drop('Location_Cluster_Problem_Cluster', axis=1, inplace=True)
# consoleMessage_and_Print("CheckPoint 3")
# Reorder the columns
project_proposals_df = project_proposals_df[['Location_Cluster', 'Problem_Cluster', 'Solutions Proposed']]
# consoleMessage_and_Print("CheckPoint 4")
except Exception as e:
consoleMessage_and_Print("Meta AI Solution did not work, trying CHATGPT solution")
try:
# Convert project_proposals dictionary to DataFrame
project_proposals_df = pd.DataFrame.from_dict(
proposals, orient='index', columns=['Solutions Proposed']
)
# If the index is a tuple, it automatically becomes a MultiIndex, so we handle naming correctly:
if isinstance(project_proposals_df.index, pd.MultiIndex):
project_proposals_df.index.names = ['Location_Cluster', 'Problem_Cluster']
else:
# If for some reason it's not a MultiIndex, we name it appropriately
project_proposals_df.index.name = 'Cluster'
# Reset index to have Location_Cluster and Problem_Cluster as columns
project_proposals_df.reset_index(inplace=True)
except Exception as e:
print(e)
# ### Convert project_proposals dictionary to DataFrame
# project_proposals_df = pd.DataFrame.from_dict(project_proposals, orient='index', columns=['Solutions Proposed'])
# project_proposals_df.index.names = ['Location_Cluster', 'Problem_Cluster']
# project_proposals_df.reset_index(inplace=True)
consoleMessage_and_Print("Creating the Excel file.")
output_filename = "OutPut_PPs.xlsx"
with pd.ExcelWriter(output_filename) as writer:
processed_df.to_excel(writer, sheet_name='Input_Processed', index=False)
budget_cluster_df.to_excel(writer, sheet_name='Financial_Weights')
problem_cluster_df.to_excel(writer, sheet_name='Problem_Descriptions')
try:
project_proposals_df.to_excel(writer, sheet_name='Project_Proposals', index=False)
except Exception as e:
consoleMessage_and_Print("Error during Project Proposal excelling at the end")
# Ensure location_clusters and problem_clusters are in DataFrame format
if isinstance(location_clusters, pd.DataFrame):
location_clusters.to_excel(writer, sheet_name='Location_Clusters', index=False)
else:
consoleMessage_and_Print("Converting Location Clusters to df")
pd.DataFrame(location_clusters).to_excel(writer, sheet_name='Location_Clusters', index=False)
if isinstance(problem_clusters, pd.DataFrame):
problem_clusters.to_excel(writer, sheet_name='Problem_Clusters', index=False)
else:
consoleMessage_and_Print("Converting Problem Clusters to df")
pd.DataFrame(problem_clusters).to_excel(writer, sheet_name='Problem_Clusters', index=False)
consoleMessage_and_Print("Processing completed. Ready for download.")
return output_filename, "\n".join(console_messages) # Return the processed DataFrame as Excel file
except Exception as e:
# return str(e) # Return the error message
# error_message = f"Error processing file: {str(e)}"
# print(error_message) # Log the error
consoleMessage_and_Print(f"Error during processing: {str(e)}")
# return error_message, "Santanu Banerjee" # Return the error message to the user
return None, "\n".join(console_messages)
example_files = []
# example_files.append('#TaxDirection (Responses)_BasicExample.xlsx')
example_files.append('#TaxDirection (Responses)_IntermediateExample.xlsx')
# example_files.append('#TaxDirection (Responses)_UltimateExample.xlsx')
import random
a_random_object = random.choice(["⇒", "↣", "↠", "→"])
# Define the Gradio interface
interface = gr.Interface(
fn=process_excel, # The function to process the uploaded file
inputs=gr.File(type="filepath", label="Upload Excel File here. \t Be sure to check that the column headings in your upload are the same as in the Example files below. \t (Otherwise there will be Error during the processing)"), # File upload input
examples=example_files, # Add the example files
outputs=[
gr.File(label="Download the processed Excel File containing the ** Project Proposals ** for each Location~Problem paired combination"), # File download output
gr.Textbox(label="Console Messages", lines=10, interactive=False) # Console messages output
],
# title="Excel File Uploader",
# title="Upload Excel file containing #TaxDirections → Download HyperLocal Project Proposals\n",
title = (
"<p style='font-weight: bold; font-size: 25px; text-align: center;'>"
"<span style='color: blue;'>Upload Excel file containing #TaxDirections</span> "
# "<span style='color: brown; font-size: 35px;'>→ </span>"
# "<span style='color: brown; font-size: 35px;'>⇒ ↣ ↠ </span>"
"<span style='color: brown; font-size: 35px;'> " +a_random_object +" </span>"
"<span style='color: green;'>Download HyperLocal Project Proposals</span>"
"</p>\n"
),
description=(
"<p style='font-size: 12px; color: gray; text-align: center'>This tool allows for the systematic evaluation and proposal of solutions tailored to specific location-problem pairs, ensuring efficient resource allocation and project planning. For more information, visit <a href='https://santanban.github.io/TaxDirection/' target='_blank'>#TaxDirection weblink</a>.</p>"
"<p style='font-weight: bold; font-size: 16px; color: blue;'>Upload an Excel file to process and download the result or use the Example files:</p>"
"<p style='font-weight: bold; font-size: 15px; color: blue;'>(click on any of them to directly process the file and Download the result)</p>"
"<p style='font-weight: bold; font-size: 14px; color: green; text-align: right;'>Processed output contains a Project Proposal for each Location~Problem paired combination (i.e. each cell).</p>"
"<p style='font-weight: bold; font-size: 13px; color: green; text-align: right;'>Corresponding Budget Allocation and estimated Project Completion Time are provided in different sheets.</p>"
"<p style='font-size: 12px; color: gray; text-align: center'>Note: The example files provided above are for demonstration purposes. Feel free to upload your own Excel files to see the results. If you have any questions, refer to the documentation-links or contact <a href='https://www.change.org/p/democracy-evolution-ensuring-humanity-s-eternal-existence-through-taxdirection' target='_blank'>support</a>.</p>"
) # Solid description with right-aligned second sentence
)
# Launch the interface
if __name__ == "__main__":
interface.launch() |