Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
def main():
|
| 6 |
+
# 加载情感分析模型和tokenizer
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained("WRX020510/CustomModel_twitter", num_labels=3)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
|
| 9 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 10 |
+
|
| 11 |
+
# 加载文本摘要模型
|
| 12 |
+
summarizer = pipeline("summarization", model="human-centered-summarization/financial-summarization-pegasus")
|
| 13 |
+
|
| 14 |
+
# Streamlit 应用界面设计
|
| 15 |
+
st.title("Financial Text Analysis with Hugging Face Spaces")
|
| 16 |
+
st.write("Enter a financial text to analyze its sentiment and generate a summary:")
|
| 17 |
+
|
| 18 |
+
# 用户输入
|
| 19 |
+
user_input = st.text_area("Text Input", height=150)
|
| 20 |
+
|
| 21 |
+
if user_input:
|
| 22 |
+
# 情感分析
|
| 23 |
+
sentiment_result = sentiment_pipeline(user_input)
|
| 24 |
+
sentiment = sentiment_result[0]['label']
|
| 25 |
+
st.write(f"Sentiment (0-2): {sentiment}")
|
| 26 |
+
|
| 27 |
+
# 文本摘要
|
| 28 |
+
summary_result = summarizer(user_input, max_length=130, min_length=30, do_sample=False)
|
| 29 |
+
summary = summary_result[0]['summary_text']
|
| 30 |
+
st.write("Summary:")
|
| 31 |
+
st.write(summary)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
main()
|