Deevyankar commited on
Commit
e17d93e
ยท
verified ยท
1 Parent(s): c751150

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
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
- percent_change = (len(added) + len(removed)) / max(len(old_lines), 1) * 100
16
- result = f"๐Ÿ“ˆ Content Updated: {percent_change:.2f}%\n\n"
17
- result += f"๐Ÿ”ผ Added Lines: {len(added)}\n๐Ÿ”ฝ Removed Lines: {len(removed)}\n\n"
18
 
19
- return result + "\n".join(changes[:100]) # show first 100 lines of diff
 
 
 
 
 
20
 
 
21
  iface = gr.Interface(
22
- fn=compare_docs,
23
  inputs=[
24
- gr.Textbox(label="Old Version", lines=20, placeholder="Paste old document text here..."),
25
- gr.Textbox(label="New Version", lines=20, placeholder="Paste new document text here...")
26
  ],
27
  outputs="text",
28
- title="Document Update Comparator",
29
- description="Upload or paste old and new version of a document to compare changes and calculate % updated."
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()