genaibeauty commited on
Commit
8d31bb6
Β·
verified Β·
1 Parent(s): 5e5e6b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -36
app.py CHANGED
@@ -1,49 +1,76 @@
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)
 
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()