import gradio as gr import google.generativeai as genai from markitdown import MarkItDown from typing import Union import os def extract_content(input_source: str) -> str: """Extract text content using MarkItDown""" try: md = MarkItDown() result = md.convert(input_source) return result.text_content except Exception as e: raise ValueError(f"Error processing {input_source}: {str(e)}") def summarize_content(content: str, api_key: str) -> str: """Summarize text using Gemini Flash""" genai.configure(api_key=os.getenv('GEMINI_KEY')) model = genai.GenerativeModel('gemini-1.5-flash') response = model.generate_content( f"Create a structured summary with key points using bullet points. " f"Focus on main ideas and important details:\n\n{content}" ) return response.text def process_input(api_key: str, url: str = None, file: str = None) -> str: """Handle both URL and file inputs""" try: if url: text = extract_content(url) elif file: text = extract_content(file) else: return "⚠️ Please provide either a URL or upload a file" if not text.strip(): return "❌ Error: No text could be extracted from the provided input" return summarize_content(text, api_key) except Exception as e: return f"❌ Processing Error: {str(e)}" # Gradio interface with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("""# 📄 Smart Content Summarizer **Powered by Gemini Flash & Microsoft Markitdown**""") with gr.Row(): with gr.Tab("🌐 URL Input"): url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com/article") url_submit = gr.Button("Summarize URL", variant="primary") with gr.Tab("📁 File Upload"): file_input = gr.File(label="Upload Document (PDF, DOCX, TXT)", file_types=[".pdf", ".docx", ".txt"]) file_submit = gr.Button("Summarize File", variant="primary") api_key_input = gr.Textbox(label="Google API Key", type="password", placeholder="Enter your API key here") output = gr.Markdown() url_submit.click( fn=process_input, inputs=[api_key_input, url_input, None], outputs=output ) file_submit.click( fn=process_input, inputs=[api_key_input, None, file_input], outputs=output ) if __name__ == "__main__": demo.launch()