Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pymupdf # PyMuPDF for PDF extraction
|
| 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.")
|
| 20 |
+
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") # Load 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 # Return list of text 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 |
+
|
| 50 |
+
if overview_response.status_code == 200:
|
| 51 |
+
overview_data = overview_response.json()
|
| 52 |
+
market_cap = overview_data.get("MarketCapitalization", "N/A")
|
| 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 |
+
|
| 61 |
+
if income_response.status_code == 200:
|
| 62 |
+
income_data = income_response.json()
|
| 63 |
+
annual_reports = income_data.get("annualReports", [])
|
| 64 |
+
revenue = annual_reports[0].get("totalRevenue", "N/A") if annual_reports else "N/A"
|
| 65 |
+
else:
|
| 66 |
+
st.error(f"❌ Failed to fetch income statement. Status Code: {income_response.status_code}")
|
| 67 |
+
return "Error fetching income statement."
|
| 68 |
+
|
| 69 |
+
return f"Market Cap: ${market_cap}\nTotal Revenue: ${revenue}"
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
st.error("❌ Exception in fetching financial data.")
|
| 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]) # Use first few chunks
|
| 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)
|
| 85 |
+
prompt = f"Analyze the financial status of {company_ticker} based on:\n{financial_info}\n\nUser Query: {user_query}"
|
| 86 |
+
else:
|
| 87 |
+
return "Invalid mode selected."
|
| 88 |
+
|
| 89 |
+
response = llm.invoke(prompt)
|
| 90 |
+
return response.content
|
| 91 |
+
except Exception as e:
|
| 92 |
+
st.error("❌ Failed to generate AI response.")
|
| 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 |
+
# User Input Fields
|
| 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.")
|
| 110 |
+
else:
|
| 111 |
+
with st.spinner("Processing... ⏳"):
|
| 112 |
+
response = generate_response(user_query, company_ticker, mode, uploaded_file)
|
| 113 |
+
st.subheader("💡 AI Response")
|
| 114 |
+
st.write(response)
|