Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,76 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
results = search(query)
|
24 |
|
25 |
-
|
26 |
for res in results:
|
27 |
-
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
for res in results:
|
32 |
sentiment = analyze_sentiment(res.page_content)
|
33 |
-
|
34 |
|
35 |
-
|
|
|
|
|
|
|
36 |
def get_stock_info(ticker):
|
|
|
37 |
stock_data = get_stock_data(ticker)
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
process_pdfs()
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
+
from extract_pdf import extract_text_from_pdf, extract_text_from_scanned_pdf
|
4 |
+
from vector_store import add_document, search
|
5 |
+
from stock_data import get_stock_data
|
6 |
+
from sentiment import analyze_sentiment
|
7 |
+
|
8 |
+
# Create folders if not exist
|
9 |
+
DATA_FOLDER = "data"
|
10 |
+
FAISS_FOLDER = "faiss_index"
|
11 |
+
os.makedirs(DATA_FOLDER, exist_ok=True)
|
12 |
+
os.makedirs(FAISS_FOLDER, exist_ok=True)
|
13 |
+
|
14 |
+
|
15 |
+
# 1οΈβ£ Process uploaded PDF and add to FAISS
|
16 |
+
def process_pdf(file):
|
17 |
+
"""Process uploaded PDF and add to FAISS"""
|
18 |
+
file_path = os.path.join(DATA_FOLDER, file.name)
|
19 |
+
with open(file_path, "wb") as f:
|
20 |
+
f.write(file.read())
|
21 |
+
|
22 |
+
# Extract text using available methods
|
23 |
+
text = extract_text_from_pdf(file_path) or extract_text_from_scanned_pdf(file_path)
|
24 |
+
add_document(text, file.name)
|
25 |
+
return f"β
PDF '{file.name}' indexed successfully!"
|
26 |
+
|
27 |
+
|
28 |
+
# 2οΈβ£ Search FAISS for relevant PDFs
|
29 |
+
def query_pdf(query):
|
30 |
+
"""Search FAISS index for relevant documents."""
|
31 |
results = search(query)
|
32 |
|
33 |
+
response = "π **Top Matching Documents:**\n\n"
|
34 |
for res in results:
|
35 |
+
response += f"π **{res.metadata['id']}**\n{text[:500]}...\n\n"
|
36 |
|
37 |
+
# Sentiment analysis
|
38 |
+
response += "\nπ **Sentiment Analysis:**\n"
|
39 |
for res in results:
|
40 |
sentiment = analyze_sentiment(res.page_content)
|
41 |
+
response += f"π **{res.metadata['id']}** β Sentiment: {sentiment}\n"
|
42 |
|
43 |
+
return response
|
44 |
+
|
45 |
+
|
46 |
+
# 3οΈβ£ Fetch Live Stock Market Data
|
47 |
def get_stock_info(ticker):
|
48 |
+
"""Fetch real-time stock price from Yahoo Finance."""
|
49 |
stock_data = get_stock_data(ticker)
|
50 |
+
return f"\nπ Stock Price for {ticker}: ${stock_data['price']}"
|
51 |
+
|
52 |
+
|
53 |
+
# Gradio UI
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("# Stock Analysis PDF Indexer")
|
56 |
+
|
57 |
+
# File Upload for PDFs
|
58 |
+
pdf_upload = gr.File(label="Upload PDFs", file_count="multiple")
|
59 |
+
upload_button = gr.Button("Upload and Index PDF")
|
60 |
+
upload_message = gr.Textbox(label="Upload Status", interactive=False)
|
61 |
+
|
62 |
+
# PDF Query Input
|
63 |
+
query_input = gr.Textbox(label="Query PDFs")
|
64 |
+
query_output = gr.Textbox(label="Query Results", interactive=False)
|
65 |
|
66 |
+
# Stock Ticker Input
|
67 |
+
stock_input = gr.Textbox(label="Enter Stock Ticker (e.g., AAPL)")
|
68 |
+
stock_output = gr.Textbox(label="Stock Data", interactive=False)
|
|
|
69 |
|
70 |
+
# Actions
|
71 |
+
upload_button.click(process_pdf, inputs=pdf_upload, outputs=upload_message)
|
72 |
+
query_input.submit(query_pdf, inputs=query_input, outputs=query_output)
|
73 |
+
stock_input.submit(get_stock_info, inputs=stock_input, outputs=stock_output)
|
74 |
|
75 |
+
# Launch the Gradio interface
|
76 |
+
demo.launch()
|