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