add fingraph v.10 code
Browse files
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
README.md
CHANGED
@@ -1,14 +1,26 @@
|
|
1 |
---
|
2 |
-
title: Fingraph
|
3 |
-
emoji: 🏢
|
4 |
-
colorFrom: blue
|
5 |
-
colorTo: yellow
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.31.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: mit
|
11 |
short_description: The fortune teller of financial trends.
|
12 |
-
---
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Fingraph | sdk: gradio | sdk_version: 5.31.0 |license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
short_description: The fortune teller of financial trends.
|
|
|
4 |
|
5 |
+
# Financial Analyst Agent (LLaMA 3.1 + LangChain Style)
|
6 |
+
|
7 |
+
A lightweight, interactive HuggingFace Space that fetches financial news articles, summarizes them using LLaMA 3.1, and analyzes stock trends.
|
8 |
+
|
9 |
+
## Features
|
10 |
+
- Google News scraping from Reuters
|
11 |
+
- Article summarization via LLM
|
12 |
+
- Trend analysis on recent stock prices
|
13 |
+
- All wrapped in a clean Gradio interface
|
14 |
+
|
15 |
+
## How It Works
|
16 |
+
1. Enter a finance-related search term (e.g. "tech stocks").
|
17 |
+
2. Enter a stock symbol (e.g. "AAPL").
|
18 |
+
3. The app fetches relevant news and summarizes it.
|
19 |
+
4. It pulls recent Yahoo Finance data and analyzes it using the LLM.
|
20 |
+
|
21 |
+
## Setup
|
22 |
+
pip install -r requirements.txt
|
23 |
+
python app.py
|
24 |
+
|
25 |
+
Built with ❤️ using Hugging Face, Gradio, and LLaMA 3.1
|
26 |
+
---
|
app.py
CHANGED
@@ -1,7 +1,71 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import pandas as pd
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
import gradio as gr
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
6 |
|
7 |
+
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
|
10 |
+
llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
|
11 |
|
12 |
+
def run_llm(prompt: str):
|
13 |
+
result = llm_pipeline(prompt)[0]["generated_text"]
|
14 |
+
return result.replace(prompt, "")
|
15 |
+
|
16 |
+
def fetch_financial_news(query="markets", max_articles=3):
|
17 |
+
url = f"https://www.google.com/search?q={query}+site:reuters.com&tbm=nws"
|
18 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
19 |
+
response = requests.get(url, headers=headers)
|
20 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
21 |
+
links = []
|
22 |
+
for g in soup.find_all('a', href=True):
|
23 |
+
href = g['href']
|
24 |
+
if "reuters.com" in href and len(links) < max_articles:
|
25 |
+
links.append(href.split("&")[0].replace("/url?q=", ""))
|
26 |
+
return links
|
27 |
+
|
28 |
+
def summarize_news_article(url):
|
29 |
+
try:
|
30 |
+
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
31 |
+
soup = BeautifulSoup(r.text, "html.parser")
|
32 |
+
paragraphs = soup.find_all('p')
|
33 |
+
text = "\n".join(p.get_text() for p in paragraphs[:10])
|
34 |
+
prompt = f"You are a financial analyst. Summarize the key points from this article:\n\n{text}\n\nReturn a concise summary suitable for investors."
|
35 |
+
return run_llm(prompt)
|
36 |
+
except Exception as e:
|
37 |
+
return f"Failed to summarize article: {e}"
|
38 |
+
|
39 |
+
def analyze_stock_data(symbol="AAPL"):
|
40 |
+
try:
|
41 |
+
url = f"https://query1.finance.yahoo.com/v7/finance/download/{symbol}?period1=1682899200&period2=1685577600&interval=1d&events=history"
|
42 |
+
df = pd.read_csv(url)
|
43 |
+
df["Date"] = pd.to_datetime(df["Date"])
|
44 |
+
closing_prices = df[["Date", "Close"]].tail(10)
|
45 |
+
data_str = closing_prices.to_string(index=False)
|
46 |
+
prompt = f"You're a financial analyst. Given the following recent closing prices of {symbol}, analyze the trend and summarize in plain English:\n\n{data_str}"
|
47 |
+
return run_llm(prompt)
|
48 |
+
except Exception as e:
|
49 |
+
return f"Failed to fetch stock data: {e}"
|
50 |
+
|
51 |
+
def analyze(query, stock_symbol):
|
52 |
+
output = ""
|
53 |
+
output += "📈 Fetching Financial News...\n"
|
54 |
+
urls = fetch_financial_news(query)
|
55 |
+
for url in urls:
|
56 |
+
output += f"\n📰 {url}\n"
|
57 |
+
output += summarize_news_article(url) + "\n"
|
58 |
+
output += "\n📊 Analyzing Stock Trends...\n"
|
59 |
+
output += analyze_stock_data(stock_symbol)
|
60 |
+
return output
|
61 |
+
|
62 |
+
gr.Interface(
|
63 |
+
fn=analyze,
|
64 |
+
inputs=[
|
65 |
+
gr.Textbox(label="Financial News Topic", value="tech stocks"),
|
66 |
+
gr.Textbox(label="Stock Symbol", value="AAPL")
|
67 |
+
],
|
68 |
+
outputs=gr.Textbox(label="Financial Summary", lines=20),
|
69 |
+
title="🧠 Financial Analyst Agent (LLaMA 3.1 + LangChain Style)",
|
70 |
+
description="Summarizes financial news and stock data using LLaMA 3.1 + LangChain-style prompts."
|
71 |
+
).launch()
|
requirements.txt
ADDED
File without changes
|