Spaces:
Sleeping
Sleeping
Rick-Xu315
commited on
Commit
•
5832b95
1
Parent(s):
b1c855a
app
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import difflib
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
import numpy as np
|
10 |
+
import gradio as gr
|
11 |
+
import difflib
|
12 |
+
|
13 |
+
|
14 |
+
def word_lcs(text1, text2):
|
15 |
+
words1 = text1.split()
|
16 |
+
words2 = text2.split()
|
17 |
+
|
18 |
+
m, n = len(words1), len(words2)
|
19 |
+
|
20 |
+
# Initialize a 2D array to store the length of word-level LCS
|
21 |
+
dp = [[0] * (n + 1) for _ in range(m + 1)]
|
22 |
+
|
23 |
+
# Fill the dp array using bottom-up dynamic programming
|
24 |
+
for i in range(1, m + 1):
|
25 |
+
for j in range(1, n + 1):
|
26 |
+
if words1[i - 1] == words2[j - 1]:
|
27 |
+
dp[i][j] = dp[i - 1][j - 1] + 1
|
28 |
+
else:
|
29 |
+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
|
30 |
+
|
31 |
+
# Reconstruct the word-level LCS from the dp array
|
32 |
+
i, j = m, n
|
33 |
+
word_lcs = []
|
34 |
+
while i > 0 and j > 0:
|
35 |
+
if words1[i - 1] == words2[j - 1]:
|
36 |
+
word_lcs.append(words1[i - 1])
|
37 |
+
i -= 1
|
38 |
+
j -= 1
|
39 |
+
elif dp[i - 1][j] > dp[i][j - 1]:
|
40 |
+
i -= 1
|
41 |
+
else:
|
42 |
+
j -= 1
|
43 |
+
|
44 |
+
# Reverse the word-level LCS to get the correct order
|
45 |
+
word_lcs = word_lcs[::-1]
|
46 |
+
result=[]
|
47 |
+
for word in text2.split():
|
48 |
+
if word in word_lcs:
|
49 |
+
result.append((word+" ","-"))
|
50 |
+
word_lcs.remove(word)
|
51 |
+
else:
|
52 |
+
result.append((word,"+"))
|
53 |
+
|
54 |
+
#result=[(word+" ",None) for word in word_lcs]
|
55 |
+
|
56 |
+
return result
|
57 |
+
#定义处理函数
|
58 |
+
|
59 |
+
# 定义处理函数 f
|
60 |
+
def process(text1, text2,function):
|
61 |
+
# 这里替换成你的处理逻辑,生成图片的代码
|
62 |
+
# 示例:使用Matplotlib生成一个简单的图像
|
63 |
+
if function=="naive":
|
64 |
+
d = difflib.Differ()
|
65 |
+
return [
|
66 |
+
(token[2:], token[0] if token[0] != " " else None)
|
67 |
+
for token in d.compare(text1, text2)
|
68 |
+
]
|
69 |
+
elif function=="LCS":
|
70 |
+
return(word_lcs(text1,text2))
|
71 |
+
|
72 |
+
|
73 |
+
# 创建 Gradio 界面
|
74 |
+
iface = gr.Interface(
|
75 |
+
fn=process, # 此处的 fn 将在运行时动态设置
|
76 |
+
inputs=[
|
77 |
+
gr.Textbox(label="Origin Text",value="The quick brown fox jumped over the lazy dogs.",lines=3),
|
78 |
+
gr.Textbox(label="Edited Text",value="The fast brown fox jumps over lazy dogs.",lines=3),
|
79 |
+
gr.Radio(["naive", "Largest Common Subsequence"], label="Select Function")
|
80 |
+
],
|
81 |
+
outputs=gr.HighlightedText(
|
82 |
+
label="Diff",
|
83 |
+
combine_adjacent=True,
|
84 |
+
show_legend=True,
|
85 |
+
color_map={"+": "red", "-": "green"}),
|
86 |
+
theme=gr.themes.Base()
|
87 |
+
|
88 |
+
)
|
89 |
+
|
90 |
+
# 设置动态函数选择
|
91 |
+
|
92 |
+
# 启动 Gradio 应用
|
93 |
+
iface.launch()
|
94 |
+
|
95 |
+
|