Download src/app.py from Ai-Bharti/pdf_data: direct link, hf CLI and curl.
- Browser
- Download file 8.3 kB
-
https://huggingface.co/spaces/Ai-Bharti/pdf_data/resolve/main/src/app.py
- Command line
-
hf download hf://spaces/Ai-Bharti/pdf_data/src/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ai-Bharti/pdf_data/resolve/main/src/app.py
8.3 kB
| import gradio as gr | |
| import pandas as pd | |
| from datetime import datetime | |
| import time | |
| import os | |
| from scraper import get_pdf_urls_from_screener | |
| from pdf_reader import extract_text_from_bse_pdf, extract_key_info | |
| # ============================================================ | |
| # HuggingFace Inference API use karenge (FREE!) | |
| # ============================================================ | |
| import requests as req | |
| HF_API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn" | |
| def summarize_via_hf_api(text, hf_token): | |
| """HuggingFace ke FREE API se summary banata hai.""" | |
| if not hf_token or hf_token.strip() == "": | |
| return "β οΈ HuggingFace token dalo pehle!" | |
| short_text = " ".join(text.split()[:700]) | |
| if len(short_text) < 50: | |
| return "Text bahut chhota hai - summary nahi ban sakti" | |
| headers = {"Authorization": f"Bearer {hf_token}"} | |
| payload = { | |
| "inputs": short_text, | |
| "parameters": {"max_length": 130, "min_length": 40, "do_sample": False} | |
| } | |
| try: | |
| response = req.post(HF_API_URL, headers=headers, json=payload, timeout=30) | |
| if response.status_code == 503: | |
| time.sleep(10) | |
| response = req.post(HF_API_URL, headers=headers, json=payload, timeout=30) | |
| result = response.json() | |
| if isinstance(result, list) and result: | |
| return result[0].get("summary_text", "Summary nahi aayi") | |
| elif isinstance(result, dict) and "error" in result: | |
| return f"API Error: {result['error']}" | |
| return str(result) | |
| except Exception as e: | |
| return f"β API call failed: {str(e)}" | |
| # ============================================================ | |
| # MAIN FUNCTION | |
| # ============================================================ | |
| def run_scraper(page_number, hf_token, max_pdfs, session_id, csrf_token): | |
| """ | |
| Poora pipeline: Screener β PDF URLs β Text β AI Summary β Table | |
| session_id / csrf_token: | |
| - Agar UI mein dala hai β woh use hoga | |
| - Agar khali chhoda β HuggingFace Secrets se automatically aayega | |
| """ | |
| logs = [] | |
| def log(msg): | |
| logs.append(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}") | |
| return "\n".join(logs) | |
| # --- Cookies decide karo --- | |
| # Priority: UI input > HuggingFace Secrets > kuch nahi (guest mode) | |
| cookies = {} | |
| if session_id and session_id.strip(): | |
| # User ne UI mein dala | |
| cookies = { | |
| "sessionid": session_id.strip(), | |
| "csrftoken": csrf_token.strip() if csrf_token else "" | |
| } | |
| current_log = log("π UI se cookies use ho rahi hain") | |
| elif os.environ.get("SCREENER_SESSION_ID"): | |
| # HuggingFace Secrets se auto-load | |
| cookies = { | |
| "sessionid": os.environ.get("SCREENER_SESSION_ID", ""), | |
| "csrftoken": os.environ.get("SCREENER_CSRF_TOKEN", "") | |
| } | |
| current_log = log("π HuggingFace Secrets se cookies load hui!") | |
| else: | |
| current_log = log("β οΈ Cookies nahi mili - guest mode mein chalega") | |
| yield current_log, None, None | |
| # Step 1 - Screener se PDF links nikalo | |
| current_log = log(f"π Screener.in page {int(page_number)} se links nikal raha hoon...") | |
| yield current_log, None, None | |
| pdf_data = get_pdf_urls_from_screener(page_num=int(page_number), cookies=cookies if cookies else None) | |
| if not pdf_data: | |
| current_log = log("β Koi PDF links nahi mile!") | |
| current_log = log("π‘ Tip: Cookies check karo ya Screener pe login karo") | |
| yield current_log, None, None | |
| return | |
| total = min(len(pdf_data), int(max_pdfs)) | |
| current_log = log(f"β {len(pdf_data)} PDF links mile! Pehle {total} process karunga.") | |
| yield current_log, None, None | |
| # Step 2 - Har PDF process karo | |
| results = [] | |
| for i, (company, pdf_url) in enumerate(pdf_data[:total]): | |
| current_log = log(f"π [{i+1}/{total}] {pdf_url[:65]}...") | |
| yield current_log, None, None | |
| text = extract_text_from_bse_pdf(pdf_url) | |
| if "β" in text: | |
| current_log = log(" β οΈ Skip - PDF read nahi hua") | |
| yield current_log, None, None | |
| continue | |
| key_info = extract_key_info(text) | |
| current_log = log(" π€ AI Summary ban raha hai...") | |
| yield current_log, None, None | |
| summary = summarize_via_hf_api(text, hf_token) | |
| results.append({ | |
| "π Date": datetime.now().strftime("%Y-%m-%d"), | |
| "π’ Company": company[:40], | |
| "π Order Type": key_info.get("order_type", "Order"), | |
| "π° Amounts": ", ".join(key_info.get("amounts_found", []))[:60], | |
| "π€ AI Summary": summary, | |
| "π PDF URL": pdf_url | |
| }) | |
| current_log = log(" β Done!") | |
| yield current_log, None, None | |
| time.sleep(1) | |
| # Step 3 - Results ready | |
| if not results: | |
| current_log = log("π Koi results nahi aayi!") | |
| yield current_log, None, None | |
| return | |
| df = pd.DataFrame(results) | |
| csv_path = f"/tmp/orders_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv" | |
| df.to_csv(csv_path, index=False) | |
| current_log = log(f"π COMPLETE! {len(results)} orders process hue!") | |
| current_log = log("πΎ CSV ready for download!") | |
| yield current_log, df, csv_path | |
| # ============================================================ | |
| # GRADIO UI | |
| # ============================================================ | |
| with gr.Blocks(title="π BSE Order Tracker", theme=gr.themes.Soft()) as app: | |
| gr.Markdown(""" | |
| # π BSE Order Tracker | |
| ### Screener.in se orders dhundo β PDF padhho β AI Summary pao! | |
| """) | |
| # --- Row 1: HF Token + Page + Count --- | |
| with gr.Row(): | |
| hf_token_input = gr.Textbox( | |
| label="π HuggingFace API Token", | |
| placeholder="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
| type="password", | |
| scale=3, | |
| info="huggingface.co β Settings β Access Tokens β New Token (Read)" | |
| ) | |
| page_input = gr.Slider( | |
| label="π Screener Page", minimum=1, maximum=50, value=1, step=1, scale=1 | |
| ) | |
| max_pdf_input = gr.Slider( | |
| label="π Kitne PDFs?", minimum=1, maximum=20, value=5, step=1, scale=1 | |
| ) | |
| # --- Row 2: Screener Cookies (Optional - agar Secrets set hain toh khali chhodo) --- | |
| with gr.Accordion("πͺ Screener Cookies (Optional - HuggingFace Secrets use karo)", open=False): | |
| gr.Markdown(""" | |
| > **Yeh tab use karo jab:** Aap locally test kar rahe ho ya Secrets set nahi kiye hain. | |
| > | |
| > **Recommended:** HuggingFace Space β **Settings β Secrets** mein daalo: | |
| > - `SCREENER_SESSION_ID` | |
| > - `SCREENER_CSRF_TOKEN` | |
| > | |
| > Tab yeh boxes khali chhod sakte ho - automatically load ho jaayega! π | |
| """) | |
| with gr.Row(): | |
| session_id_input = gr.Textbox( | |
| label="πͺ sessionid", | |
| placeholder="Screener F12 β Application β Cookies β sessionid", | |
| type="password" | |
| ) | |
| csrf_token_input = gr.Textbox( | |
| label="πͺ csrftoken", | |
| placeholder="Screener F12 β Application β Cookies β csrftoken", | |
| type="password" | |
| ) | |
| run_btn = gr.Button("π Scraping Shuru Karo!", variant="primary", size="lg") | |
| gr.Markdown("---") | |
| status_box = gr.Textbox( | |
| label="π Live Status & Logs", lines=12, | |
| placeholder="Button dabao - yahan sab dikhega step by step...", | |
| interactive=False | |
| ) | |
| results_table = gr.Dataframe(label="π Orders Results Table", wrap=True, interactive=False) | |
| download_btn = gr.File(label="β¬οΈ CSV Download Karo") | |
| run_btn.click( | |
| fn=run_scraper, | |
| inputs=[page_input, hf_token_input, max_pdf_input, | |
| session_id_input, csrf_token_input], | |
| outputs=[status_box, results_table, download_btn] | |
| ) | |
| gr.Markdown(""" | |
| --- | |
| > π οΈ **Built with:** Python + Gradio + HuggingFace BART | | |
| > π **Data:** Screener.in + BSE India | | |
| > π **Cookies:** HuggingFace Secrets se secure tarike se load hoti hain | |
| """) | |
| app.launch() | |