Compare diff text
Browse files
app.py
CHANGED
@@ -1,30 +1,38 @@
|
|
1 |
-
from
|
2 |
-
import gradio as gr
|
3 |
-
|
4 |
-
CUSTOM_PATH = "/gradio"
|
5 |
-
|
6 |
-
app = FastAPI()
|
7 |
-
|
8 |
-
|
9 |
-
@app.get("/")
|
10 |
-
def read_main():
|
11 |
-
return {"message": "This is your main app"}
|
12 |
|
|
|
13 |
|
14 |
-
io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
|
15 |
-
app = gr.mount_gradio_app(app, io, path=CUSTOM_PATH)
|
16 |
-
|
17 |
-
def greet(name):
|
18 |
-
return "Hello " + name + "!!"
|
19 |
-
|
20 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
21 |
-
iface.launch()
|
22 |
-
|
23 |
-
def echo(text, request: gr.Request):
|
24 |
-
if request:
|
25 |
-
print("Request headers dictionary:", request.headers)
|
26 |
-
print("IP address:", request.client.host)
|
27 |
-
print("Query parameters:", dict(request.query_params))
|
28 |
-
return text
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from difflib import Differ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import gradio as gr
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def diff_texts(text1, text2):
|
7 |
+
d = Differ()
|
8 |
+
return [
|
9 |
+
(token[2:], token[0] if token[0] != " " else None)
|
10 |
+
for token in d.compare(text1, text2)
|
11 |
+
]
|
12 |
+
|
13 |
+
|
14 |
+
demo = gr.Interface(
|
15 |
+
diff_texts,
|
16 |
+
[
|
17 |
+
gr.Textbox(
|
18 |
+
label="Text 1",
|
19 |
+
info="Initial text",
|
20 |
+
lines=3,
|
21 |
+
value="The quick brown fox jumped over the lazy dogs.",
|
22 |
+
),
|
23 |
+
gr.Textbox(
|
24 |
+
label="Text 2",
|
25 |
+
info="Text to compare",
|
26 |
+
lines=3,
|
27 |
+
value="The fast brown fox jumps over lazy dogs.",
|
28 |
+
),
|
29 |
+
],
|
30 |
+
gr.HighlightedText(
|
31 |
+
label="Diff",
|
32 |
+
combine_adjacent=True,
|
33 |
+
show_legend=True,
|
34 |
+
color_map={"+": "red", "-": "green"}),
|
35 |
+
theme=gr.themes.Base()
|
36 |
+
)
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.launch()
|