|
import gradio as gr |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
model_path = "arad1367/crypto_sustainability_news_text_classifier-distilbert-base-uncased" |
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_path) |
|
|
|
def crypto_classifier(text: str): |
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512) |
|
outputs = model(**inputs) |
|
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1) |
|
|
|
labels = ["Negative", "Neutral", "Positive"] |
|
output_dict = {label: prob.item() for label, prob in zip(labels, probabilities[0])} |
|
return output_dict |
|
|
|
custom_css = """ |
|
.container { |
|
max-width: 1200px; |
|
margin: auto; |
|
padding: 20px; |
|
font-family: 'Inter', system-ui, -apple-system, sans-serif; |
|
} |
|
.header { |
|
text-align: center; |
|
margin: 2em 0; |
|
color: #2d7ff9; |
|
} |
|
.description { |
|
text-align: center; |
|
margin-bottom: 2em; |
|
color: #666; |
|
} |
|
.footer { |
|
text-align: center; |
|
margin-top: 20px; |
|
padding: 20px; |
|
border-top: 1px solid #eee; |
|
background: #f8f9fa; |
|
} |
|
.footer a { |
|
color: #2d7ff9; |
|
text-decoration: none; |
|
margin: 0 10px; |
|
font-weight: 500; |
|
} |
|
.footer a:hover { |
|
text-decoration: underline; |
|
} |
|
.duplicate-button { |
|
background-color: #2d7ff9 !important; |
|
color: white !important; |
|
border-radius: 8px !important; |
|
padding: 10px 20px !important; |
|
margin: 20px auto !important; |
|
display: block !important; |
|
} |
|
""" |
|
|
|
examples = [ |
|
["The Crypto Alpha Conference, focusing on sustainability in the cryptocurrency world, will be organized next year."], |
|
["There are growing concerns about the environmental impact of cryptocurrency mining processes."], |
|
["Major companies have committed to investing in sustainable cryptocurrencies."], |
|
["The new blockchain protocol reduces energy consumption by 90%."], |
|
["Renewable energy adoption in mining operations has increased significantly."], |
|
["The decentralized network operates on renewable energy sources."], |
|
["Bitcoin mining contributes to increased carbon emissions in developing countries."] |
|
] |
|
|
|
with gr.Blocks(theme='earneleh/paris', css=custom_css) as demo: |
|
with gr.Column(elem_classes="container"): |
|
gr.Markdown("# Cryptocurrency News Sustainability Classifier", elem_classes="header") |
|
gr.Markdown( |
|
"Analyze cryptocurrency-related text to determine its sustainability implications.", |
|
elem_classes="description" |
|
) |
|
|
|
input_text = gr.Textbox( |
|
label="Input Text", |
|
placeholder="Enter cryptocurrency-related news or statement...", |
|
lines=3 |
|
) |
|
output_label = gr.Label(label="Classification Results", num_top_classes=3) |
|
submit_btn = gr.Button("Analyze", variant="primary") |
|
|
|
gr.Examples(examples=examples, inputs=input_text) |
|
|
|
submit_btn.click(fn=crypto_classifier, inputs=input_text, outputs=output_label) |
|
|
|
gr.DuplicateButton( |
|
value="Duplicate Space for private use", |
|
elem_classes="duplicate-button" |
|
) |
|
|
|
gr.HTML(""" |
|
<div class="footer"> |
|
<a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> | |
|
<a href="https://github.com/arad1367" target="_blank">GitHub</a> | |
|
<a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a> |
|
<p>Made with π by Pejman Ebrahimi</p> |
|
</div> |
|
""") |
|
|
|
demo.launch(share=True) |