Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
model_path = "arad1367/crypto_sustainability_news_text_classifier-distilbert-base-uncased"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
8 |
+
|
9 |
+
def crypto_classifier(text: str):
|
10 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
11 |
+
outputs = model(**inputs)
|
12 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
13 |
+
|
14 |
+
labels = ["Negative", "Neutral", "Positive"]
|
15 |
+
output_dict = {label: prob.item() for label, prob in zip(labels, probabilities[0])}
|
16 |
+
return output_dict
|
17 |
+
|
18 |
+
custom_css = """
|
19 |
+
.container {
|
20 |
+
max-width: 1200px;
|
21 |
+
margin: auto;
|
22 |
+
padding: 20px;
|
23 |
+
font-family: 'Inter', system-ui, -apple-system, sans-serif;
|
24 |
+
}
|
25 |
+
.header {
|
26 |
+
text-align: center;
|
27 |
+
margin: 2em 0;
|
28 |
+
color: #2d7ff9;
|
29 |
+
}
|
30 |
+
.description {
|
31 |
+
text-align: center;
|
32 |
+
margin-bottom: 2em;
|
33 |
+
color: #666;
|
34 |
+
}
|
35 |
+
.footer {
|
36 |
+
text-align: center;
|
37 |
+
margin-top: 20px;
|
38 |
+
padding: 20px;
|
39 |
+
border-top: 1px solid #eee;
|
40 |
+
background: #f8f9fa;
|
41 |
+
}
|
42 |
+
.footer a {
|
43 |
+
color: #2d7ff9;
|
44 |
+
text-decoration: none;
|
45 |
+
margin: 0 10px;
|
46 |
+
font-weight: 500;
|
47 |
+
}
|
48 |
+
.footer a:hover {
|
49 |
+
text-decoration: underline;
|
50 |
+
}
|
51 |
+
.duplicate-button {
|
52 |
+
background-color: #2d7ff9 !important;
|
53 |
+
color: white !important;
|
54 |
+
border-radius: 8px !important;
|
55 |
+
padding: 10px 20px !important;
|
56 |
+
margin: 20px auto !important;
|
57 |
+
display: block !important;
|
58 |
+
}
|
59 |
+
"""
|
60 |
+
|
61 |
+
examples = [
|
62 |
+
["The Crypto Alpha Conference, focusing on sustainability in the cryptocurrency world, will be organized next year."],
|
63 |
+
["There are growing concerns about the environmental impact of cryptocurrency mining processes."],
|
64 |
+
["Major companies have committed to investing in sustainable cryptocurrencies."],
|
65 |
+
["The new blockchain protocol reduces energy consumption by 90%."],
|
66 |
+
["Renewable energy adoption in mining operations has increased significantly."],
|
67 |
+
["The decentralized network operates on renewable energy sources."],
|
68 |
+
["Bitcoin mining contributes to increased carbon emissions in developing countries."]
|
69 |
+
]
|
70 |
+
|
71 |
+
with gr.Blocks(theme='earneleh/paris', css=custom_css) as demo:
|
72 |
+
with gr.Column(elem_classes="container"):
|
73 |
+
gr.Markdown("# Cryptocurrency News Sustainability Classifier", elem_classes="header")
|
74 |
+
gr.Markdown(
|
75 |
+
"Analyze cryptocurrency-related text to determine its sustainability implications.",
|
76 |
+
elem_classes="description"
|
77 |
+
)
|
78 |
+
|
79 |
+
input_text = gr.Textbox(
|
80 |
+
label="Input Text",
|
81 |
+
placeholder="Enter cryptocurrency-related news or statement...",
|
82 |
+
lines=3
|
83 |
+
)
|
84 |
+
output_label = gr.Label(label="Classification Results", num_top_classes=3)
|
85 |
+
submit_btn = gr.Button("Analyze", variant="primary")
|
86 |
+
|
87 |
+
gr.Examples(examples=examples, inputs=input_text)
|
88 |
+
|
89 |
+
submit_btn.click(fn=crypto_classifier, inputs=input_text, outputs=output_label)
|
90 |
+
|
91 |
+
gr.DuplicateButton(
|
92 |
+
value="Duplicate Space for private use",
|
93 |
+
elem_classes="duplicate-button"
|
94 |
+
)
|
95 |
+
|
96 |
+
gr.HTML("""
|
97 |
+
<div class="footer">
|
98 |
+
<a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> |
|
99 |
+
<a href="https://github.com/arad1367" target="_blank">GitHub</a> |
|
100 |
+
<a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a>
|
101 |
+
<p>Made with π by Pejman Ebrahimi</p>
|
102 |
+
</div>
|
103 |
+
""")
|
104 |
+
|
105 |
+
demo.launch(share=True)
|