Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from diff_match_patch import diff_match_patch
|
3 |
+
import re
|
4 |
+
|
5 |
+
def main():
|
6 |
+
st.set_page_config(page_title="Prompt Comparison Tool", layout="wide")
|
7 |
+
st.title("Prompt Comparison Tool")
|
8 |
+
|
9 |
+
# Initialize session state
|
10 |
+
if 'old_text' not in st.session_state:
|
11 |
+
st.session_state.old_text = ""
|
12 |
+
if 'new_text' not in st.session_state:
|
13 |
+
st.session_state.new_text = ""
|
14 |
+
if 'diff_html' not in st.session_state:
|
15 |
+
st.session_state.diff_html = ""
|
16 |
+
|
17 |
+
# Create two columns for text areas
|
18 |
+
col1, col2 = st.columns(2)
|
19 |
+
|
20 |
+
with col1:
|
21 |
+
st.session_state.old_text = st.text_area(
|
22 |
+
"Old Prompt:",
|
23 |
+
height=300,
|
24 |
+
value=st.session_state.old_text,
|
25 |
+
placeholder="Enter original text..."
|
26 |
+
)
|
27 |
+
|
28 |
+
with col2:
|
29 |
+
st.session_state.new_text = st.text_area(
|
30 |
+
"New Prompt:",
|
31 |
+
height=300,
|
32 |
+
value=st.session_state.new_text,
|
33 |
+
placeholder="Enter modified text..."
|
34 |
+
)
|
35 |
+
|
36 |
+
# Button controls
|
37 |
+
col_compare, col_clear, col_copy = st.columns([1, 1, 1])
|
38 |
+
|
39 |
+
with col_compare:
|
40 |
+
if st.button("Compare"):
|
41 |
+
dmp = diff_match_patch()
|
42 |
+
diffs = dmp.diff_main(st.session_state.old_text, st.session_state.new_text)
|
43 |
+
dmp.diff_cleanupSemantic(diffs)
|
44 |
+
|
45 |
+
html = []
|
46 |
+
for op, text in diffs:
|
47 |
+
text = text.replace("\n", "<br>") # Preserve newlines in HTML
|
48 |
+
if op == 1: # Insertion
|
49 |
+
html.append(f'<span style="background-color: #c8e6c9; border-radius: 4px; padding: 2px 4px;">{text}</span>')
|
50 |
+
elif op == -1: # Deletion
|
51 |
+
html.append(f'<span style="background-color: #ffcdd2; text-decoration: line-through; border-radius: 4px; padding: 2px 4px;">{text}</span>')
|
52 |
+
else: # No change
|
53 |
+
html.append(text)
|
54 |
+
|
55 |
+
st.session_state.diff_html = "".join(html)
|
56 |
+
|
57 |
+
with col_clear:
|
58 |
+
if st.button("Clear"):
|
59 |
+
st.session_state.old_text = ""
|
60 |
+
st.session_state.new_text = ""
|
61 |
+
st.session_state.diff_html = ""
|
62 |
+
st.rerun()
|
63 |
+
|
64 |
+
# Display comparison results
|
65 |
+
if st.session_state.diff_html:
|
66 |
+
st.markdown("### Comparison Result:")
|
67 |
+
st.markdown(st.session_state.diff_html, unsafe_allow_html=True)
|
68 |
+
|
69 |
+
# Copy functionality
|
70 |
+
with col_copy:
|
71 |
+
if st.button("Copy Result"):
|
72 |
+
if st.session_state.diff_html:
|
73 |
+
# Remove HTML tags and convert <br> to newlines
|
74 |
+
clean_text = re.sub(r'<br>', '\n', st.session_state.diff_html)
|
75 |
+
clean_text = re.sub(r'<[^>]+>', '', clean_text)
|
76 |
+
|
77 |
+
try:
|
78 |
+
import pyperclip
|
79 |
+
pyperclip.copy(clean_text)
|
80 |
+
st.success("Result copied to clipboard!")
|
81 |
+
except:
|
82 |
+
st.error("Clipboard access requires extra permissions. Please copy manually:")
|
83 |
+
st.code(clean_text)
|
84 |
+
else:
|
85 |
+
st.warning("No comparison result to copy")
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
main()
|