File size: 2,584 Bytes
813c3b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import google.generativeai as genai
from markitdown import MarkItDown
from typing import Union

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=api_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()