Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,15 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
-
import pymupdf
|
4 |
import traceback
|
5 |
from sentence_transformers import SentenceTransformer
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
from langchain_groq import ChatGroq
|
8 |
|
9 |
-
# Load API keys from Streamlit secrets
|
10 |
ALPHA_VANTAGE_API_KEY = st.secrets["ALPHA_VANTAGE_API_KEY"]
|
11 |
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
12 |
-
|
13 |
-
# Initialize Sentence Transformer for embeddings
|
14 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
15 |
|
16 |
-
# Initialize LLM
|
17 |
try:
|
18 |
llm = ChatGroq(temperature=0, model="llama3-70b-8192", api_key=GROQ_API_KEY)
|
19 |
st.success("β
Groq LLM initialized successfully.")
|
@@ -21,29 +17,24 @@ except Exception as e:
|
|
21 |
st.error("β Failed to initialize Groq LLM.")
|
22 |
traceback.print_exc()
|
23 |
|
24 |
-
# Function to extract and chunk text from PDFs
|
25 |
def extract_text_from_pdf(uploaded_file, max_length=5000):
|
26 |
try:
|
27 |
-
doc = pymupdf.open(stream=uploaded_file.read(), filetype="pdf")
|
28 |
full_text = "".join(page.get_text() for page in doc)
|
29 |
-
|
30 |
-
# Split text into chunks to avoid LLM token limits
|
31 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=max_length, chunk_overlap=200)
|
32 |
chunks = text_splitter.split_text(full_text)
|
33 |
|
34 |
-
return chunks
|
35 |
except Exception as e:
|
36 |
st.error("β Failed to extract text from PDF.")
|
37 |
traceback.print_exc()
|
38 |
return ["Error extracting text."]
|
39 |
|
40 |
-
# Function to fetch financial data from Alpha Vantage
|
41 |
def fetch_financial_data(company_ticker):
|
42 |
if not company_ticker:
|
43 |
return "No ticker symbol provided. Please enter a valid company ticker."
|
44 |
|
45 |
try:
|
46 |
-
# Fetch Market Cap from Company Overview
|
47 |
overview_url = f"https://www.alphavantage.co/query?function=OVERVIEW&symbol={company_ticker}&apikey={ALPHA_VANTAGE_API_KEY}"
|
48 |
overview_response = requests.get(overview_url)
|
49 |
|
@@ -53,8 +44,6 @@ def fetch_financial_data(company_ticker):
|
|
53 |
else:
|
54 |
st.error(f"β Failed to fetch company overview. Status Code: {overview_response.status_code}")
|
55 |
return "Error fetching company overview."
|
56 |
-
|
57 |
-
# Fetch Revenue from Income Statement
|
58 |
income_url = f"https://www.alphavantage.co/query?function=INCOME_STATEMENT&symbol={company_ticker}&apikey={ALPHA_VANTAGE_API_KEY}"
|
59 |
income_response = requests.get(income_url)
|
60 |
|
@@ -73,12 +62,11 @@ def fetch_financial_data(company_ticker):
|
|
73 |
traceback.print_exc()
|
74 |
return "Error fetching financial data."
|
75 |
|
76 |
-
# Function to generate response using Groq's LLM
|
77 |
def generate_response(user_query, company_ticker, mode, uploaded_file):
|
78 |
try:
|
79 |
if mode == "PDF Upload Mode":
|
80 |
chunks = extract_text_from_pdf(uploaded_file)
|
81 |
-
chunked_summary = "\n\n".join(chunks[:3])
|
82 |
prompt = f"Summarize the key financial insights from this document:\n\n{chunked_summary}"
|
83 |
elif mode == "Live Data Mode":
|
84 |
financial_info = fetch_financial_data(company_ticker)
|
@@ -93,17 +81,15 @@ def generate_response(user_query, company_ticker, mode, uploaded_file):
|
|
93 |
traceback.print_exc()
|
94 |
return "Error generating response."
|
95 |
|
96 |
-
# Streamlit UI
|
97 |
st.title("π AI-Powered Financial Insights Chatbot")
|
98 |
st.write("Upload financial reports or fetch live financial data to get AI-driven insights.")
|
99 |
|
100 |
-
|
101 |
user_query = st.text_input("Enter your query:")
|
102 |
company_ticker = st.text_input("Enter company ticker symbol (optional):")
|
103 |
mode = st.radio("Select Mode:", ["PDF Upload Mode", "Live Data Mode"])
|
104 |
uploaded_file = st.file_uploader("Upload PDF (Only for PDF Mode)", type=["pdf"])
|
105 |
|
106 |
-
# Button to process request
|
107 |
if st.button("Get Insights"):
|
108 |
if mode == "PDF Upload Mode" and not uploaded_file:
|
109 |
st.error("β Please upload a PDF file.")
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
import pymupdf
|
4 |
import traceback
|
5 |
from sentence_transformers import SentenceTransformer
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
from langchain_groq import ChatGroq
|
8 |
|
|
|
9 |
ALPHA_VANTAGE_API_KEY = st.secrets["ALPHA_VANTAGE_API_KEY"]
|
10 |
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
|
|
|
|
11 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
12 |
|
|
|
13 |
try:
|
14 |
llm = ChatGroq(temperature=0, model="llama3-70b-8192", api_key=GROQ_API_KEY)
|
15 |
st.success("β
Groq LLM initialized successfully.")
|
|
|
17 |
st.error("β Failed to initialize Groq LLM.")
|
18 |
traceback.print_exc()
|
19 |
|
|
|
20 |
def extract_text_from_pdf(uploaded_file, max_length=5000):
|
21 |
try:
|
22 |
+
doc = pymupdf.open(stream=uploaded_file.read(), filetype="pdf")
|
23 |
full_text = "".join(page.get_text() for page in doc)
|
|
|
|
|
24 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=max_length, chunk_overlap=200)
|
25 |
chunks = text_splitter.split_text(full_text)
|
26 |
|
27 |
+
return chunks
|
28 |
except Exception as e:
|
29 |
st.error("β Failed to extract text from PDF.")
|
30 |
traceback.print_exc()
|
31 |
return ["Error extracting text."]
|
32 |
|
|
|
33 |
def fetch_financial_data(company_ticker):
|
34 |
if not company_ticker:
|
35 |
return "No ticker symbol provided. Please enter a valid company ticker."
|
36 |
|
37 |
try:
|
|
|
38 |
overview_url = f"https://www.alphavantage.co/query?function=OVERVIEW&symbol={company_ticker}&apikey={ALPHA_VANTAGE_API_KEY}"
|
39 |
overview_response = requests.get(overview_url)
|
40 |
|
|
|
44 |
else:
|
45 |
st.error(f"β Failed to fetch company overview. Status Code: {overview_response.status_code}")
|
46 |
return "Error fetching company overview."
|
|
|
|
|
47 |
income_url = f"https://www.alphavantage.co/query?function=INCOME_STATEMENT&symbol={company_ticker}&apikey={ALPHA_VANTAGE_API_KEY}"
|
48 |
income_response = requests.get(income_url)
|
49 |
|
|
|
62 |
traceback.print_exc()
|
63 |
return "Error fetching financial data."
|
64 |
|
|
|
65 |
def generate_response(user_query, company_ticker, mode, uploaded_file):
|
66 |
try:
|
67 |
if mode == "PDF Upload Mode":
|
68 |
chunks = extract_text_from_pdf(uploaded_file)
|
69 |
+
chunked_summary = "\n\n".join(chunks[:3])
|
70 |
prompt = f"Summarize the key financial insights from this document:\n\n{chunked_summary}"
|
71 |
elif mode == "Live Data Mode":
|
72 |
financial_info = fetch_financial_data(company_ticker)
|
|
|
81 |
traceback.print_exc()
|
82 |
return "Error generating response."
|
83 |
|
|
|
84 |
st.title("π AI-Powered Financial Insights Chatbot")
|
85 |
st.write("Upload financial reports or fetch live financial data to get AI-driven insights.")
|
86 |
|
87 |
+
|
88 |
user_query = st.text_input("Enter your query:")
|
89 |
company_ticker = st.text_input("Enter company ticker symbol (optional):")
|
90 |
mode = st.radio("Select Mode:", ["PDF Upload Mode", "Live Data Mode"])
|
91 |
uploaded_file = st.file_uploader("Upload PDF (Only for PDF Mode)", type=["pdf"])
|
92 |
|
|
|
93 |
if st.button("Get Insights"):
|
94 |
if mode == "PDF Upload Mode" and not uploaded_file:
|
95 |
st.error("β Please upload a PDF file.")
|