Spaces:
Runtime error
Runtime error
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() | |