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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
from markitdown import MarkItDown
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from markitdown import MarkItDown
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# Initialize MarkItDown
|
| 9 |
+
md = MarkItDown()
|
| 10 |
+
|
| 11 |
+
# Configure Gemini AI
|
| 12 |
+
genai.configure(api_key=os.getenv('GEMINI_KEY'))
|
| 13 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 14 |
+
|
| 15 |
+
def process_with_markitdown(input_path):
|
| 16 |
+
"""Process file or URL with MarkItDown and return text content"""
|
| 17 |
+
try:
|
| 18 |
+
result = md.convert(input_path)
|
| 19 |
+
return result.text_content
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error processing input: {str(e)}"
|
| 22 |
+
|
| 23 |
+
def save_uploaded_file(file):
|
| 24 |
+
"""Save uploaded file to temporary location and return path"""
|
| 25 |
+
if file is None:
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
temp_dir = tempfile.gettempdir()
|
| 30 |
+
temp_path = os.path.join(temp_dir, file.name)
|
| 31 |
+
|
| 32 |
+
with open(temp_path, 'wb') as f:
|
| 33 |
+
f.write(file.read())
|
| 34 |
+
|
| 35 |
+
return temp_path
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error saving file: {str(e)}"
|
| 38 |
+
|
| 39 |
+
async def summarize_text(text):
|
| 40 |
+
"""Summarize the input text using Gemini AI"""
|
| 41 |
+
try:
|
| 42 |
+
prompt = f"""Please provide a concise summary of the following text. Focus on the main points and key takeaways:
|
| 43 |
+
|
| 44 |
+
{text}
|
| 45 |
+
|
| 46 |
+
Summary:"""
|
| 47 |
+
|
| 48 |
+
response = await model.generate_content_async(prompt)
|
| 49 |
+
return response.text
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error generating summary: {str(e)}"
|
| 52 |
+
|
| 53 |
+
async def process_input(input_text, uploaded_file=None):
|
| 54 |
+
"""Main function to process either URL or uploaded file"""
|
| 55 |
+
try:
|
| 56 |
+
if uploaded_file is not None:
|
| 57 |
+
# Handle file upload
|
| 58 |
+
temp_path = save_uploaded_file(uploaded_file)
|
| 59 |
+
if temp_path.startswith('Error'):
|
| 60 |
+
return temp_path
|
| 61 |
+
|
| 62 |
+
text = process_with_markitdown(temp_path)
|
| 63 |
+
|
| 64 |
+
# Clean up temporary file
|
| 65 |
+
try:
|
| 66 |
+
os.remove(temp_path)
|
| 67 |
+
except:
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
elif input_text.startswith(('http://', 'https://')):
|
| 71 |
+
# Handle URL
|
| 72 |
+
text = process_with_markitdown(input_text)
|
| 73 |
+
|
| 74 |
+
else:
|
| 75 |
+
# Handle direct text input
|
| 76 |
+
text = input_text
|
| 77 |
+
|
| 78 |
+
if text.startswith('Error'):
|
| 79 |
+
return text
|
| 80 |
+
|
| 81 |
+
# Generate summary using Gemini AI
|
| 82 |
+
summary = await summarize_text(text)
|
| 83 |
+
return summary
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"Error processing input: {str(e)}"
|
| 87 |
+
|
| 88 |
+
# Create Gradio interface with drag-and-drop
|
| 89 |
+
with gr.Blocks(css="""
|
| 90 |
+
.upload-box {
|
| 91 |
+
border: 2px dashed #ccc;
|
| 92 |
+
border-radius: 8px;
|
| 93 |
+
padding: 20px;
|
| 94 |
+
text-align: center;
|
| 95 |
+
transition: border-color 0.3s ease;
|
| 96 |
+
}
|
| 97 |
+
.upload-box:hover, .upload-box.dragover {
|
| 98 |
+
border-color: #666;
|
| 99 |
+
}
|
| 100 |
+
""") as iface:
|
| 101 |
+
gr.Markdown("# Text Summarization Tool")
|
| 102 |
+
gr.Markdown("Enter a URL, paste text, or drag & drop a file to get a summary.")
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
input_text = gr.Textbox(
|
| 106 |
+
label="Enter URL or text",
|
| 107 |
+
placeholder="Enter a URL or paste text here...",
|
| 108 |
+
scale=2
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
with gr.Row():
|
| 112 |
+
file_upload = gr.File(
|
| 113 |
+
label="Drop files here or click to upload",
|
| 114 |
+
file_types=[
|
| 115 |
+
".pdf", ".docx", ".xlsx", ".csv", ".txt", ".md",
|
| 116 |
+
".html", ".htm", ".xml", ".json"
|
| 117 |
+
],
|
| 118 |
+
file_count="single",
|
| 119 |
+
scale=2,
|
| 120 |
+
elem_classes=["upload-box"]
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
submit_btn = gr.Button("Summarize", variant="primary")
|
| 125 |
+
clear_btn = gr.Button("Clear")
|
| 126 |
+
|
| 127 |
+
output_text = gr.Textbox(label="Summary", lines=10)
|
| 128 |
+
|
| 129 |
+
# Add JavaScript for drag-and-drop highlighting
|
| 130 |
+
gr.JS("""
|
| 131 |
+
function setupDragDrop() {
|
| 132 |
+
const uploadBox = document.querySelector('.upload-box');
|
| 133 |
+
|
| 134 |
+
['dragenter', 'dragover'].forEach(eventName => {
|
| 135 |
+
uploadBox.addEventListener(eventName, (e) => {
|
| 136 |
+
e.preventDefault();
|
| 137 |
+
uploadBox.classList.add('dragover');
|
| 138 |
+
});
|
| 139 |
+
});
|
| 140 |
+
|
| 141 |
+
['dragleave', 'drop'].forEach(eventName => {
|
| 142 |
+
uploadBox.addEventListener(eventName, (e) => {
|
| 143 |
+
e.preventDefault();
|
| 144 |
+
uploadBox.classList.remove('dragover');
|
| 145 |
+
});
|
| 146 |
+
});
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// Call setup when the page loads
|
| 150 |
+
if (document.readyState === 'complete') {
|
| 151 |
+
setupDragDrop();
|
| 152 |
+
} else {
|
| 153 |
+
window.addEventListener('load', setupDragDrop);
|
| 154 |
+
}
|
| 155 |
+
""")
|
| 156 |
+
|
| 157 |
+
# Set up event handlers
|
| 158 |
+
submit_btn.click(
|
| 159 |
+
fn=process_input,
|
| 160 |
+
inputs=[input_text, file_upload],
|
| 161 |
+
outputs=output_text
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
clear_btn.click(
|
| 165 |
+
fn=lambda: (None, None, ""),
|
| 166 |
+
inputs=None,
|
| 167 |
+
outputs=[input_text, file_upload, output_text]
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Add examples
|
| 171 |
+
gr.Examples(
|
| 172 |
+
examples=[
|
| 173 |
+
["https://example.com/article"],
|
| 174 |
+
["This is a sample text that needs to be summarized..."],
|
| 175 |
+
],
|
| 176 |
+
inputs=input_text genai.configure(api_key=os.getenv('GEMINI_KEY'))
|
| 177 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
iface.launch()
|
| 182 |
+
|
| 183 |
import gradio as gr
|
| 184 |
import google.generativeai as genai
|
| 185 |
from markitdown import MarkItDown
|