Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,47 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import difflib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def compare_docs(old_text, new_text):
|
| 6 |
old_lines = old_text.splitlines()
|
| 7 |
new_lines = new_text.splitlines()
|
| 8 |
-
|
| 9 |
-
diff = difflib.unified_diff(old_lines, new_lines)
|
| 10 |
-
changes = list(diff)
|
| 11 |
-
|
| 12 |
-
added = [line for line in changes if line.startswith('+') and not line.startswith('+++')]
|
| 13 |
-
removed = [line for line in changes if line.startswith('-') and not line.startswith('---')]
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
iface = gr.Interface(
|
| 22 |
-
fn=
|
| 23 |
inputs=[
|
| 24 |
-
gr.
|
| 25 |
-
gr.
|
| 26 |
],
|
| 27 |
outputs="text",
|
| 28 |
-
title="Document
|
| 29 |
-
description="Upload
|
| 30 |
)
|
| 31 |
|
| 32 |
iface.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import difflib
|
| 4 |
+
from docx import Document
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def extract_text(file):
|
| 8 |
+
ext = os.path.splitext(file.name)[1]
|
| 9 |
+
if ext == ".txt":
|
| 10 |
+
return file.read().decode("utf-8")
|
| 11 |
+
elif ext == ".docx":
|
| 12 |
+
doc = Document(file)
|
| 13 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
| 14 |
+
else:
|
| 15 |
+
return "Unsupported file type. Please upload a .txt or .docx file."
|
| 16 |
+
|
| 17 |
+
def compare_documents(old_file, new_file):
|
| 18 |
+
old_text = extract_text(old_file)
|
| 19 |
+
new_text = extract_text(new_file)
|
| 20 |
|
|
|
|
| 21 |
old_lines = old_text.splitlines()
|
| 22 |
new_lines = new_text.splitlines()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
diff = list(difflib.unified_diff(old_lines, new_lines))
|
| 25 |
+
added = [line for line in diff if line.startswith('+') and not line.startswith('+++')]
|
| 26 |
+
removed = [line for line in diff if line.startswith('-') and not line.startswith('---')]
|
| 27 |
|
| 28 |
+
percent_change = (len(added) + len(removed)) / max(len(old_lines), 1) * 100
|
| 29 |
+
summary = f"๐ Updated Content: {percent_change:.2f}%\n\n"
|
| 30 |
+
summary += f"๐ผ Added: {len(added)} lines\n๐ฝ Removed: {len(removed)} lines\n\n"
|
| 31 |
+
preview = "\n".join(diff[:100]) or "No differences found."
|
| 32 |
+
|
| 33 |
+
return summary + preview
|
| 34 |
|
| 35 |
+
# Gradio Interface
|
| 36 |
iface = gr.Interface(
|
| 37 |
+
fn=compare_documents,
|
| 38 |
inputs=[
|
| 39 |
+
gr.File(label="Upload Old Document (.txt or .docx)"),
|
| 40 |
+
gr.File(label="Upload New Document (.txt or .docx)")
|
| 41 |
],
|
| 42 |
outputs="text",
|
| 43 |
+
title="๐ Document Version Comparator",
|
| 44 |
+
description="Upload two versions of a document to compare updates and get change percentage. Supports .txt and .docx files."
|
| 45 |
)
|
| 46 |
|
| 47 |
iface.launch()
|