fix
Browse files
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title: FinGPT
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
@@ -11,40 +11,48 @@ license: mit
|
|
| 11 |
hf_oauth: true
|
| 12 |
---
|
| 13 |
|
| 14 |
-
#
|
| 15 |
|
| 16 |
-
这是一个基于 **FinGPT/fingpt-mt_llama3-8b_lora**
|
| 17 |
|
| 18 |
## ⚠️ 重要配置
|
| 19 |
|
| 20 |
由于使用了Llama 3基础模型,需要在Spaces设置中配置访问权限:
|
| 21 |
|
| 22 |
1. 确保你的HF账号已经获得 [Meta-Llama-3-8B](https://huggingface.co/meta-llama/Meta-Llama-3-8B) 的访问权限
|
| 23 |
-
2. 在Spaces的Settings
|
| 24 |
|
| 25 |
## 功能特性
|
| 26 |
|
| 27 |
-
-
|
| 28 |
-
- 🧠
|
| 29 |
-
- 🚀 GPU
|
| 30 |
-
-
|
| 31 |
|
| 32 |
## 使用说明
|
| 33 |
|
| 34 |
-
1.
|
| 35 |
-
2. 点击"
|
| 36 |
-
3.
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
## 模型信息
|
| 40 |
|
| 41 |
- 基础模型:meta-llama/Meta-Llama-3-8B
|
| 42 |
- LoRA适配器:FinGPT/fingpt-mt_llama3-8b_lora
|
| 43 |
-
-
|
|
|
|
| 44 |
|
| 45 |
-
##
|
| 46 |
|
| 47 |
-
-
|
| 48 |
-
-
|
| 49 |
-
-
|
| 50 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: FinGPT Sentiment Analysis
|
| 3 |
+
emoji: 📊
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
|
|
| 11 |
hf_oauth: true
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# 📊 FinGPT 金融新闻情感分析
|
| 15 |
|
| 16 |
+
这是一个基于 **FinGPT/fingpt-mt_llama3-8b_lora** 模型的金融新闻情感分析工具。
|
| 17 |
|
| 18 |
## ⚠️ 重要配置
|
| 19 |
|
| 20 |
由于使用了Llama 3基础模型,需要在Spaces设置中配置访问权限:
|
| 21 |
|
| 22 |
1. 确保你的HF账号已经获得 [Meta-Llama-3-8B](https://huggingface.co/meta-llama/Meta-Llama-3-8B) 的访问权限
|
| 23 |
+
2. 在Spaces的Settings中启用 `hf_oauth: true`(已在README配置中启用)
|
| 24 |
|
| 25 |
## 功能特性
|
| 26 |
|
| 27 |
+
- 📰 **情感分析**:自动分析金融新闻的情感倾向(positive/neutral/negative)
|
| 28 |
+
- 🧠 **专业模型**:基于Llama 3-8B在金融市场趋势分析任务上微调
|
| 29 |
+
- 🚀 **GPU加速**:使用Hugging Face Spaces的GPU支持快速推理
|
| 30 |
+
- 🌐 **英文支持**:针对英文金融新闻优化
|
| 31 |
|
| 32 |
## 使用说明
|
| 33 |
|
| 34 |
+
1. 在输入框中粘贴或输入金融新闻文本
|
| 35 |
+
2. 点击"分析情感"按钮或按Enter键
|
| 36 |
+
3. 查看AI分析的情感结果:
|
| 37 |
+
- **positive(积极)**:利好消息,市场看涨信号
|
| 38 |
+
- **neutral(中性)**:中性报道,无明显倾向
|
| 39 |
+
- **negative(消极)**:利空消息,市场看跌信号
|
| 40 |
|
| 41 |
## 模型信息
|
| 42 |
|
| 43 |
- 基础模型:meta-llama/Meta-Llama-3-8B
|
| 44 |
- LoRA适配器:FinGPT/fingpt-mt_llama3-8b_lora
|
| 45 |
+
- 任务类型:Market Trend Analysis(市场趋势分析)
|
| 46 |
+
- 输出类别:positive / neutral / negative
|
| 47 |
|
| 48 |
+
## 应用场景
|
| 49 |
|
| 50 |
+
- 📈 量化交易:情感信号作为交易策略输入
|
| 51 |
+
- 📊 市场研究:批量分析新闻情绪趋势
|
| 52 |
+
- 🔍 风险监控:实时监测负面新闻
|
| 53 |
+
- 💼 投资决策:辅助判断市场情绪
|
| 54 |
+
|
| 55 |
+
## 参考
|
| 56 |
+
|
| 57 |
+
- [FinGPT项目](https://github.com/AI4Finance-Foundation/FinGPT)
|
| 58 |
+
- [模型页面](https://huggingface.co/FinGPT/fingpt-mt_llama3-8b_lora)
|
app.py
CHANGED
|
@@ -3,147 +3,135 @@ import spaces
|
|
| 3 |
import torch
|
| 4 |
from peft import PeftModel
|
| 5 |
import os
|
| 6 |
-
from datasets import load_dataset
|
| 7 |
-
from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM, LlamaForCausalLM, LlamaTokenizerFast
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
| 10 |
# 获取HF token(Spaces会自动提供)
|
| 11 |
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
model = None
|
| 15 |
tokenizer = None
|
|
|
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model = PeftModel.from_pretrained(model, peft_model)
|
| 24 |
-
model = model.eval()
|
| 25 |
-
# model = AutoModel.from_pretrained("tuananhle/fingpt-forecaster_dow30_qwen3-8b_lora_250814_v3", dtype="auto")
|
| 26 |
-
# base_model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
-
# 'meta-llama/Llama-2-7b-chat-hf',
|
| 28 |
-
# trust_remote_code=True,
|
| 29 |
-
# device_map="auto",
|
| 30 |
-
# torch_dtype=torch.float16, # optional if you have enough VRAM
|
| 31 |
-
# token=hf_token,
|
| 32 |
-
# )
|
| 33 |
-
# tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf',
|
| 34 |
-
# trust_remote_code=True,
|
| 35 |
-
# token=hf_token,
|
| 36 |
-
# )
|
| 37 |
-
# model = PeftModel.from_pretrained(base_model, 'FinGPT/fingpt-forecaster_dow30_llama2-7b_lora')
|
| 38 |
-
# model = model.eval()
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
print("\n" + "=" * 50)
|
| 42 |
print("❌ 模型加载失败!")
|
| 43 |
print(f"错误信息: {e}")
|
|
|
|
| 44 |
raise
|
| 45 |
|
| 46 |
|
| 47 |
@spaces.GPU
|
| 48 |
-
def
|
| 49 |
"""
|
| 50 |
-
|
| 51 |
"""
|
| 52 |
if model is None or tokenizer is None:
|
| 53 |
-
return "❌ 模型未正确加载,请检查Spaces
|
| 54 |
|
| 55 |
try:
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
conversation.append(f"Assistant: {bot_msg}")
|
| 61 |
-
|
| 62 |
-
conversation.append(f"User: {message}")
|
| 63 |
-
conversation.append("Assistant:")
|
| 64 |
-
|
| 65 |
-
prompt = "\n".join(conversation)
|
| 66 |
|
| 67 |
# 编码输入
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# 生成响应
|
| 72 |
with torch.no_grad():
|
| 73 |
-
|
| 74 |
-
**
|
| 75 |
-
|
| 76 |
-
temperature=0.7,
|
| 77 |
-
top_p=0.9,
|
| 78 |
-
do_sample=True,
|
| 79 |
pad_token_id=tokenizer.eos_token_id
|
| 80 |
)
|
| 81 |
|
| 82 |
# 解码输出
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
if "Assistant:" in response:
|
| 87 |
-
response = response.split("Assistant:")[-1].strip()
|
| 88 |
|
| 89 |
-
return response
|
| 90 |
except Exception as e:
|
| 91 |
-
return f"❌
|
| 92 |
|
| 93 |
|
| 94 |
-
# 创建Gradio
|
| 95 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 96 |
gr.Markdown(
|
| 97 |
"""
|
| 98 |
-
#
|
| 99 |
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
| 103 |
"""
|
| 104 |
)
|
| 105 |
|
| 106 |
-
chatbot = gr.Chatbot(
|
| 107 |
-
label="聊天记录",
|
| 108 |
-
height=500,
|
| 109 |
-
bubble_full_width=False
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
with gr.Row():
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
gr.Examples(
|
| 123 |
examples=[
|
| 124 |
-
"
|
| 125 |
-
"
|
| 126 |
-
"
|
| 127 |
-
"
|
|
|
|
|
|
|
| 128 |
],
|
| 129 |
-
inputs=
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
# 事件处理
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
user_msg = history[-1][0]
|
| 138 |
-
bot_response = chat(user_msg, history[:-1])
|
| 139 |
-
history[-1][1] = bot_response
|
| 140 |
-
return history
|
| 141 |
-
|
| 142 |
-
msg.submit(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 143 |
-
bot_message, chatbot, chatbot
|
| 144 |
)
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 149 |
|
|
|
|
| 3 |
import torch
|
| 4 |
from peft import PeftModel
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
from transformers.generation.utils import GenerationConfig
|
| 10 |
# 获取HF token(Spaces会自动提供)
|
| 11 |
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 12 |
|
| 13 |
+
print("=" * 50)
|
| 14 |
+
print("开始加载FinGPT情感分析模型...")
|
| 15 |
+
print("=" * 50)
|
| 16 |
|
| 17 |
model = None
|
| 18 |
tokenizer = None
|
| 19 |
+
device = None
|
| 20 |
|
| 21 |
try:
|
| 22 |
+
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained("Go4miii/DISC-FinLLM", use_fast=False, trust_remote_code=True)
|
| 24 |
+
model = AutoModelForCausalLM.from_pretrained("Go4miii/DISC-FinLLM", device_map="auto", torch_dtype=torch.float16, trust_remote_code=True)
|
| 25 |
+
model.generation_config = GenerationConfig.from_pretrained("Go4miii/DISC-FinLLM")
|
| 26 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
except Exception as e:
|
| 29 |
print("\n" + "=" * 50)
|
| 30 |
print("❌ 模型加载失败!")
|
| 31 |
print(f"错误信息: {e}")
|
| 32 |
+
print("=" * 50)
|
| 33 |
raise
|
| 34 |
|
| 35 |
|
| 36 |
@spaces.GPU
|
| 37 |
+
def analyze_sentiment(news_text):
|
| 38 |
"""
|
| 39 |
+
分析金融新闻的情感倾向
|
| 40 |
"""
|
| 41 |
if model is None or tokenizer is None:
|
| 42 |
+
return "❌ 模型未正确加载,请检查Spaces日志。"
|
| 43 |
|
| 44 |
try:
|
| 45 |
+
# 构建prompt(按照FinGPT的格式)
|
| 46 |
+
prompt = f'''Instruction: What is the sentiment of this news? Please choose an answer from {{negative/neutral/positive}}
|
| 47 |
+
Input: {news_text}
|
| 48 |
+
Answer: '''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# 编码输入
|
| 51 |
+
tokens = tokenizer(
|
| 52 |
+
prompt,
|
| 53 |
+
return_tensors='pt',
|
| 54 |
+
padding=True,
|
| 55 |
+
max_length=512,
|
| 56 |
+
truncation=True
|
| 57 |
+
).to(device)
|
| 58 |
|
| 59 |
# 生成响应
|
| 60 |
with torch.no_grad():
|
| 61 |
+
res = model.generate(
|
| 62 |
+
**tokens,
|
| 63 |
+
max_length=512,
|
|
|
|
|
|
|
|
|
|
| 64 |
pad_token_id=tokenizer.eos_token_id
|
| 65 |
)
|
| 66 |
|
| 67 |
# 解码输出
|
| 68 |
+
res_sentence = tokenizer.decode(res[0], skip_special_tokens=True)
|
| 69 |
+
|
| 70 |
+
# 提取答案
|
| 71 |
+
if "Answer: " in res_sentence:
|
| 72 |
+
sentiment = res_sentence.split("Answer: ")[1].strip()
|
| 73 |
+
# 清理多余的换行和空格
|
| 74 |
+
sentiment = sentiment.split('\n')[0].strip()
|
| 75 |
+
else:
|
| 76 |
+
sentiment = res_sentence
|
| 77 |
|
| 78 |
+
return sentiment
|
|
|
|
|
|
|
| 79 |
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"❌ 分析出错: {str(e)}"
|
| 82 |
|
| 83 |
|
| 84 |
+
# 创建Gradio界面
|
| 85 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 86 |
gr.Markdown(
|
| 87 |
"""
|
| 88 |
+
# 📊 FinGPT 金融新闻情���分析
|
| 89 |
|
| 90 |
+
基于 **FinGPT/fingpt-mt_llama3-8b_lora** 模型的金融新闻情感分析工具。
|
| 91 |
|
| 92 |
+
输入金融新闻文本,AI将分析其情感倾向:**positive(积极)** / **neutral(中性)** / **negative(消极)**
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
+
with gr.Column():
|
| 98 |
+
news_input = gr.Textbox(
|
| 99 |
+
label="📰 输入金融新闻",
|
| 100 |
+
placeholder="粘贴或输入金融新闻内容...",
|
| 101 |
+
lines=6
|
| 102 |
+
)
|
| 103 |
+
analyze_btn = gr.Button("🔍 分析情感", variant="primary", size="lg")
|
| 104 |
|
| 105 |
+
with gr.Column():
|
| 106 |
+
sentiment_output = gr.Textbox(
|
| 107 |
+
label="😊 情感分析结果",
|
| 108 |
+
lines=2
|
| 109 |
+
)
|
| 110 |
|
| 111 |
gr.Examples(
|
| 112 |
examples=[
|
| 113 |
+
"FINANCING OF ASPOCOMP 'S GROWTH Aspocomp is aggressively pursuing its growth strategy by increasingly focusing on technologically more demanding HDI printed circuit boards PCBs.",
|
| 114 |
+
"According to Gran, the company has no plans to move all production to Russia, although that is where the company is growing.",
|
| 115 |
+
"Apple Inc. reported record quarterly revenue of $123.9 billion, up 11% year over year, and quarterly earnings per diluted share of $2.10.",
|
| 116 |
+
"The Federal Reserve announced a 0.75 percentage point interest rate increase, the largest since 1994, to combat rising inflation.",
|
| 117 |
+
"Tesla shares tumbled 12% after the company missed delivery expectations for the third consecutive quarter.",
|
| 118 |
+
"Microsoft and OpenAI announced a multi-year partnership to develop advanced AI technologies, with Microsoft investing $10 billion.",
|
| 119 |
],
|
| 120 |
+
inputs=news_input,
|
| 121 |
+
label="📋 示例新闻(点击使用)"
|
| 122 |
)
|
| 123 |
|
| 124 |
# 事件处理
|
| 125 |
+
analyze_btn.click(
|
| 126 |
+
fn=analyze_sentiment,
|
| 127 |
+
inputs=news_input,
|
| 128 |
+
outputs=sentiment_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
+
|
| 131 |
+
news_input.submit(
|
| 132 |
+
fn=analyze_sentiment,
|
| 133 |
+
inputs=news_input,
|
| 134 |
+
outputs=sentiment_output
|
| 135 |
)
|
| 136 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 137 |
|