Spaces:
Running
Running
feat: init0
Browse files
main.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from markitdown import MarkItDown
|
| 4 |
+
from typing import Union
|
| 5 |
+
|
| 6 |
+
def extract_content(input_source: str) -> str:
|
| 7 |
+
"""Extract text content using MarkItDown"""
|
| 8 |
+
try:
|
| 9 |
+
md = MarkItDown()
|
| 10 |
+
result = md.convert(input_source)
|
| 11 |
+
return result.text_content
|
| 12 |
+
except Exception as e:
|
| 13 |
+
raise ValueError(f"Error processing {input_source}: {str(e)}")
|
| 14 |
+
|
| 15 |
+
def summarize_content(content: str, api_key: str) -> str:
|
| 16 |
+
"""Summarize text using Gemini Flash"""
|
| 17 |
+
genai.configure(api_key=api_key)
|
| 18 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 19 |
+
response = model.generate_content(
|
| 20 |
+
f"Create a structured summary with key points using bullet points. "
|
| 21 |
+
f"Focus on main ideas and important details:\n\n{content}"
|
| 22 |
+
)
|
| 23 |
+
return response.text
|
| 24 |
+
|
| 25 |
+
def process_input(api_key: str, url: str = None, file: str = None) -> str:
|
| 26 |
+
"""Handle both URL and file inputs"""
|
| 27 |
+
try:
|
| 28 |
+
if url:
|
| 29 |
+
text = extract_content(url)
|
| 30 |
+
elif file:
|
| 31 |
+
text = extract_content(file)
|
| 32 |
+
else:
|
| 33 |
+
return "β οΈ Please provide either a URL or upload a file"
|
| 34 |
+
|
| 35 |
+
if not text.strip():
|
| 36 |
+
return "β Error: No text could be extracted from the provided input"
|
| 37 |
+
|
| 38 |
+
return summarize_content(text, api_key)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"β Processing Error: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 44 |
+
gr.Markdown("""# π Smart Content Summarizer
|
| 45 |
+
**Powered by Gemini Flash & Microsoft Markitdown**""")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Tab("π URL Input"):
|
| 49 |
+
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com/article")
|
| 50 |
+
url_submit = gr.Button("Summarize URL", variant="primary")
|
| 51 |
+
|
| 52 |
+
with gr.Tab("π File Upload"):
|
| 53 |
+
file_input = gr.File(label="Upload Document (PDF, DOCX, TXT)",
|
| 54 |
+
file_types=[".pdf", ".docx", ".txt"])
|
| 55 |
+
file_submit = gr.Button("Summarize File", variant="primary")
|
| 56 |
+
|
| 57 |
+
api_key_input = gr.Textbox(label="Google API Key", type="password",
|
| 58 |
+
placeholder="Enter your API key here")
|
| 59 |
+
output = gr.Markdown()
|
| 60 |
+
|
| 61 |
+
url_submit.click(
|
| 62 |
+
fn=process_input,
|
| 63 |
+
inputs=[api_key_input, url_input, None],
|
| 64 |
+
outputs=output
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
file_submit.click(
|
| 68 |
+
fn=process_input,
|
| 69 |
+
inputs=[api_key_input, None, file_input],
|
| 70 |
+
outputs=output
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|