Spaces:
Sleeping
Sleeping
feat: update space styles
#2
by CoinW - opened
- app.py +62 -44
- relative_tester.py +5 -1
app.py
CHANGED
|
@@ -8,20 +8,23 @@ from relative_tester import RelativeTester
|
|
| 8 |
|
| 9 |
def run_test(input_text):
|
| 10 |
if not input_text:
|
| 11 |
-
return "
|
| 12 |
# return two_sample_tester.test(input_text.strip())
|
| 13 |
return relative_tester.test(input_text.strip())
|
| 14 |
return f"Prediction: Human (Mocked for {input_text})"
|
| 15 |
|
| 16 |
|
| 17 |
css = """
|
| 18 |
-
#header { text-align: center;
|
| 19 |
-
#
|
|
|
|
| 20 |
.links {
|
| 21 |
display: flex;
|
| 22 |
justify-content: flex-end;
|
| 23 |
-
gap:
|
| 24 |
margin-right: 10px;
|
|
|
|
|
|
|
| 25 |
align-items: center;
|
| 26 |
font-size: 0.9em;
|
| 27 |
color: #ADD8E6;
|
|
@@ -30,28 +33,18 @@ css = """
|
|
| 30 |
margin: 0 5px;
|
| 31 |
color: #000;
|
| 32 |
}
|
| 33 |
-
|
| 34 |
/* Adjusting layout for Input Text and Inference Result */
|
| 35 |
.input-row {
|
| 36 |
display: flex;
|
| 37 |
width: 100%;
|
| 38 |
}
|
| 39 |
-
|
| 40 |
.input-text {
|
| 41 |
flex: 3; /* 4 parts of the row */
|
| 42 |
margin-right: 1px;
|
| 43 |
border-radius: 8px;
|
| 44 |
padding: 12px;
|
| 45 |
-
border: 2px
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
.output-text {
|
| 49 |
-
flex: 1; /* 1 part of the row */
|
| 50 |
-
border-radius: 8px;
|
| 51 |
-
padding: 12px;
|
| 52 |
-
border: 2px soild #d1d1d1;
|
| 53 |
}
|
| 54 |
-
|
| 55 |
/* Set button widths to match the Select Model width */
|
| 56 |
.button {
|
| 57 |
width: 250px; /* Same as the select box width */
|
|
@@ -61,31 +54,24 @@ css = """
|
|
| 61 |
font-weight: bold;
|
| 62 |
border-radius: 8px;
|
| 63 |
}
|
| 64 |
-
|
| 65 |
.button:hover {
|
| 66 |
background-color: #0000FF;
|
| 67 |
}
|
| 68 |
-
|
| 69 |
/* Set height for the Select Model dropdown */
|
| 70 |
.select {
|
| 71 |
height: 100px; /* Set height to 100px */
|
| 72 |
}
|
| 73 |
-
|
| 74 |
/* Accordion Styling */
|
| 75 |
.accordion {
|
| 76 |
width: 100%; /* Set the width of the accordion to match the parent */
|
| 77 |
-
max-height:
|
| 78 |
-
overflow-y: auto; /* Allow scrolling if the content exceeds max height */
|
| 79 |
margin-bottom: 10px; /* Add space below accordion */
|
| 80 |
box-sizing: border-box; /* Ensure padding is included in width/height */
|
| 81 |
}
|
| 82 |
-
|
| 83 |
/* Accordion content max-height */
|
| 84 |
.accordion-content {
|
| 85 |
-
max-height:
|
| 86 |
-
overflow-y: auto; /* Add a scrollbar if content overflows */
|
| 87 |
}
|
| 88 |
-
|
| 89 |
.demo-banner {
|
| 90 |
background-color: #f3f4f6;
|
| 91 |
padding: 20px;
|
|
@@ -96,12 +82,35 @@ css = """
|
|
| 96 |
margin-bottom: 20px;
|
| 97 |
color: #ff5722;
|
| 98 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
"""
|
| 100 |
|
| 101 |
# Gradio App
|
| 102 |
with gr.Blocks(css=css) as app:
|
| 103 |
with gr.Row():
|
| 104 |
-
gr.HTML(
|
|
|
|
|
|
|
| 105 |
with gr.Row():
|
| 106 |
gr.HTML(
|
| 107 |
"""
|
|
@@ -110,14 +119,14 @@ with gr.Blocks(css=css) as app:
|
|
| 110 |
<span class="separator">|</span>
|
| 111 |
<a href="https://github.com/xLearn-AU/R-Detect" target="_blank">Code</a>
|
| 112 |
<span class="separator">|</span>
|
| 113 |
-
<a href="mailto:
|
| 114 |
</div>
|
| 115 |
"""
|
| 116 |
)
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
gr.HTML(
|
| 120 |
-
'<div class="demo-banner">This is a demo. For the full version, please refer to the <a href="https://github.com/xLearn-AU/R-Detect" target="_blank">GitHub</a> or the <a href="https://openreview.net/forum?id=z9j7wctoGV" target="_blank">Paper</a>.</div>'
|
| 121 |
)
|
| 122 |
|
| 123 |
with gr.Row():
|
|
@@ -132,38 +141,47 @@ with gr.Blocks(css=css) as app:
|
|
| 132 |
label="Inference Result",
|
| 133 |
placeholder="Made by Human or AI",
|
| 134 |
elem_id="output-text",
|
| 135 |
-
lines=
|
| 136 |
-
|
| 137 |
)
|
|
|
|
| 138 |
with gr.Row():
|
| 139 |
submit_button = gr.Button(
|
| 140 |
"Run Detection", variant="primary", elem_classes=["button"]
|
| 141 |
)
|
| 142 |
clear_button = gr.Button("Clear", variant="secondary", elem_classes=["button"])
|
| 143 |
|
| 144 |
-
submit_button.click(run_test, inputs=[input_text], outputs=output)
|
| 145 |
clear_button.click(lambda: ("", ""), inputs=[], outputs=[input_text, output])
|
| 146 |
|
| 147 |
-
with gr.Accordion("Disclaimer", open=
|
| 148 |
gr.Markdown(
|
| 149 |
"""
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
)
|
| 154 |
|
| 155 |
-
with gr.Accordion("
|
| 156 |
gr.Markdown(
|
| 157 |
"""
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
|
|
|
|
| 8 |
|
| 9 |
def run_test(input_text):
|
| 10 |
if not input_text:
|
| 11 |
+
return "Please enter some text to test."
|
| 12 |
# return two_sample_tester.test(input_text.strip())
|
| 13 |
return relative_tester.test(input_text.strip())
|
| 14 |
return f"Prediction: Human (Mocked for {input_text})"
|
| 15 |
|
| 16 |
|
| 17 |
css = """
|
| 18 |
+
#header { text-align: center; margin-bottom: 5px; color: #black; font-weight: bold; font-weight: bold;}
|
| 19 |
+
#header_bigger { font-size: 2.5em; }
|
| 20 |
+
#header_smaller { font-size: 2em; }
|
| 21 |
.links {
|
| 22 |
display: flex;
|
| 23 |
justify-content: flex-end;
|
| 24 |
+
gap: 5px;
|
| 25 |
margin-right: 10px;
|
| 26 |
+
margin-top: -10px;
|
| 27 |
+
margin-bottom: -20px !important;
|
| 28 |
align-items: center;
|
| 29 |
font-size: 0.9em;
|
| 30 |
color: #ADD8E6;
|
|
|
|
| 33 |
margin: 0 5px;
|
| 34 |
color: #000;
|
| 35 |
}
|
|
|
|
| 36 |
/* Adjusting layout for Input Text and Inference Result */
|
| 37 |
.input-row {
|
| 38 |
display: flex;
|
| 39 |
width: 100%;
|
| 40 |
}
|
|
|
|
| 41 |
.input-text {
|
| 42 |
flex: 3; /* 4 parts of the row */
|
| 43 |
margin-right: 1px;
|
| 44 |
border-radius: 8px;
|
| 45 |
padding: 12px;
|
| 46 |
+
border: 2px solid #d1d1d1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
|
|
|
| 48 |
/* Set button widths to match the Select Model width */
|
| 49 |
.button {
|
| 50 |
width: 250px; /* Same as the select box width */
|
|
|
|
| 54 |
font-weight: bold;
|
| 55 |
border-radius: 8px;
|
| 56 |
}
|
|
|
|
| 57 |
.button:hover {
|
| 58 |
background-color: #0000FF;
|
| 59 |
}
|
|
|
|
| 60 |
/* Set height for the Select Model dropdown */
|
| 61 |
.select {
|
| 62 |
height: 100px; /* Set height to 100px */
|
| 63 |
}
|
|
|
|
| 64 |
/* Accordion Styling */
|
| 65 |
.accordion {
|
| 66 |
width: 100%; /* Set the width of the accordion to match the parent */
|
| 67 |
+
max-height: auto; /* Set a auto-height for accordion */
|
|
|
|
| 68 |
margin-bottom: 10px; /* Add space below accordion */
|
| 69 |
box-sizing: border-box; /* Ensure padding is included in width/height */
|
| 70 |
}
|
|
|
|
| 71 |
/* Accordion content max-height */
|
| 72 |
.accordion-content {
|
| 73 |
+
max-height: auto; /* auto the height of the content */
|
|
|
|
| 74 |
}
|
|
|
|
| 75 |
.demo-banner {
|
| 76 |
background-color: #f3f4f6;
|
| 77 |
padding: 20px;
|
|
|
|
| 82 |
margin-bottom: 20px;
|
| 83 |
color: #ff5722;
|
| 84 |
}
|
| 85 |
+
/* Green for Human text */
|
| 86 |
+
.highlighted-human {
|
| 87 |
+
background-color: #d4edda;
|
| 88 |
+
color: #155724;
|
| 89 |
+
border: 2px solid #28a745;
|
| 90 |
+
}
|
| 91 |
+
/* Red for AI text */
|
| 92 |
+
.highlighted-ai {
|
| 93 |
+
background-color: #f8d7da;
|
| 94 |
+
color: #721c24;
|
| 95 |
+
border: 2px solid #dc3545;
|
| 96 |
+
}
|
| 97 |
+
/* Yellow for errors */
|
| 98 |
+
.highlighted-error {
|
| 99 |
+
background-color: #fff3cd;
|
| 100 |
+
color: #856404;
|
| 101 |
+
border: 2px solid #ffc107;
|
| 102 |
+
}
|
| 103 |
+
#output-text textarea {
|
| 104 |
+
font-family: 'Impact', sans-serif;
|
| 105 |
+
}
|
| 106 |
"""
|
| 107 |
|
| 108 |
# Gradio App
|
| 109 |
with gr.Blocks(css=css) as app:
|
| 110 |
with gr.Row():
|
| 111 |
+
gr.HTML(
|
| 112 |
+
'<div id="header"><span id="header_bigger">R-Detect: </span><span id="header_smaller">Human-Rewritten or AI-Generated</span></div>'
|
| 113 |
+
)
|
| 114 |
with gr.Row():
|
| 115 |
gr.HTML(
|
| 116 |
"""
|
|
|
|
| 119 |
<span class="separator">|</span>
|
| 120 |
<a href="https://github.com/xLearn-AU/R-Detect" target="_blank">Code</a>
|
| 121 |
<span class="separator">|</span>
|
| 122 |
+
<a href="mailto:yiliao.song@gmail.com" target="_blank">Contact</a>
|
| 123 |
</div>
|
| 124 |
"""
|
| 125 |
)
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
gr.HTML(
|
| 129 |
+
'<div class="demo-banner">This is a demo running on CPU only. For the full version, please refer to the <a href="https://github.com/xLearn-AU/R-Detect" target="_blank">GitHub</a> or the <a href="https://openreview.net/forum?id=z9j7wctoGV" target="_blank">Paper</a>.</div>'
|
| 130 |
)
|
| 131 |
|
| 132 |
with gr.Row():
|
|
|
|
| 141 |
label="Inference Result",
|
| 142 |
placeholder="Made by Human or AI",
|
| 143 |
elem_id="output-text",
|
| 144 |
+
lines=2, # Keep it compact
|
| 145 |
+
interactive=False, # Make it read-only
|
| 146 |
)
|
| 147 |
+
|
| 148 |
with gr.Row():
|
| 149 |
submit_button = gr.Button(
|
| 150 |
"Run Detection", variant="primary", elem_classes=["button"]
|
| 151 |
)
|
| 152 |
clear_button = gr.Button("Clear", variant="secondary", elem_classes=["button"])
|
| 153 |
|
| 154 |
+
submit_button.click(run_test, inputs=[input_text], outputs=[output])
|
| 155 |
clear_button.click(lambda: ("", ""), inputs=[], outputs=[input_text, output])
|
| 156 |
|
| 157 |
+
with gr.Accordion("Disclaimer", open=True, elem_classes=["accordion"]):
|
| 158 |
gr.Markdown(
|
| 159 |
"""
|
| 160 |
+
- **Disclaimer**: This tool is for demonstration purposes only. It is not a foolproof AI detector.
|
| 161 |
+
- **Accuracy**: Results may vary based on input length and quality.
|
| 162 |
+
"""
|
| 163 |
)
|
| 164 |
|
| 165 |
+
with gr.Accordion("Cite Our Work", open=True, elem_classes=["accordion"]):
|
| 166 |
gr.Markdown(
|
| 167 |
"""
|
| 168 |
+
```
|
| 169 |
+
@inproceedings{song2025deep,
|
| 170 |
+
title = {Deep Kernel Relative Test for Machine-generated Text Detection},
|
| 171 |
+
author = {Yiliao Song and Zhenqiao Yuan and Shuhai Zhang and Zhen Fang and Jun Yu and Feng Liu},
|
| 172 |
+
booktitle = {The Twelfth International Conference on Learning Representations},
|
| 173 |
+
year = {2025},
|
| 174 |
+
url = {https://openreview.net/pdf?id=z9j7wctoGV}
|
| 175 |
+
}
|
| 176 |
+
```
|
| 177 |
+
"""
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
with gr.Accordion("Acknowledgement", open=True, elem_classes=["accordion"]):
|
| 181 |
+
gr.Markdown(
|
| 182 |
+
"""
|
| 183 |
+
We Thanks Jinqian Wang and Jerry Ye for their help in the development of this space.,
|
| 184 |
+
"""
|
| 185 |
)
|
| 186 |
|
| 187 |
|
relative_tester.py
CHANGED
|
@@ -73,4 +73,8 @@ class RelativeTester:
|
|
| 73 |
print("DEBUG: power:", power)
|
| 74 |
print("DEBUG: power list:", h_u_list)
|
| 75 |
# Return the result
|
| 76 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
print("DEBUG: power:", power)
|
| 74 |
print("DEBUG: power list:", h_u_list)
|
| 75 |
# Return the result
|
| 76 |
+
return (
|
| 77 |
+
"Most likely Human Write"
|
| 78 |
+
if power <= threshold
|
| 79 |
+
else "Most likely Machine Generated"
|
| 80 |
+
)
|