nick5363 commited on
Commit
666a17d
·
verified ·
1 Parent(s): a477bd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -8
app.py CHANGED
@@ -1,14 +1,57 @@
 
 
 
1
  from transformers import pipeline
2
  import gradio as gr
3
 
 
4
  pipe = pipeline("text2text-generation", model="google/flan-t5-xl")
5
 
6
- def analyze_news(text):
7
- prompt = f"Tóm tắt và phân tích tin tức tài chính sau: {text}"
8
- result = pipe(prompt, max_length=200)[0]["generated_text"]
9
- return result
 
 
 
10
 
11
- gr.Interface(fn=analyze_news,
12
- inputs=gr.Textbox(lines=8, placeholder="Dán nội dung tin tức Yahoo Finance ở đây..."),
13
- outputs="text",
14
- title="Phân tích Tin tức Tài chính (FREE)").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
  from transformers import pipeline
5
  import gradio as gr
6
 
7
+ # Load mô hình miễn phí
8
  pipe = pipeline("text2text-generation", model="google/flan-t5-xl")
9
 
10
+ # Hàm fetch nội dung bài báo từ link
11
+ def fetch_article(url):
12
+ headers = {"User-Agent": "Mozilla/5.0"}
13
+ r = requests.get(url, headers=headers)
14
+ soup = BeautifulSoup(r.text, "html.parser")
15
+ paragraphs = soup.find_all("p")
16
+ return " ".join(p.text for p in paragraphs)
17
 
18
+ # Hàm xử lý chính
19
+ def auto_analyze_news(ticker="SPY"):
20
+ stock = yf.Ticker(ticker)
21
+ news = stock.news
22
+
23
+ if not news:
24
+ return "Không có tin tức mới nào cho mã này."
25
+
26
+ article = news[0] # lấy tin mới nhất
27
+ url = article['link']
28
+ title = article['title']
29
+
30
+ try:
31
+ content = fetch_article(url)
32
+ except:
33
+ return f"Không thể lấy nội dung từ bài: {title}"
34
+
35
+ prompt = f"""
36
+ Tin tức: {title}
37
+
38
+ {content}
39
+
40
+ Hãy phân tích:
41
+ 1. Đây là tin Bullish, Bearish hay Neutral?
42
+ 2. Ảnh hưởng thế nào đến {ticker}, SPY hoặc QQQ?
43
+ 3. Tóm tắt ngắn nội dung bài viết.
44
+ """
45
+ result = pipe(prompt, max_length=512)[0]["generated_text"]
46
+ return f"**Tiêu đề:** {title}\n\n**Phân tích AI:**\n{result}"
47
+
48
+ # Gradio giao diện
49
+ demo = gr.Interface(
50
+ fn=auto_analyze_news,
51
+ inputs=gr.Textbox(value="SPY", label="Ticker cần lấy tin (mặc định SPY)"),
52
+ outputs="text",
53
+ title="AI Tự động Phân tích Tin tài chính mới nhất",
54
+ description="Bot sẽ tự lấy tin mới nhất từ Yahoo Finance qua yfinance, phân tích xu hướng và ảnh hưởng đến SPY/QQQ."
55
+ )
56
+
57
+ demo.launch()