File size: 24,973 Bytes
a87bc00
 
 
bcf08f8
a87bc00
c405013
2cc0cb0
a87bc00
 
c405013
 
529eea1
bcf08f8
 
7b11062
c405013
a87bc00
c405013
a87bc00
 
c27c36d
a87bc00
bcf08f8
029ffc9
 
bcf08f8
f18a30e
63ec771
a92d4d8
08c56ef
a92d4d8
c405013
bcf08f8
029ffc9
b3aaf10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c405013
bcf08f8
97be419
bcf08f8
a87bc00
bcf08f8
a87bc00
2cbc17e
a87bc00
bcf08f8
c405013
bcf08f8
c405013
 
 
bcf08f8
c405013
bcf08f8
 
 
c405013
bcf08f8
c405013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcf08f8
 
 
a87bc00
 
bcf08f8
 
 
 
 
 
 
 
 
 
a87bc00
 
bcf08f8
 
 
 
a87bc00
bcf08f8
 
029ffc9
 
c405013
 
 
029ffc9
 
2cbc17e
029ffc9
 
2cbc17e
029ffc9
bcf08f8
 
029ffc9
 
 
bcf08f8
029ffc9
c405013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a87bc00
bcf08f8
 
 
 
2cbc17e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5beb0e
bcf08f8
 
 
 
54ed831
bcf08f8
c405013
bcf08f8
3d52ed2
 
bcf08f8
3d52ed2
 
bcf08f8
 
3d52ed2
 
 
 
 
 
 
 
 
 
 
bcf08f8
2cbc17e
3d52ed2
2cbc17e
bcf08f8
2cbc17e
3d52ed2
 
 
 
 
 
 
bcf08f8
2cbc17e
 
 
c405013
 
2cbc17e
 
c405013
2cbc17e
 
54ed831
bcf08f8
3d52ed2
200dcdb
2cc0cb0
 
d6d183b
2cbc17e
200dcdb
3d52ed2
200dcdb
2cbc17e
2cc0cb0
d6d183b
 
 
 
 
 
 
 
 
 
 
 
 
2cbc17e
d6d183b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200dcdb
2cc0cb0
200dcdb
3d52ed2
 
 
 
 
 
200dcdb
3d52ed2
 
200dcdb
2cc0cb0
 
200dcdb
2cc0cb0
200dcdb
2cc0cb0
 
200dcdb
 
2cbc17e
3d52ed2
d6d183b
200dcdb
54ed831
d6d183b
 
bcf08f8
3d52ed2
 
bcf08f8
c405013
2cbc17e
 
 
 
c405013
2cbc17e
 
 
bcf08f8
2cbc17e
bcf08f8
2cbc17e
bcf08f8
2cbc17e
d6d183b
2cbc17e
c405013
bcf08f8
3d52ed2
bcf08f8
 
d6d183b
2cbc17e
 
bcf08f8
c405013
bcf08f8
3d52ed2
 
 
 
 
 
 
2cbc17e
bcf08f8
3d52ed2
 
c405013
3d52ed2
 
 
 
 
c405013
3d52ed2
 
bcf08f8
2cbc17e
3d52ed2
2cbc17e
3d52ed2
bcf08f8
 
3d52ed2
 
2cbc17e
3d52ed2
2cbc17e
54ed831
c405013
bcf08f8
 
 
 
 
 
c405013
bcf08f8
 
 
 
018141a
9f93f0e
c405013
018141a
7a966d7
2cbc17e
6542f5d
2cbc17e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcf08f8
c405013
 
bcf08f8
c405013
018141a
bcf08f8
a87bc00
bcf08f8
2cbc17e
bcf08f8
 
2cbc17e
bcf08f8
884d940
c405013
 
de45d29
cbcdc2b
2cbc17e
029ffc9
2cbc17e
 
 
 
 
 
 
029ffc9
2cbc17e
 
 
 
c405013
2cbc17e
9f93f0e
2cbc17e
 
 
 
 
 
029ffc9
2cbc17e
 
 
 
 
 
 
 
 
 
 
 
bcf08f8
2cbc17e
 
 
 
 
029ffc9
 
 
bcf08f8
c405013
2cbc17e
 
 
 
 
 
 
 
 
029ffc9
bcf08f8
2cbc17e
029ffc9
 
bcf08f8
 
 
029ffc9
2cbc17e
c405013
2cbc17e
029ffc9
 
d6d183b
018141a
c405013
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
import random
import matplotlib.pyplot as plt
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
# from nltk.stem import WordNetLemmatizer # Not used, commented out
from nltk.text import Text
from nltk.probability import FreqDist
from cleantext import clean
# import textract # Replaced by PyPDF2
import PyPDF2 # Added for PDF parsing
import urllib.request
from io import BytesIO
import sys
import pandas as pd
# import cv2 # Not used, commented out
import re
from wordcloud import WordCloud # , ImageColorGenerator # ImageColorGenerator not used, commented out
from textblob import TextBlob
from PIL import Image
import os
import gradio as gr
from dotenv import load_dotenv
import groq
import json
import traceback
import numpy as np
import unidecode
import contractions
from sklearn.feature_extraction.text import TfidfVectorizer

# Load environment variables
load_dotenv()

# Inside your main script (e.g., near the top after imports)
import nltk
import ssl # Sometimes needed for NLTK downloads

def ensure_nltk_resources():
    try:
        # Try to find a resource to see if download is needed
        # Using punkt as an example; you might check others too
        nltk.data.find('tokenizers/punkt')
        nltk.data.find('corpora/stopwords')
        # Add checks for wordnet, words, punkt_tab as needed
    except LookupError:
        print("NLTK resources not found. Downloading...")
        try:
            # Handle potential SSL issues (common on some systems)
            _create_unverified_https_context = ssl._create_unverified_context
        except AttributeError:
            pass
        else:
            ssl._create_default_https_context = _create_unverified_https_context

        nltk.download(['stopwords', 'wordnet', 'words'])
        nltk.download('punkt')
        nltk.download('punkt_tab')
        print("NLTK resources downloaded successfully.")

# Call the function at the start of your script
ensure_nltk_resources()



# Initialize Groq client
groq_api_key = os.getenv("GROQ_API_KEY")
groq_client = groq.Groq(api_key=groq_api_key) if groq_api_key else None

# Stopwords customization
stop_words = set(stopwords.words('english'))
stop_words.update({'ask', 'much', 'thank', 'etc.', 'e', 'We', 'In', 'ed', 'pa', 'This', 'also', 'A', 'fu', 'To', '5', 'ing', 'er', '2'}) # Ensure stop_words is a set

# --- Parsing & Preprocessing Functions ---
# --- Replaced textract with PyPDF2 ---
def Parsing(parsed_text):
    """
    Parses text from a PDF file using PyPDF2.
    """
    try:
        # Get the file path from the Gradio UploadFile object
        if hasattr(parsed_text, 'name'):
            file_path = parsed_text.name
        else:
            # Fallback if it's somehow just a string path
            file_path = parsed_text

        # Use PyPDF2 to read the PDF
        text = ""
        with open(file_path, 'rb') as pdf_file: # Open in binary read mode
            pdf_reader = PyPDF2.PdfReader(pdf_file)
            for page_num in range(len(pdf_reader.pages)):
                page = pdf_reader.pages[page_num]
                text += page.extract_text() + "\n" # Add newline between pages

        # Clean the extracted text
        return clean(text)

    except FileNotFoundError:
        print(f"Error parsing PDF: File not found at path: {file_path}")
        return f"Error parsing PDF: File not found. Please check the file upload."
    except PyPDF2.errors.PdfReadError as pre:
        print(f"Error reading PDF: {pre}")
        return f"Error reading PDF: The file might be corrupted or password-protected."
    except Exception as e:
        print(f"Error parsing PDF: {e}")
        return f"Error parsing PDF: {e}"

def clean_text(text):
    text = text.encode("ascii", errors="ignore").decode("ascii")
    text = unidecode.unidecode(text)
    text = contractions.fix(text)
    text = re.sub(r"\n", " ", text)
    text = re.sub(r"\t", " ", text)
    text = re.sub(r"/ ", " ", text)
    text = text.strip()
    text = re.sub(" +", " ", text).strip()
    text = [word for word in text.split() if word not in stop_words]
    return ' '.join(text)

def Preprocess(textParty):
    text1Party = re.sub('[^A-Za-z0-9]+', ' ', textParty)
    pattern = re.compile(r'\b(' + r'|'.join(stopwords.words('english')) + r')\b\s*')
    text2Party = pattern.sub('', text1Party)
    return text2Party

# --- Core Analysis Functions ---
def generate_summary(text):
    if not groq_client:
        return "Summarization is not available. Please set up your GROQ_API_KEY in the .env file."
    # Adjusted truncation length for potentially better summary context
    if len(text) > 15000:
        text = text[:15000]
    try:
        completion = groq_client.chat.completions.create(
            model="llama3-8b-8192", # Or your preferred model
            messages=[
                {"role": "system", "content": "You are a helpful assistant that summarizes political manifestos. Provide a concise, objective summary that captures the key policy proposals, themes, and promises in the manifesto."},
                {"role": "user", "content": f"Please summarize the following political manifesto text in about 300-500 words, focusing on the main policy areas, promises, and themes:\n{text}"}
            ],
            temperature=0.3,
            max_tokens=800
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"Error generating summary: {str(e)}"

# --- New LLM-based Search Function ---
def get_contextual_search_result(target_word, tar_passage, groq_client_instance, max_context_length=8000):
    """
    Uses the LLM to provide contextual information about the target word within the passage.
    """
    if not target_word or target_word.strip() == "":
        return "Please enter a search term."

    if not groq_client_instance:
        return "Contextual search requires the LLM API. Please set up your GROQ_API_KEY."

    # Truncate passage if too long for the model/context window
    original_length = len(tar_passage)
    if original_length > max_context_length:
        tar_passage_truncated = tar_passage[:max_context_length]
        print(f"Warning: Passage truncated for LLM search context from {original_length} to {max_context_length} characters.")
    else:
        tar_passage_truncated = tar_passage

    # --- Improved Prompt ---
    prompt = f"""
You are an expert political analyst. You have been given a section of a political manifesto and a specific search term.
Your task is to extract and summarize all information related to the search term from the provided text.
Focus on:
1.  Specific policies, promises, or statements related to the term.
2.  The context in which the term is used.
3.  Any key details, figures, or commitments mentioned.
Present your findings concisely. If the term is not relevant or not found in the provided text section, state that clearly.
Search Term: {target_word}
Manifesto Text Section:
{tar_passage_truncated}
Relevant Information:
"""

    try:
        completion = groq_client_instance.chat.completions.create(
            model="llama3-8b-8192", # Use the same or a suitable model
            messages=[
                {"role": "system", "content": "You are a helpful assistant skilled at analyzing political texts and extracting relevant information based on a search query. Provide clear, concise summaries."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.2, # Low temperature for more factual extraction
            max_tokens=1000  # Adjust based on expected output length
        )
        result = completion.choices[0].message.content.strip()
        # Add a note if the input was truncated
        if original_length > max_context_length:
             result = f"(Note: Analysis based on the first {max_context_length} characters of the manifesto.)\n\n" + result
        return result if result else f"No specific context for '{target_word}' could be generated from the provided text section."
    except Exception as e:
        error_msg = f"Error during contextual search for '{target_word}': {str(e)}"
        print(error_msg)
        traceback.print_exc()
        return error_msg # Or return the error message directly

def fDistance(text2Party):
    word_tokens_party = word_tokenize(text2Party)
    fdistance = FreqDist(word_tokens_party).most_common(10)
    mem = {x[0]: x[1] for x in fdistance}
    vectorizer = TfidfVectorizer(max_features=15, stop_words='english')
    try:
        tfidf_matrix = vectorizer.fit_transform(sent_tokenize(text2Party))
        feature_names = vectorizer.get_feature_names_out()
        tfidf_scores = {}
        sentences = sent_tokenize(text2Party)
        for i, word in enumerate(feature_names):
            scores = []
            for j in range(tfidf_matrix.shape[0]): # Iterate through sentences
                 if i < tfidf_matrix.shape[1]: # Check if word index is valid for this sentence vector
                    scores.append(tfidf_matrix[j, i])
            if scores:
                tfidf_scores[word] = sum(scores) / len(scores) # Average TF-IDF score across sentences
        combined_scores = {}
        all_words = set(list(mem.keys()) + list(tfidf_scores.keys()))
        max_freq = max(mem.values()) if mem else 1
        max_tfidf = max(tfidf_scores.values()) if tfidf_scores else 1
        for word in all_words:
            freq_score = mem.get(word, 0) / max_freq
            tfidf_score = tfidf_scores.get(word, 0) / max_tfidf
            combined_scores[word] = (freq_score * 0.3) + (tfidf_score * 0.7)
        top_words = dict(sorted(combined_scores.items(), key=lambda x: x[1], reverse=True)[:10])
        return normalize(top_words)
    except ValueError as ve: # Handle case where TF-IDF fails (e.g., empty after processing)
        print(f"Warning: TF-IDF failed, using only frequency: {ve}")
        # Fallback to just normalized frequency if TF-IDF fails
        if mem:
             max_freq = max(mem.values())
             return {k: v / max_freq for k, v in list(mem.items())[:10]} # Return top 10 freq, normalized
        else:
             return {}

def normalize(d, target=1.0):
    raw = sum(d.values())
    factor = target / raw if raw != 0 else 0
    return {key: value * factor for key, value in d.items()}

# --- Visualization Functions with Error Handling ---
# --- Improved safe_plot to handle apply_aspect errors ---
def safe_plot(func, *args, **kwargs):
    """Executes a plotting function and returns the image, handling errors."""
    buf = None # Initialize buffer
    try:
        # Ensure a clean figure state
        fig = plt.figure() # Create a new figure explicitly
        func(*args, **kwargs)
        buf = BytesIO()
        # Try saving with bbox_inches, but catch potential apply_aspect error
        try:
            plt.savefig(buf, format='png', bbox_inches='tight')
        except AttributeError as ae:
            if "apply_aspect" in str(ae):
                print(f"Warning: bbox_inches='tight' failed ({ae}), saving without it.")
                buf.seek(0) # Reset buffer as it might be partially written
                buf = BytesIO() # Get a fresh buffer
                plt.savefig(buf, format='png') # Save without bbox_inches
            else:
                raise # Re-raise if it's a different AttributeError
        buf.seek(0)
        img = Image.open(buf)
        plt.close(fig) # Explicitly close the specific figure
        return img
    except Exception as e:
        print(f"Plotting error in safe_plot: {e}")
        if buf:
            buf.close() # Ensure buffer is closed on error if it was created
        traceback.print_exc()
        # Try to return a placeholder or None
        plt.close('all') # Aggressive close on error
        return None

def fDistancePlot(text2Party):
    def plot_func():
        tokens = word_tokenize(text2Party)
        if not tokens:
             plt.text(0.5, 0.5, "No data to plot", ha='center', va='center')
             return
        fdist = FreqDist(tokens)
        fdist.plot(15, title='Frequency Distribution')
        plt.xticks(rotation=45, ha='right') # Rotate x-axis labels if needed
        plt.tight_layout()
    return safe_plot(plot_func)

def DispersionPlot(textParty):
    """Generates the word dispersion plot."""
    buf = None # Initialize buffer
    try:
        word_tokens_party = word_tokenize(textParty)
        print(f"Debug DispersionPlot: Total tokens: {len(word_tokens_party)}") # Debug print
        if not word_tokens_party:
            print("Warning: No tokens found for dispersion plot.")
            return None

        moby = Text(word_tokens_party)
        fdistance = FreqDist(word_tokens_party)
        print(f"Debug DispersionPlot: FreqDist sample: {list(fdistance.most_common(10))}") # Debug print

        # --- Improved word selection logic ---
        # Get common words, excluding very short words which might be punctuation/non-informative
        # or words that dispersion_plot might have trouble with.
        common_words_raw = fdistance.most_common(15) # Check a few more common words
        # Filter: length > 2, isalpha (to avoid punctuation), not just digits
        common_words_filtered = [(word, freq) for word, freq in common_words_raw if len(word) > 2 and word.isalpha() and not word.isdigit()]
        print(f"Debug DispersionPlot: Filtered common words: {common_words_filtered}") # Debug print

        # Select top 5 from filtered list
        if len(common_words_filtered) < 5:
            word_Lst = [word for word, _ in common_words_filtered]
        else:
            word_Lst = [common_words_filtered[x][0] for x in range(5)]

        # Final check: Ensure words are present in the Text object (moby)
        # This is crucial for dispersion_plot
        final_word_list = [word for word in word_Lst if word in moby] # Check membership in the Text object
        print(f"Debug DispersionPlot: Final word list for plot: {final_word_list}") # Debug print

        if not final_word_list:
            print("Warning: No suitable words found for dispersion plot after filtering and checking membership.")
            # Create a simple plot indicating no data
            fig, ax = plt.subplots(figsize=(8, 3))
            ax.text(0.5, 0.5, "No suitable words found for dispersion plot", ha='center', va='center', transform=ax.transAxes)
            ax.set_xlim(0, 1)
            ax.set_ylim(0, 1)
            ax.axis('off') # Hide axes for the message
            fig.suptitle('Dispersion Plot')
        else:
            # --- Manage figure explicitly without passing 'ax' ---
            fig = plt.figure(figsize=(10, 5)) # Create figure explicitly
            plt.title('Dispersion Plot')
            # Call dispersion_plot with the verified word list
            moby.dispersion_plot(final_word_list)
            plt.tight_layout()

        buf = BytesIO()
        # Handle potential apply_aspect error for dispersion plot
        try:
            fig.savefig(buf, format='png', bbox_inches='tight')
        except AttributeError as ae:
            if "apply_aspect" in str(ae):
                 print(f"Warning: bbox_inches='tight' failed for Dispersion Plot ({ae}), saving without it.")
                 buf.seek(0)
                 buf = BytesIO() # Get a fresh buffer
                 fig.savefig(buf, format='png')
            else:
                 raise # Re-raise if it's a different AttributeError
        buf.seek(0)
        img = Image.open(buf)
        plt.close(fig) # Close the specific figure created
        return img

    except Exception as e:
        print(f"Dispersion plot error: {e}")
        if buf:
            buf.close() # Ensure buffer is closed on error
        traceback.print_exc()
        plt.close('all') # Aggressive close on error
        # Optionally return a placeholder image or None
        return None # Return None on error



def word_cloud_generator(parsed_text_name, text_Party):
    """Generates the word cloud image."""
    buf = None # Initialize buffer
    try:
        # Handle case where parsed_text_name might not have .name
        filename_lower = ""
        if hasattr(parsed_text_name, 'name') and parsed_text_name.name:
            filename_lower = parsed_text_name.name.lower()
        elif isinstance(parsed_text_name, str):
             filename_lower = parsed_text_name.lower()

        mask_path = None
        if 'bjp' in filename_lower:
            mask_path = 'bjpImg2.jpeg'
        elif 'congress' in filename_lower:
            mask_path = 'congress3.jpeg'
        elif 'aap' in filename_lower:
            mask_path = 'aapMain2.jpg'


        if text_Party.strip() == "":
             raise ValueError("Text for word cloud is empty")

        # Generate word cloud object
        if mask_path and os.path.exists(mask_path):
            orgImg = Image.open(mask_path)

            if orgImg.mode != 'RGB':
                orgImg = orgImg.convert('RGB')
            mask = np.array(orgImg)
            wordcloud = WordCloud(max_words=3000, mask=mask, background_color='white', mode='RGBA').generate(text_Party) # Added mode='RGBA'
        else:
            wordcloud = WordCloud(max_words=2000, background_color='white', mode='RGBA').generate(text_Party)

        # --- Key Fix: Explicitly manage figure and axes for word cloud ---
        fig, ax = plt.subplots(figsize=(8, 6)) # Create new figure and axes
        ax.imshow(wordcloud, interpolation='bilinear')
        ax.axis("off")
        fig.tight_layout(pad=0) # Remove padding

        buf = BytesIO()
        # Handle potential apply_aspect error for word cloud too
        try:
             fig.savefig(buf, format='png', bbox_inches='tight', dpi=150, facecolor='white') # Added dpi and facecolor
        except AttributeError as ae:
            if "apply_aspect" in str(ae):
                 print(f"Warning: bbox_inches='tight' failed for Word Cloud ({ae}), saving without it.")
                 buf.seek(0)
                 buf = BytesIO()
                 fig.savefig(buf, format='png', dpi=150, facecolor='white')
            else:
                 raise
        buf.seek(0)
        img = Image.open(buf)
        plt.close(fig) # Close the specific figure
        return img

    except Exception as e:
        print(f"Word cloud error: {e}")
        if buf:
            buf.close() # Ensure buffer is closed on error
        traceback.print_exc()
        plt.close('all') # Aggressive close on error
        return None # Return None on error

# --- Main Analysis Function ---
def analysis(Manifesto, Search):
    try:
        if Manifesto is None:
            return "No file uploaded", {}, None, None, None, None, None, "No file uploaded"
        if Search.strip() == "":
            Search = "government"
        raw_party = Parsing(Manifesto) # Uses PyPDF2 now
        if isinstance(raw_party, str) and raw_party.startswith("Error"):
            return raw_party, {}, None, None, None, None, None, "Parsing failed"
        text_Party = clean_text(raw_party)
        text_Party_processed = Preprocess(text_Party)

        # --- Perform Search FIRST using the ORIGINAL text for better context ---
        # Use the new LLM-based search function
        searChRes = get_contextual_search_result(Search, raw_party, groq_client)

        summary = generate_summary(raw_party) # Use raw_party for summary for more context?

        # --- Sentiment Analysis ---
        if not text_Party_processed.strip():
             # Handle empty text after processing
             df_dummy = pd.DataFrame({'Polarity_Label': ['Neutral'], 'Subjectivity_Label': ['Low']})
             polarity_val = 0.0
             subjectivity_val = 0.0
        else:
            polarity_val = TextBlob(text_Party_processed).sentiment.polarity
            subjectivity_val = TextBlob(text_Party_processed).sentiment.subjectivity
            polarity_label = 'Positive' if polarity_val > 0 else 'Negative' if polarity_val < 0 else 'Neutral'
            subjectivity_label = 'High' if subjectivity_val > 0.5 else 'Low'
            df_dummy = pd.DataFrame({'Polarity_Label': [polarity_label], 'Subjectivity_Label': [subjectivity_label]})

        # --- Generate Plots with Safe Plotting ---
        # Pass the potentially empty text and handle inside plotting functions
        sentiment_plot = safe_plot(lambda: df_dummy['Polarity_Label'].value_counts().plot(kind='bar', color="#FF9F45", title='Sentiment Analysis'))
        subjectivity_plot = safe_plot(lambda: df_dummy['Subjectivity_Label'].value_counts().plot(kind='bar', color="#B667F1", title='Subjectivity Analysis'))
        freq_plot = fDistancePlot(text_Party_processed)
        dispersion_plot = DispersionPlot(text_Party_processed) # Uses updated version
        wordcloud = word_cloud_generator(Manifesto, text_Party_processed) # Pass Manifesto object itself, uses updated version
        fdist_Party = fDistance(text_Party_processed)
        # searChRes is now generated earlier using LLM

        return searChRes, fdist_Party, sentiment_plot, subjectivity_plot, wordcloud, freq_plot, dispersion_plot, summary

    except Exception as e:
        error_msg = f"Critical error in analysis function: {str(e)}"
        print(error_msg)
        traceback.print_exc()
        # Return error messages/images in the correct order
        return error_msg, {}, None, None, None, None, None, "Analysis failed"

# --- Gradio Interface ---
# Use Blocks for custom layout
with gr.Blocks(title='Manifesto Analysis') as demo:
    gr.Markdown("# Manifesto Analysis")
    # Input Section
    with gr.Row():
        with gr.Column(scale=1): # Adjust scale if needed
             file_input = gr.File(label="Upload Manifesto PDF", file_types=[".pdf"])
        with gr.Column(scale=1):
             search_input = gr.Textbox(label="Search Term", placeholder="Enter a term to search in the manifesto")
             submit_btn = gr.Button("Analyze Manifesto", variant='primary') # Make button prominent

    # Output Section using Tabs
    with gr.Tabs():
        # --- Summary Tab ---
        with gr.TabItem("Summary"):
            summary_output = gr.Textbox(label='AI-Generated Summary', lines=10, interactive=False)

        # --- Search Results Tab (uses LLM output now) ---
        with gr.TabItem("Search Results"):
            search_output = gr.Textbox(label='Context Based Search Results', lines=15, interactive=False, max_lines=20) # Increased lines/max_lines

        # --- Key Topics Tab ---
        with gr.TabItem("Key Topics"):
             topics_output = gr.Label(label="Most Relevant Topics (LLM Enhanced)", num_top_classes=10) # Show top 10

        # --- Visualizations Tab ---
        with gr.TabItem("Visualizations"):
            # Use Rows and Columns for better arrangement
            with gr.Row(): # Row 1: Sentiment & Subjectivity
                with gr.Column():
                    sentiment_output = gr.Image(label='Sentiment Analysis', interactive=False, height=400) # Set height
                with gr.Column():
                    subjectivity_output = gr.Image(label='Subjectivity Analysis', interactive=False, height=400)

            with gr.Row(): # Row 2: Word Cloud & Frequency
                with gr.Column():
                    wordcloud_output = gr.Image(label='Word Cloud', interactive=False, height=400)
                with gr.Column():
                    freq_output = gr.Image(label='Frequency Distribution', interactive=False, height=400)

            with gr.Row(): # Row 3: Dispersion Plot (Full width)
                with gr.Column():
                    dispersion_output = gr.Image(label='Dispersion Plot', interactive=False, height=400) # Adjust height as needed

    # --- Link Button Click to Function and Outputs ---
    submit_btn.click(
        fn=analysis,
        inputs=[file_input, search_input],
        outputs=[
            search_output,        # 1 (Now contextual LLM output)
            topics_output,        # 2
            sentiment_output,     # 3
            subjectivity_output,  # 4
            wordcloud_output,     # 5
            freq_output,          # 6
            dispersion_output,    # 7
            summary_output        # 8
        ],
        concurrency_limit=1   # Limit concurrent analyses if needed
    )

    # --- Examples ---
    gr.Examples(
        examples=[
            ["Example/AAP_Manifesto_2019.pdf", "government"],
            ["Example/Bjp_Manifesto_2019.pdf", "environment"],
            ["Example/Congress_Manifesto_2019.pdf", "safety"]
        ],
        inputs=[file_input, search_input],
        outputs=[search_output, topics_output, sentiment_output, subjectivity_output, wordcloud_output, freq_output, dispersion_output, summary_output], # Link examples to outputs
        fn=analysis # Run analysis on example click
    )


if __name__ == "__main__":
    demo.launch(debug=True, share=False, show_error=True)