Spaces:
Runtime error
Runtime error
| ''' | |
| import altair as alt | |
| import numpy as np | |
| import pandas as pd | |
| import streamlit as st | |
| """ | |
| # Welcome to Streamlit! | |
| Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:. | |
| If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community | |
| forums](https://discuss.streamlit.io). | |
| In the meantime, below is an example of what you can do with just a few lines of code: | |
| """ | |
| num_points = st.slider("Number of points in spiral", 1, 10000, 1100) | |
| num_turns = st.slider("Number of turns in spiral", 1, 300, 31) | |
| indices = np.linspace(0, 1, num_points) | |
| theta = 2 * np.pi * num_turns * indices | |
| radius = indices | |
| x = radius * np.cos(theta) | |
| y = radius * np.sin(theta) | |
| df = pd.DataFrame({ | |
| "x": x, | |
| "y": y, | |
| "idx": indices, | |
| "rand": np.random.randn(num_points), | |
| }) | |
| st.altair_chart(alt.Chart(df, height=700, width=700) | |
| .mark_point(filled=True) | |
| .encode( | |
| x=alt.X("x", axis=None), | |
| y=alt.Y("y", axis=None), | |
| color=alt.Color("idx", legend=None, scale=alt.Scale()), | |
| size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])), | |
| )) | |
| ''' | |
| import streamlit as st | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| import torch.nn.functional as F | |
| import os | |
| st.set_page_config(page_title="FinBERT Sentiment", layout="centered") | |
| st.title("💰 FinBERT: Financial Sentiment Analysis") | |
| st.markdown("Модель: `yiyanghkust/finbert-tone` — обучена на финансовых текстах") | |
| def load_model(): | |
| # Установка кастомного пути к кэшу | |
| cache_dir = "/tmp/huggingface" | |
| os.makedirs(cache_dir, exist_ok=True) | |
| tokenizer = AutoTokenizer.from_pretrained("yiyanghkust/finbert-tone", cache_dir=cache_dir) | |
| model = AutoModelForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone", cache_dir=cache_dir) | |
| return tokenizer, model | |
| tokenizer, model = load_model() | |
| text = st.text_area("Введите финансовую новость или отчёт:", height=150) | |
| if st.button("Анализировать тональность") and text.strip(): | |
| inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = F.softmax(outputs.logits, dim=1).squeeze() | |
| labels = ["📉 Negative", "😐 Neutral", "📈 Positive"] | |
| for label, prob in zip(labels, probs): | |
| st.write(f"**{label}:** {prob.item():.3f}") |