vikpande commited on
Commit
09f258b
Β·
1 Parent(s): 61e0819

fix: switch to public Nous-Hermes model for tokenizer compatibility

Browse files
Files changed (2) hide show
  1. app.py +12 -4
  2. requirements.txt +1 -2
app.py CHANGED
@@ -1,18 +1,23 @@
 
 
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 = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
 
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"}
@@ -25,6 +30,7 @@ def fetch_financial_news(query="markets", max_articles=3):
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"})
@@ -36,6 +42,7 @@ def summarize_news_article(url):
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"
@@ -48,6 +55,7 @@ def analyze_stock_data(symbol="AAPL"):
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"
@@ -66,6 +74,6 @@ gr.Interface(
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()
 
1
+ # app.py β€” Financial News + Stock Analyzer with Nous-Hermes-2 (Mistral)
2
+
3
  import requests
4
  import pandas as pd
5
  from bs4 import BeautifulSoup
6
  import gradio as gr
7
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
8
 
9
+ # βœ… Public, compatible model
10
+ model_id = "NousResearch/Nous-Hermes-2-Mistral-7B-DPO"
11
  tokenizer = AutoTokenizer.from_pretrained(model_id)
12
+ model = AutoModelForCausalLM.from_pretrained(model_id)
13
+
14
  llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
15
 
16
  def run_llm(prompt: str):
17
  result = llm_pipeline(prompt)[0]["generated_text"]
18
  return result.replace(prompt, "")
19
 
20
+ # ----------- Financial News Fetcher -----------
21
  def fetch_financial_news(query="markets", max_articles=3):
22
  url = f"https://www.google.com/search?q={query}+site:reuters.com&tbm=nws"
23
  headers = {"User-Agent": "Mozilla/5.0"}
 
30
  links.append(href.split("&")[0].replace("/url?q=", ""))
31
  return links
32
 
33
+ # ----------- News Article Summarizer -----------
34
  def summarize_news_article(url):
35
  try:
36
  r = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
 
42
  except Exception as e:
43
  return f"Failed to summarize article: {e}"
44
 
45
+ # ----------- Stock Data Analyzer -----------
46
  def analyze_stock_data(symbol="AAPL"):
47
  try:
48
  url = f"https://query1.finance.yahoo.com/v7/finance/download/{symbol}?period1=1682899200&period2=1685577600&interval=1d&events=history"
 
55
  except Exception as e:
56
  return f"Failed to fetch stock data: {e}"
57
 
58
+ # ----------- Gradio App Interface -----------
59
  def analyze(query, stock_symbol):
60
  output = ""
61
  output += "πŸ“ˆ Fetching Financial News...\n"
 
74
  gr.Textbox(label="Stock Symbol", value="AAPL")
75
  ],
76
  outputs=gr.Textbox(label="Financial Summary", lines=20),
77
+ title="🧠 Fingraph β€” Financial Analyst Agent",
78
+ description="Summarizes news + stock trends using Nous-Hermes-2 (Mistral) and LLM insights."
79
  ).launch()
requirements.txt CHANGED
@@ -3,5 +3,4 @@ torch
3
  pandas
4
  beautifulsoup4
5
  requests
6
- gradio
7
- tiktoken
 
3
  pandas
4
  beautifulsoup4
5
  requests
6
+ gradio