Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from utils.pdf_processing import extract_text_from_pdf, extract_text_from_scanned_pdf
|
3 |
+
from utils.vector_store import add_document, search
|
4 |
+
from utils.stock_data import get_stock_data
|
5 |
+
from utils.sentiment import analyze_sentiment
|
6 |
+
|
7 |
+
# Define PDF folder
|
8 |
+
PDF_FOLDER = "data"
|
9 |
+
|
10 |
+
# Process and index all PDFs
|
11 |
+
def process_pdfs():
|
12 |
+
for pdf_file in os.listdir(PDF_FOLDER):
|
13 |
+
pdf_path = os.path.join(PDF_FOLDER, pdf_file)
|
14 |
+
|
15 |
+
# Extract text from normal or scanned PDFs
|
16 |
+
text = extract_text_from_pdf(pdf_path) or extract_text_from_scanned_pdf(pdf_path)
|
17 |
+
|
18 |
+
# Store in FAISS vector store
|
19 |
+
add_document(text, pdf_file)
|
20 |
+
|
21 |
+
# Query the system
|
22 |
+
def query_system(query):
|
23 |
+
results = search(query)
|
24 |
+
|
25 |
+
print("π Top Matching Documents:")
|
26 |
+
for res in results:
|
27 |
+
print(f"\nπ Document: {res.metadata['id']}\n{text[:500]}...")
|
28 |
+
|
29 |
+
# Perform sentiment analysis
|
30 |
+
print("\nπ Sentiment Analysis:")
|
31 |
+
for res in results:
|
32 |
+
sentiment = analyze_sentiment(res.page_content)
|
33 |
+
print(f"π Document: {res.metadata['id']}, Sentiment: {sentiment}")
|
34 |
+
|
35 |
+
# Fetch stock data
|
36 |
+
def get_stock_info(ticker):
|
37 |
+
stock_data = get_stock_data(ticker)
|
38 |
+
print(f"\nπ Stock Price for {ticker}: ${stock_data['price']}")
|
39 |
+
|
40 |
+
# Run the application
|
41 |
+
if __name__ == "__main__":
|
42 |
+
print("π Indexing PDFs...")
|
43 |
+
process_pdfs()
|
44 |
+
|
45 |
+
query = input("\nπ Enter your query: ")
|
46 |
+
query_system(query)
|
47 |
+
|
48 |
+
ticker = input("\nπ Enter a stock ticker (e.g., AAPL, TSLA): ")
|
49 |
+
get_stock_info(ticker)
|