Krish9497/DirectorOccupationTool
0
1import streamlit as st2import pandas as pd3import requests4import re5import time6import io7import json8 9st.set_page_config(page_title="Testing Environment", layout="wide")10 11# Initialize session storage variables12if "verification_data" not in st.session_state:13 st.session_state.verification_data = []14if "current_index" not in st.session_state:15 st.session_state.current_index = 016 17# --- BACKGROUND COMBINED ALGORITHMS ---18def clean(text):19 if text is None or str(text).lower() == "nan":20 return ""21 return re.sub(r"\s+", " ", str(text)).strip()22 23def google_search(query, api_key):24 url = "https://google.serper.dev/search"25 headers = {"X-API-KEY": api_key, "Content-Type": "application/json"}26 try:27 res = requests.post(url, json={"q": query}, headers=headers, timeout=12)28 return res.json().get("organic", [])29 except:30 return []31 32def extract_identity(results):33 priority_domains = ["linkedin.com", "bloomberg.com", "moneycontrol.com", "business-standard.com", "economictimes.com", "wikipedia.org", "marketscreener.com"]34 priority_keywords = ["founder", "ceo", "managing director", "md", "chairman", "chairperson", "chief executive officer", "executive director", "president", "cio"]35 bad_domains = ["mycorporateinfo", "zaubacorp", "tofler", "quickcompany", "corporatedir"]36 37 scored = []38 for item in results:39 title, snippet, link = clean(item.get("title", "")), clean(item.get("snippet", "")), clean(item.get("link", ""))40 combined = f"{title} | {snippet}"41 lower = combined.lower()42 score = sum(15 for kw in priority_keywords if kw in lower)43 score += sum(25 for dom in priority_domains if dom in link.lower())44 score -= sum(40 for bad in bad_domains if bad in link.lower())45 scored.append({"score": score, "identity": combined, "source": link})46 47 if not scored: return {"score": 0, "identity": "Not Found", "source": ""}48 return sorted(scored, key=lambda x: x["score"], reverse=True)[0]49 50def verify_via_ollama(director, company, snippet, url, model):51 prompt = f"""52 You are an AI validation system. Evaluate if this snippet belongs to the target director at the specified company.53 Director: {director} | Company: {company} | Snippet: {snippet}54 Respond strictly in JSON matching this schema:55 {{"verdict": "MATCHED" or "UNVERIFIED", "confidence_score": 0-100, "curated_title_summary": "Clean short professional title string"}}56 Do not output markdown code blocks or introduction notes. Raw JSON string only.57 """58 try:59 payload = {"model": model, "prompt": prompt, "stream": False, "format": "json"}60 res = requests.post(f"{url}/api/generate", json=payload, timeout=15)61 return json.loads(res.json().get("response", "{}"))62 except Exception as e:63 return {"verdict": "ERROR", "confidence_score": 0, "curated_title_summary": f"Ollama Unreachable: {str(e)}"}64 65# --- APPLICATION INTERFACE ---66st.title("๐งซ Pipeline & Verification Local Sandbox Test")67 68with st.sidebar:69 st.header("๐ Test Parameters")70 serper_key = st.text_input("Serper API Key", type="password")71 ollama_host = st.text_input("Local Ollama Host URL", value="http://localhost:11434")72 ollama_model = st.text_input("Ollama Model Engine", value="llama3")73 test_rows = st.number_input("Limit Testing Rows", min_value=1, max_value=50, value=3)74 75tab1, tab2 = st.tabs(["๐ Phase 1: Run Extraction", "๐ Phase 2: AI Verification Module"])76 77with tab1:78 st.subheader("Test Upload Data Processing")79 uploaded_file = st.file_uploader("Choose mock 'Directors.xlsx' sheet", type=["xlsx"])80 81 if uploaded_file and st.button("โก Start Test Simulation Run"):82 if not serper_key:83 st.error("Please insert a valid testing Serper Key.")84 else:85 df = pd.read_excel(uploaded_file, header=1).head(int(test_rows))86 progress = st.progress(0)87 status = st.empty()88 temp_list = []89 90 for idx, row in df.iterrows():91 din = str(row.get("DIN", ""))92 first = str(row.get("Director First Name", ""))93 middle = "" if str(row.get("Director Middle Name", "")).lower() == "nan" else str(row.get("Director Middle Name", ""))94 surname = str(row.get("Director Surname", ""))95 name = clean(f"{first} {middle} {surname}")96 company_col = "Company Name" if "Company Name" in df.columns else "Company"97 company = str(row.get(company_col, ""))98 99 status.text(f"Extracting row {idx+1}: {name}")100 101 # Extract Web Match Identity102 organic = google_search(f'"{name}" "{din}"', serper_key) + google_search(f'"{name}"', serper_key)103 res = extract_identity(organic)104 105 if res["identity"] == "Not Found" or res["score"] <= 10:106 fallback_query = f'"{first} {surname}" "{company}" (director OR board OR executive)'107 fallback_res = extract_identity(google_search(fallback_query, serper_key))108 if fallback_res["identity"] != "Not Found": res = fallback_res109 110 # Execute Local AI Verification Phase111 status.text(f"Auditing row {idx+1} via local Ollama inference...")112 ai_audit = verify_via_ollama(name, company, res["identity"], ollama_host, ollama_model)113 114 temp_list.append({115 "DIN": din, "Director Name": name, "Company Context": company,116 "Web Identity Output": res["identity"], "Source Document": res["source"],117 "AI Confidence Score": f"{ai_audit.get('confidence_score', 0)}%",118 "Verification Status": "Approved" if ai_audit.get("verdict") == "MATCHED" else "Unverified",119 "Curated Profile Summary": ai_audit.get("curated_title_summary", res["identity"])120 })121 progress.progress((idx + 1) / len(df))122 123 st.session_state.verification_data = temp_list124 status.success("Simulation Complete! Move to Phase 2 Tab above.")125 st.dataframe(pd.DataFrame(temp_list))126 127with tab2:128 if not st.session_state.verification_data:129 st.info("Seed mock records by running a short collection loop inside Tab 1.")130 else:131 v_data = st.session_state.verification_data132 st.session_state.current_index = st.selectbox("Select Record Row:", range(len(v_data)), format_func=lambda i: f"Row {i+1}: {v_data[i]['Director Name']}")133 134 item = v_data[st.session_state.current_index]135 c1, c2 = st.columns(2)136 with c1:137 st.markdown("#### Input Validation Metrics")138 st.text_input("Name", item["Director Name"], disabled=True)139 st.text_input("Target Entity Reference", item["Company Context"], disabled=True)140 st.info(f"**Web Context Snippet:**\n\n{item['Web Identity Output']}")141 with c2:142 st.markdown("#### Dynamic Ollama Transformation")143 edited = st.text_area("Live AI Curated Output String:", value=item["Curated Profile Summary"], height=140)144 v_data[st.session_state.current_index]["Curated Profile Summary"] = edited145 146 b1, b2 = st.columns(2)147 if b1.button("โ
Approve"):148 v_data[st.session_state.current_index]["Verification Status"] = "Approved"149 st.toast("Record Checked!")150 if b2.button("โ Flag Mismatch"):151 v_data[st.session_state.current_index]["Verification Status"] = "Rejected"152 st.toast("Flagged")