File size: 3,777 Bytes
bbc00e6
a2dfbcc
e8ee2b2
d30b786
3e4c3ad
6a69b65
 
 
 
a2dfbcc
e8ee2b2
bbc00e6
e8ee2b2
 
 
d30b786
e8ee2b2
 
 
 
 
 
d30b786
e8ee2b2
2fe2468
a2dfbcc
e8ee2b2
3e4c3ad
 
e8ee2b2
6a69b65
 
e8ee2b2
6a69b65
a2dfbcc
bbc00e6
a2dfbcc
e8ee2b2
a2dfbcc
 
e8ee2b2
 
 
bbc00e6
e8ee2b2
 
bbc00e6
a2dfbcc
3e4c3ad
 
6a69b65
 
3e4c3ad
 
e8ee2b2
a2dfbcc
e8ee2b2
bbc00e6
e8ee2b2
bbc00e6
a2dfbcc
 
 
3e4c3ad
a2dfbcc
 
 
 
 
 
e8ee2b2
a2dfbcc
 
 
 
e8ee2b2
a2dfbcc
 
 
3e4c3ad
a2dfbcc
 
 
 
 
 
 
 
 
 
 
 
e8ee2b2
a2dfbcc
 
2fe2468
e8ee2b2
bbc00e6
3e4c3ad
6a69b65
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
import gradio as gr
from transformers import pipeline
import PyPDF2

# ---------------- Load Hugging Face Pipelines ----------------
# Lightweight summarizer
summarizer = pipeline("summarization", model="sshleifer/distilbart-xsum-12-6")
# Instruction-tuned generator for eco tips
generator = pipeline("text2text-generation", model="google/flan-t5-small")

# ---------------- Core Functions ----------------
def extract_text_from_pdf(pdf_file):
    """Extract text from uploaded PDF file."""
    if pdf_file is None:
        return ""
    try:
        pdf_reader = PyPDF2.PdfReader(pdf_file)
        text = ""
        for page in pdf_reader.pages:
            if page.extract_text():
                text += page.extract_text() + "\n"
        return text
    except Exception as e:
        return f"Error reading PDF: {str(e)}"

def eco_tips_generator(problem_keywords):
    """Generate eco-friendly tips based on keywords."""
    if not problem_keywords.strip():
        return "Please enter some keywords (e.g., plastic, solar, energy saving)."
    prompt = (
        f"Give practical eco-friendly tips for sustainable living related to: {problem_keywords}. "
        f"Provide specific, clear, and actionable solutions."
    )
    result = generator(prompt, max_length=150)
    return result[0]["generated_text"]

def policy_summarization(pdf_file, policy_text):
    """Summarize either uploaded PDF or pasted text."""
    if pdf_file is not None:
        content = extract_text_from_pdf(pdf_file)
        text_to_summarize = content if content.strip() else policy_text
    else:
        text_to_summarize = policy_text

    if not text_to_summarize.strip():
        return "Please upload a PDF or paste policy text."

    try:
        summary = summarizer(
            text_to_summarize,
            max_length=120,
            min_length=30,
            do_sample=False
        )
        return summary[0]['summary_text']
    except Exception as e:
        return f"Error during summarization: {str(e)}"

# ---------------- Gradio Interface ----------------
with gr.Blocks() as app:
    gr.Markdown("# 🌍 Eco Assistant & Policy Analyzer")

    with gr.Tabs():
        # Eco Tips Generator
        with gr.TabItem("♻️ Eco Tips Generator"):
            with gr.Row():
                with gr.Column():
                    keywords_input = gr.Textbox(
                        label="Environmental Problem/Keywords",
                        placeholder="e.g., plastic, solar, water waste, energy saving...",
                        lines=3
                    )
                    generate_tips_btn = gr.Button("Generate Eco Tips")

                with gr.Column():
                    tips_output = gr.Textbox(label="Sustainable Living Tips", lines=15)

            generate_tips_btn.click(eco_tips_generator, inputs=keywords_input, outputs=tips_output)

        # Policy Summarization
        with gr.TabItem("📑 Policy Summarization"):
            with gr.Row():
                with gr.Column():
                    pdf_upload = gr.File(label="Upload Policy PDF", file_types=[".pdf"])
                    policy_text_input = gr.Textbox(
                        label="Or paste policy text here",
                        placeholder="Paste policy document text...",
                        lines=5
                    )
                    summarize_btn = gr.Button("Summarize Policy")

                with gr.Column():
                    summary_output = gr.Textbox(label="Policy Summary & Key Points", lines=20)

            summarize_btn.click(policy_summarization, inputs=[pdf_upload, policy_text_input], outputs=summary_output)

# ---------------- Run App ----------------
if __name__ == "__main__":
    app.launch(server_name="0.0.0.0", server_port=7860)