Spaces:
Runtime error
Runtime error
| 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) | |