Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- README.md +2 -8
- demo.py +111 -0
- distilbert-fine-tuned.ipynb +186 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title: MGT
|
3 |
-
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.36.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: MGT-Demo
|
3 |
+
app_file: demo.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.36.1
|
|
|
|
|
6 |
---
|
|
|
|
demo.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import DebertaTokenizer, DebertaForSequenceClassification
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
save_path_abstract = '/home/raj.tomar/Downloads/MGT/experiments/fine-tuned-deberta/'
|
6 |
+
model_abstract = DebertaForSequenceClassification.from_pretrained(save_path_abstract)
|
7 |
+
tokenizer_abstract = DebertaTokenizer.from_pretrained(save_path_abstract)
|
8 |
+
|
9 |
+
classifier_abstract = pipeline('text-classification', model=model_abstract, tokenizer=tokenizer_abstract)
|
10 |
+
|
11 |
+
save_path_essay = '/home/raj.tomar/Downloads/MGT/experiments/fine-tuned-deberta/'
|
12 |
+
model_essay = DebertaForSequenceClassification.from_pretrained(save_path_essay)
|
13 |
+
tokenizer_essay = DebertaTokenizer.from_pretrained(save_path_essay)
|
14 |
+
|
15 |
+
classifier_essay = pipeline('text-classification', model=model_essay, tokenizer=tokenizer_essay)
|
16 |
+
|
17 |
+
def update(name, uploaded_file, radio_input):
|
18 |
+
if uploaded_file is not None:
|
19 |
+
return f"{name}, you uploaded a file named {uploaded_file.name}."
|
20 |
+
else:
|
21 |
+
if radio_input == 'Scientific Abstract':
|
22 |
+
data = classifier_abstract(name)[0]['label']
|
23 |
+
if data == 'LABEL_0':
|
24 |
+
return "human_text"
|
25 |
+
if data == 'LABEL_1':
|
26 |
+
return "machine_text"
|
27 |
+
if data == 'LABEL_2':
|
28 |
+
return "human-written | machine-polished"
|
29 |
+
if data == 'LABEL_3':
|
30 |
+
return "machine-generated | machine-humanized"
|
31 |
+
else:
|
32 |
+
if radio_input == 'Student Essay':
|
33 |
+
data = classifier_essay(name)[0]['label']
|
34 |
+
if data == 'LABEL_0':
|
35 |
+
return "human_text"
|
36 |
+
if data == 'LABEL_1':
|
37 |
+
return "machine_text"
|
38 |
+
if data == 'LABEL_2':
|
39 |
+
return "human-written | machine-polished"
|
40 |
+
if data == 'LABEL_3':
|
41 |
+
return "machine-generated | machine-humanized"
|
42 |
+
# return "Hold on!"
|
43 |
+
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
gr.Markdown(
|
46 |
+
"""
|
47 |
+
<style>
|
48 |
+
.gr-button-secondary {
|
49 |
+
width: 100px;
|
50 |
+
height: 30px;
|
51 |
+
padding: 5px;
|
52 |
+
}
|
53 |
+
.gr-row {
|
54 |
+
display: flex;
|
55 |
+
align-items: center;
|
56 |
+
gap: 10px;
|
57 |
+
}
|
58 |
+
.gr-block {
|
59 |
+
padding: 20px;
|
60 |
+
}
|
61 |
+
.gr-markdown p {
|
62 |
+
font-size: 16px;
|
63 |
+
}
|
64 |
+
</style>
|
65 |
+
<span style='font-family: Arial, sans-serif; font-size: 20px;'>Was this text written by <strong>human</strong> or <strong>AI</strong>?</span>
|
66 |
+
<p style='font-family: Arial, sans-serif;'>Try detecting one of our sample texts:</p>
|
67 |
+
"""
|
68 |
+
)
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
for sample in ["Machine-Generated", "Human-Written", "Machine-Humanized", "Machine - Polished"]:
|
72 |
+
gr.Button(sample, variant="outline")
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
radio_button = gr.Radio(['Scientific Abstract', 'Student Essay'], label = 'Text Type', info = 'We have specialized models that work on domain-specific text.')
|
76 |
+
|
77 |
+
with gr.Row():
|
78 |
+
input_text = gr.Textbox(placeholder="Paste your text here...", label="", lines=10)
|
79 |
+
file_input = gr.File(label="Upload File")
|
80 |
+
|
81 |
+
#file_input = gr.File(label="", visible=False) # Hide the actual file input
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
check_button = gr.Button("Check Origin", variant="primary")
|
85 |
+
clear_button = gr.ClearButton([input_text, file_input, radio_button], variant='stop')
|
86 |
+
#upload_button = gr.Button("Upload File", variant="secondary")
|
87 |
+
|
88 |
+
out = gr.Textbox(label="OUTPUT", placeholder="", lines=2)
|
89 |
+
clear_button.add(out)
|
90 |
+
|
91 |
+
check_button.click(fn=update, inputs=[input_text, file_input, radio_button], outputs=out)
|
92 |
+
#upload_button.click(lambda: None, inputs=[], outputs=[]).then(fn=update, inputs=[input_text, file_input], outputs=out)
|
93 |
+
|
94 |
+
# Adding JavaScript to simulate file input click
|
95 |
+
gr.Markdown(
|
96 |
+
"""
|
97 |
+
<script>
|
98 |
+
document.addEventListener("DOMContentLoaded", function() {
|
99 |
+
const uploadButton = Array.from(document.getElementsByTagName('button')).find(el => el.innerText === "Upload File");
|
100 |
+
if (uploadButton) {
|
101 |
+
uploadButton.onclick = function() {
|
102 |
+
document.querySelector('input[type="file"]').click();
|
103 |
+
};
|
104 |
+
}
|
105 |
+
});
|
106 |
+
</script>
|
107 |
+
"""
|
108 |
+
)
|
109 |
+
|
110 |
+
demo.launch(share=True)
|
111 |
+
|
distilbert-fine-tuned.ipynb
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"from transformers import DebertaTokenizer, DebertaForSequenceClassification\n",
|
10 |
+
"from transformers import pipeline\n",
|
11 |
+
"\n",
|
12 |
+
"save_path = '/home/raj.tomar/Downloads/MGT/experiments/fine-tuned-deberta/'\n",
|
13 |
+
"model = DebertaForSequenceClassification.from_pretrained(save_path)\n",
|
14 |
+
"tokenizer = DebertaTokenizer.from_pretrained(save_path)\n",
|
15 |
+
"\n",
|
16 |
+
"classifier = pipeline('text-classification', model=model, tokenizer=tokenizer)\n"
|
17 |
+
]
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"cell_type": "code",
|
21 |
+
"execution_count": null,
|
22 |
+
"metadata": {},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
|
26 |
+
"\n",
|
27 |
+
"tokenizer_essay = AutoTokenizer.from_pretrained(\"ngocminhta/RoBERTa-MGT-Test\")\n",
|
28 |
+
"model_essay = AutoModelForSequenceClassification.from_pretrained(\"ngocminhta/RoBERTa-MGT-Test\")\n",
|
29 |
+
"\n",
|
30 |
+
"classifier_essay = pipeline('text-classification', model=model_essay, tokenizer=tokenizer_essay)"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"cell_type": "code",
|
35 |
+
"execution_count": null,
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [],
|
38 |
+
"source": [
|
39 |
+
"classifier_essay(\"The Impact of Social Media on Society In the contemporary world, social media has become an integral part of daily life, shaping how people communicate, interact, and share information. Platforms such as Facebook, Twitter, Instagram, and TikTok have transformed the landscape of communication, offering unprecedented access to information and connectivity. While social media presents numerous benefits, it also poses significant challenges and concerns. This essay explores the multifaceted impact of social media on society, examining both its positive contributions and its potential drawbacks. Positive Impacts One of the most notable positive impacts of social media is its ability to foster connectivity and communication. Social media platforms allow individuals to maintain relationships with friends and family members, regardless of geographical barriers. For instance, expatriates can stay in touch with loved ones back home, and people can form global communities based on shared interests. This enhanced connectivity has contributed to a more interconnected and empathetic world. Moreover, social media serves as a powerful tool for information dissemination and awareness. News outlets and organizations use these platforms to reach a broader audience, delivering real-time updates on global events. During crises, such as natural disasters or political upheavals, social media can facilitate the rapid spread of crucial information, aiding in disaster response and mobilizing support. Additionally, social media has played a significant role in social and political movements, enabling activists to organize, raise awareness, and advocate for change. The educational potential of social media is another significant benefit. Platforms like YouTube, LinkedIn, and Twitter offer a wealth of educational content, ranging from instructional videos to professional development resources. Educators and institutions leverage social media to engage with students, share knowledge, and foster collaborative learning environments. This democratization of information has made education more accessible to people worldwide. Negative Impacts Despite its many advantages, social media also has several negative implications. One of the most pressing concerns is its impact on mental health. Studies have shown that excessive use of social media can lead to feelings of anxiety, depression, and loneliness. The constant comparison with others' curated online personas can create unrealistic expectations and contribute to low self-esteem. Additionally, cyberbullying and online harassment have become prevalent issues, particularly among young people, exacerbating mental health problems. Another significant issue is the spread of misinformation and fake news. Social media platforms, with their vast reach and rapid information dissemination, are often used to spread false or misleading content. This phenomenon can have serious consequences, such as influencing public opinion, exacerbating political polarization, and undermining trust in institutions. The algorithms that drive social media often prioritize sensational and controversial content, further amplifying the spread of misinformation. Privacy concerns are also paramount in the discussion of social media's impact. The collection and use of personal data by social media companies raise significant ethical and security issues. Users often unknowingly share vast amounts of personal information, which can be exploited for targeted advertising, data breaches, and surveillance. The lack of stringent regulatory frameworks exacerbates these concerns, leaving users vulnerable to privacy violations. Conclusion In conclusion, social media has profoundly influenced modern society, offering both remarkable benefits and substantial challenges. It has revolutionized communication, provided educational opportunities, and empowered social and political movements. However, it also poses significant risks to mental health, facilitates the spread of misinformation, and raises critical privacy concerns. As social media continues to evolve, it is essential to strike a balance between leveraging its positive aspects and mitigating its negative impacts. Policymakers, educators, and users must work collaboratively to ensure that social media remains a force for good in society, fostering a connected, informed, and resilient global community.\")"
|
40 |
+
]
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"cell_type": "code",
|
44 |
+
"execution_count": null,
|
45 |
+
"metadata": {},
|
46 |
+
"outputs": [],
|
47 |
+
"source": [
|
48 |
+
"\n",
|
49 |
+
"classifier(\"We have analyzed the HI aperture synthesis image of the Large Magellanic\\nCloud (LMC), using an objective and quantitative measure of topology to\\nunderstand the HI distribution hosting a number of holes and clumps of various\\nsizes in the medium.\") "
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"execution_count": null,
|
55 |
+
"metadata": {},
|
56 |
+
"outputs": [],
|
57 |
+
"source": []
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"execution_count": null,
|
62 |
+
"metadata": {},
|
63 |
+
"outputs": [],
|
64 |
+
"source": [
|
65 |
+
"# from huggingface_hub import HfFolder, notebook_login\n",
|
66 |
+
"# notebook_login()"
|
67 |
+
]
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"cell_type": "code",
|
71 |
+
"execution_count": null,
|
72 |
+
"metadata": {},
|
73 |
+
"outputs": [],
|
74 |
+
"source": [
|
75 |
+
"import gradio as gr\n",
|
76 |
+
"\n",
|
77 |
+
"def update(name, uploaded_file, radio_type):\n",
|
78 |
+
" if uploaded_file is not None:\n",
|
79 |
+
" return f\"{name}, you uploaded a file named {uploaded_file.name}.\"\n",
|
80 |
+
" else:\n",
|
81 |
+
" if radio_type == 'Student Essay':\n",
|
82 |
+
" return classifier(name)[0]['label']\n",
|
83 |
+
" else:\n",
|
84 |
+
" return \"Hold on!\"\n",
|
85 |
+
"\n",
|
86 |
+
"with gr.Blocks() as demo:\n",
|
87 |
+
" gr.Markdown(\n",
|
88 |
+
" \"\"\"\n",
|
89 |
+
" <style>\n",
|
90 |
+
" .gr-button-secondary {\n",
|
91 |
+
" width: 100px;\n",
|
92 |
+
" height: 30px;\n",
|
93 |
+
" padding: 5px;\n",
|
94 |
+
" }\n",
|
95 |
+
" .gr-row {\n",
|
96 |
+
" display: flex;\n",
|
97 |
+
" align-items: center;\n",
|
98 |
+
" gap: 10px;\n",
|
99 |
+
" }\n",
|
100 |
+
" .gr-block {\n",
|
101 |
+
" padding: 20px;\n",
|
102 |
+
" }\n",
|
103 |
+
" .gr-markdown p {\n",
|
104 |
+
" font-size: 16px;\n",
|
105 |
+
" }\n",
|
106 |
+
" </style>\n",
|
107 |
+
" <span style='font-family: Arial, sans-serif; font-size: 20px;'>Was this text written by <strong>human</strong> or <strong>AI</strong>?</span>\n",
|
108 |
+
" <p style='font-family: Arial, sans-serif;'>Try detecting one of our sample texts:</p>\n",
|
109 |
+
" \"\"\"\n",
|
110 |
+
" )\n",
|
111 |
+
" \n",
|
112 |
+
" with gr.Row():\n",
|
113 |
+
" for sample in [\"Machine-Generated\", \"Human-Written\", \"Machine-Humanized\", \"Machine - Polished\"]:\n",
|
114 |
+
" gr.Button(sample, variant=\"outline\")\n",
|
115 |
+
"\n",
|
116 |
+
" with gr.Row():\n",
|
117 |
+
" radio_button = gr.Radio([ 'Student Essay','Scientific Abstract'], label = 'Text Type', info = 'We have specialized models that work on domain-specific text.', value = 'Student Essay')\n",
|
118 |
+
"\n",
|
119 |
+
" with gr.Row():\n",
|
120 |
+
" input_text = gr.Textbox(placeholder=\"Paste your text here...\", label=\"\", lines=10)\n",
|
121 |
+
" file_input = gr.File(label=\"Upload File\")\n",
|
122 |
+
"\n",
|
123 |
+
" #file_input = gr.File(label=\"\", visible=False) # Hide the actual file input\n",
|
124 |
+
" \n",
|
125 |
+
" with gr.Row():\n",
|
126 |
+
" check_button = gr.Button(\"Check Origin\", variant=\"primary\")\n",
|
127 |
+
" clear_button = gr.ClearButton([input_text, file_input, radio_button], variant='stop')\n",
|
128 |
+
" #upload_button = gr.Button(\"Upload File\", variant=\"secondary\")\n",
|
129 |
+
" \n",
|
130 |
+
" out = gr.Textbox(label=\"OUTPUT\", placeholder=\"\", lines=2)\n",
|
131 |
+
" clear_button.add(out)\n",
|
132 |
+
" \n",
|
133 |
+
" check_button.click(fn=update, inputs=[input_text, file_input, radio_button], outputs=out)\n",
|
134 |
+
" #upload_button.click(lambda: None, inputs=[], outputs=[]).then(fn=update, inputs=[input_text, file_input], outputs=out)\n",
|
135 |
+
"\n",
|
136 |
+
" # Adding JavaScript to simulate file input click\n",
|
137 |
+
" gr.Markdown(\n",
|
138 |
+
" \"\"\"\n",
|
139 |
+
" <script>\n",
|
140 |
+
" document.addEventListener(\"DOMContentLoaded\", function() {\n",
|
141 |
+
" const uploadButton = Array.from(document.getElementsByTagName('button')).find(el => el.innerText === \"Upload File\");\n",
|
142 |
+
" if (uploadButton) {\n",
|
143 |
+
" uploadButton.onclick = function() {\n",
|
144 |
+
" document.querySelector('input[type=\"file\"]').click();\n",
|
145 |
+
" };\n",
|
146 |
+
" }\n",
|
147 |
+
" });\n",
|
148 |
+
" </script>\n",
|
149 |
+
" \"\"\"\n",
|
150 |
+
" )\n",
|
151 |
+
"\n",
|
152 |
+
"demo.launch(share=True)\n",
|
153 |
+
"\n"
|
154 |
+
]
|
155 |
+
},
|
156 |
+
{
|
157 |
+
"cell_type": "code",
|
158 |
+
"execution_count": null,
|
159 |
+
"metadata": {},
|
160 |
+
"outputs": [],
|
161 |
+
"source": []
|
162 |
+
}
|
163 |
+
],
|
164 |
+
"metadata": {
|
165 |
+
"kernelspec": {
|
166 |
+
"display_name": "proj",
|
167 |
+
"language": "python",
|
168 |
+
"name": "python3"
|
169 |
+
},
|
170 |
+
"language_info": {
|
171 |
+
"codemirror_mode": {
|
172 |
+
"name": "ipython",
|
173 |
+
"version": 3
|
174 |
+
},
|
175 |
+
"file_extension": ".py",
|
176 |
+
"mimetype": "text/x-python",
|
177 |
+
"name": "python",
|
178 |
+
"nbconvert_exporter": "python",
|
179 |
+
"pygments_lexer": "ipython3",
|
180 |
+
"version": "3.10.14"
|
181 |
+
},
|
182 |
+
"orig_nbformat": 4
|
183 |
+
},
|
184 |
+
"nbformat": 4,
|
185 |
+
"nbformat_minor": 2
|
186 |
+
}
|