jer233 commited on
Commit
c319e25
·
verified ·
1 Parent(s): 8c68259

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -4
app.py CHANGED
@@ -1,5 +1,160 @@
1
- from demo import app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- if __name__ == "__main__":
4
- # Launch the Gradio interface
5
- app.launch(show_api=False, debug=True, share=True)
 
1
+ import gradio as gr
2
+ from relative_tester import relative_tester
3
+ # from two_sample_tester import two_sample_tester
4
+ from utils import init_random_seeds
5
+
6
+ init_random_seeds()
7
+
8
+
9
+ def run_test(input_text):
10
+ if not input_text:
11
+ return "Now that you've built a demo, you'll probably want to share it with others. Gradio demos can be shared in two ways: using a temporary share link or permanent hosting on Spaces."
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
+ # TODO: Add model selection in the future
17
+ # Change mode name
18
+ # def change_mode(mode):
19
+ # if mode == "Faster Model":
20
+ # .change_mode("t5-small")
21
+ # elif mode == "Medium Model":
22
+ # .change_mode("roberta-base-openai-detector")
23
+ # elif mode == "Powerful Model":
24
+ # .change_mode("falcon-rw-1b")
25
+ # else:
26
+ # gr.Error(f"Invaild mode selected.")
27
+ # return mode
28
+
29
+
30
+ css = """
31
+ #header { text-align: center; font-size: 3em; margin-bottom: 20px; }
32
+ #output-text { font-weight: bold; font-size: 1.2em; }
33
+ .links {
34
+ display: flex;
35
+ justify-content: flex-end;
36
+ gap: 10px;
37
+ margin-right: 10px;
38
+ align-items: center;
39
+ }
40
+ .separator {
41
+ margin: 0 5px;
42
+ color: black;
43
+ }
44
+
45
+ /* Adjusting layout for Input Text and Inference Result */
46
+ .input-row {
47
+ display: flex;
48
+ width: 100%;
49
+ }
50
+
51
+ .input-text {
52
+ flex: 3; /* 4 parts of the row */
53
+ margin-right: 1px;
54
+ }
55
+
56
+ .output-text {
57
+ flex: 1; /* 1 part of the row */
58
+ }
59
+
60
+ /* Set button widths to match the Select Model width */
61
+ .button {
62
+ width: 250px; /* Same as the select box width */
63
+ height: 100px; /* Button height */
64
+ }
65
+
66
+ /* Set height for the Select Model dropdown */
67
+ .select {
68
+ height: 100px; /* Set height to 100px */
69
+ }
70
+
71
+ /* Accordion Styling */
72
+ .accordion {
73
+ width: 100%; /* Set the width of the accordion to match the parent */
74
+ max-height: 200px; /* Set a max-height for accordion */
75
+ overflow-y: auto; /* Allow scrolling if the content exceeds max height */
76
+ margin-bottom: 10px; /* Add space below accordion */
77
+ box-sizing: border-box; /* Ensure padding is included in width/height */
78
+ }
79
+
80
+ /* Accordion content max-height */
81
+ .accordion-content {
82
+ max-height: 200px; /* Limit the height of the content */
83
+ overflow-y: auto; /* Add a scrollbar if content overflows */
84
+ }
85
+ """
86
+
87
+ # Gradio App
88
+ with gr.Blocks(css=css) as app:
89
+ with gr.Row():
90
+ gr.HTML('<div id="header">R-detect On HuggingFace</div>')
91
+ with gr.Row():
92
+ gr.HTML(
93
+ """
94
+ <div class="links">
95
+ <a href="https://openreview.net/forum?id=z9j7wctoGV" target="_blank">Paper</a>
96
+ <span class="separator">|</span>
97
+ <a href="https://github.com/xLearn-AU/R-Detect" target="_blank">Code</a>
98
+ <span class="separator">|</span>
99
+ <a href="mailto:[email protected]" target="_blank">Contact</a>
100
+ </div>
101
+ """
102
+ )
103
+ with gr.Row():
104
+ input_text = gr.Textbox(
105
+ label="Input Text",
106
+ placeholder="Enter Text Here",
107
+ lines=8,
108
+ elem_classes=["input-text"], # Applying the CSS class
109
+ )
110
+ output = gr.Textbox(
111
+ label="Inference Result",
112
+ placeholder="Made by Human or AI",
113
+ elem_id="output-text",
114
+ lines=8,
115
+ elem_classes=["output-text"],
116
+ )
117
+ with gr.Row():
118
+ # TODO: Add model selection in the future
119
+ # model_name = gr.Dropdown(
120
+ # [
121
+ # "Faster Model",
122
+ # "Medium Model",
123
+ # "Powerful Model",
124
+ # ],
125
+ # label="Select Model",
126
+ # value="Medium Model",
127
+ # elem_classes=["select"],
128
+ # )
129
+ submit_button = gr.Button(
130
+ "Run Detection", variant="primary", elem_classes=["button"]
131
+ )
132
+ clear_button = gr.Button("Clear", variant="secondary", elem_classes=["button"])
133
+
134
+ submit_button.click(run_test, inputs=[input_text], outputs=output)
135
+ clear_button.click(lambda: ("", ""), inputs=[], outputs=[input_text, output])
136
+
137
+ with gr.Accordion("Disclaimer", open=False, elem_classes=["accordion"]):
138
+ gr.Markdown(
139
+ """
140
+ - **Disclaimer**: This tool is for demonstration purposes only. It is not a foolproof AI detector.
141
+ - **Accuracy**: Results may vary based on input length and quality.
142
+ """
143
+ )
144
+
145
+ with gr.Accordion("Citations", open=False, elem_classes=["accordion"]):
146
+ gr.Markdown(
147
+ """
148
+ ```
149
+ @inproceedings{zhangs2024MMDMP,
150
+ title={Detecting Machine-Generated Texts by Multi-Population Aware Optimization for Maximum Mean Discrepancy},
151
+ author={Zhang, Shuhai and Song, Yiliao and Yang, Jiahao and Li, Yuanqing and Han, Bo and Tan, Mingkui},
152
+ booktitle = {International Conference on Learning Representations (ICLR)},
153
+ year={2024}
154
+ }
155
+ ```
156
+ """
157
+ )
158
+
159
+ app.launch()
160