Spaces:
Runtime error
Runtime error
File size: 5,878 Bytes
e75f8d6 | 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 77 78 79 80 81 82 83 84 85 86 87 88 | !pip install gradio openai pypdf langchain
from openai import OpenAI
from langchain.document_loaders import PyPDFLoader
import gradio as gr
# Set up the OpenAI client with the custom base URL and API key
client = OpenAI(
base_url="https://paid-ddc.xiolabs.xyz/v1",
api_key="ddc-paid-bf02fc6c4d7d40b7b7a521c6ab45ac5e"
)
# Define the custom prompt template
prompt_template = """
Use the following format:
📊 [Stock Name]
Type: [e.g., Financial Results, Corporate Announcement, Board Meeting Outcome, quarterly results, annual results, AGM, Qualified Institutional Placement, Credit Rating,Allotment of ESOPchange in management, monthly sales, Analyst/Investor meet, (LODR)-Award_of_Order, Receipt_of_Order, monthly business update, amalgamation, new plant, breakthrough, new unit, credit rating, new facility, delisting, demerger, Acquisition,order win, establishment inspection report, record time, expansion, joint venture, restructuring, merger, USFDA, debt reduction, investor presentation, business outlook update, financial report, strategic partnership, regulatory approval, product launch]
Sentiment: [Positive/Negative/Neutral]
strictly leave a one line gap
📌STOCKER Summary:
[You are the Editor-in-Chief of leading financial publications like The Economic Times, Moneycontrol, and Business Standard, with experience from top global investment banks (JP Morgan, Bank of America Securities, Morgan Stanley, Citigroup, HSBC, BNP Paribas, Goldman Sachs, Barclays Investment Bank, UBS Group AG, Société Générale). Use your extensive expertise to identify and extract all material financial and strategic developments that institutional investors would prioritize. Focus on actionable insights aligned with institutional-grade analysis.
Your task is to summarize the following stock filing in not more than 60-70 words, structured as a single, coherent paragraph, using the format below.
Ensure the summary is clear, actionable, and free of fluff. You must include sentiment analysis (Positive/Negative/Neutral) and add a short impact statement within the same paragraph if the news significantly affects the company's future; otherwise, omit it. Do not miss any critical data, such as key numbers, percentages, or strategic details that are important for investors.Provide a concise, 60-word summary of the filing in one paragraph. Ensure every word adds value for the investor. If the development is material, integrate a short impact statement within the paragraph explaining how it could affect the company's future.
Guidelines:
1. Scrape the PDF thoroughly: Check every page for all critical data—numbers, stats, strategic decisions, risks, etc. Include all material details that could impact investor decisions.
2. Prioritize information directly affecting investors.
3. Avoid jargon or overly technical terms unless absolutely necessary.
4. Use an authoritative yet accessible tone.
5. Ensure the summary is balanced and unbiased.
6. The impact statement (if included) must be integrated within the summary paragraph.
8. Highlight any new updates or developments that could influence investor perception or decision-making.
9. Contextualize all financial metrics to provide a clear picture of the company’s trajectory.
10. If any amount is greater than 99 lakhs , convert the unit from lakhs to crores.
12.In filings of monthly sales always include the sum of domestic and export sales.
13.If the filing is a quarterly sales report do a YOY comparison between this year and last year for sales, EBITDA AND PAT respectively along with %change in (brackets) and also mention the Quarter like Q3 FY25 ,Q4 FY26 ETC.
14.If the filing is a annual sales report do a YOY comparison between this year and last year for sales, EBITDA AND PAT respectively along with %change in (brackets) and also mention the financial year or any year like FY24 , FY23, FY27 ETC.
15.Leave one line after the summary and then mention the source and then leave one line after source and then mention 🚀Instant stock updates delivered by Stocker.]
Source: {source}
{text}
🚀Instant stock updates delivered by Stocker
"""
def extract_metadata_and_summarize(pdf_file):
# Load and process the PDF
loader = PyPDFLoader(pdf_file.name)
docs = loader.load()
combined_text = " ".join([doc.page_content for doc in docs])
# Extract metadata (basic logic for demonstration purposes)
# In a real-world scenario, you'd use NLP or regex to extract these details
stock_name = "Confidence Futuristic Energtech Limited" # Example extraction
report_type = "Financial Results" # Example extraction
sentiment = "Positive" # Example extraction
source = pdf_file.name # Use the file name as the source
# Prepare the prompt
prompt = prompt_template.format(
stock_name=stock_name,
report_type=report_type,
sentiment=sentiment,
source=source,
text=combined_text
)
# Call the OpenAI API to generate the summary
response = client.chat.completions.create(
model="gpt-4o", # Specify the model you want to use
messages=[
{"role": "system", "content": "You are a financial analyst summarizing stock filings."},
{"role": "user", "content": prompt}
],
temperature=0,
max_tokens=150 # Limit the response length
)
# Extract the summary from the response
summary = response.choices[0].message.content.strip()
return summary
# Create Gradio interface
inputs = [
gr.File(label="Upload PDF File")
]
outputs = gr.Textbox(label="Summary")
interface = gr.Interface(
fn=extract_metadata_and_summarize,
inputs=inputs,
outputs=outputs,
title="📈 Stocker - Stalk your Stock",
description="Upload a financial PDF.",
allow_flagging="never"
)
if __name__ == "__main__":
interface.launch(share=True)
|