Initial MVP Commit: Ingest, Enrich, Dashboard

This commit is contained in:
Jarvis
2026-01-30 11:00:44 +00:00
commit f5c91359cf
7 changed files with 252 additions and 0 deletions

9
Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install streamlit pandas
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

Binary file not shown.

55
app.py Normal file
View File

@@ -0,0 +1,55 @@
import streamlit as st
import pandas as pd
from db import get_leads, init_db
import json
st.set_page_config(page_title="TradingTwins Lead Engine", layout="wide")
st.title("🚀 Lead Engine: TradingTwins")
# Metrics
leads = get_leads()
df = pd.DataFrame(leads)
if not df.empty:
col1, col2, col3 = st.columns(3)
col1.metric("Total Leads", len(df))
col2.metric("New Today", len(df[df['status'] == 'new']))
col3.metric("Action Required", len(df[df['status'] == 'enriched']))
st.subheader("Latest Inquiries")
for index, row in df.iterrows():
with st.expander(f"{row['company_name']} - {row['status']}"):
c1, c2 = st.columns(2)
c1.write(f"**Contact:** {row['contact_name']}")
c1.write(f"**Email:** {row['email']}")
enrichment = json.loads(row['enrichment_data']) if row['enrichment_data'] else {}
if enrichment:
c2.success(f"Score: {enrichment.get('score')}")
c2.write(f"**Vertical:** {enrichment.get('vertical')}")
c2.info(f"💡 {enrichment.get('recommendation')}")
if st.button(f"Generate Response for {row['id']}"):
st.write("Generating draft... (Simulated)")
# Here we would call the LLM
draft = f"Hallo {row['contact_name']},\n\ndanke für Ihre Anfrage..."
st.text_area("Draft", draft, height=200)
else:
st.info("No leads found. Waiting for ingest...")
if st.sidebar.button("Run Ingest (Mock)"):
from ingest import ingest_mock_leads
init_db()
count = ingest_mock_leads()
st.sidebar.success(f"Ingested {count} leads.")
st.rerun()
if st.sidebar.button("Run Enrichment"):
from enrich import run_enrichment
run_enrichment()
st.sidebar.success("Enrichment complete.")
st.rerun()

BIN
data/leads.db Normal file

Binary file not shown.

70
db.py Normal file
View File

@@ -0,0 +1,70 @@
import sqlite3
import json
from datetime import datetime
DB_PATH = 'lead-engine/data/leads.db'
def init_db():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS leads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source_id TEXT UNIQUE,
received_at TIMESTAMP,
company_name TEXT,
contact_name TEXT,
email TEXT,
phone TEXT,
raw_body TEXT,
enrichment_data TEXT,
status TEXT DEFAULT 'new',
response_draft TEXT,
sent_at TIMESTAMP
)
''')
conn.commit()
conn.close()
def insert_lead(lead_data):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
try:
c.execute('''
INSERT INTO leads (source_id, received_at, company_name, contact_name, email, phone, raw_body, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (
lead_data.get('id'),
datetime.now(),
lead_data.get('company'),
lead_data.get('contact'),
lead_data.get('email'),
lead_data.get('phone'),
lead_data.get('raw_body'),
'new'
))
conn.commit()
return True
except sqlite3.IntegrityError:
return False
finally:
conn.close()
def get_leads():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM leads ORDER BY received_at DESC')
rows = c.fetchall()
conn.close()
return [dict(row) for row in rows]
def update_lead_status(lead_id, status, response_draft=None):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
if response_draft:
c.execute('UPDATE leads SET status = ?, response_draft = ? WHERE id = ?', (status, response_draft, lead_id))
else:
c.execute('UPDATE leads SET status = ? WHERE id = ?', (status, lead_id))
conn.commit()
conn.close()

43
enrich.py Normal file
View File

@@ -0,0 +1,43 @@
import json
from db import update_lead_status, get_leads
import sqlite3
def enrich_lead(lead):
# Mock Enrichment Logic
# In production, this would call the Company Explorer API
enrichment = {
'vertical': 'Unknown',
'score': 0,
'recommendation': 'Manual Review'
}
company = lead['company_name'].lower()
raw = lead['raw_body']
if 'küche' in company or 'einbau' in company:
enrichment['vertical'] = 'Manufacturing / Woodworking'
enrichment['score'] = 85
enrichment['recommendation'] = 'High Priority - Pitch Dust Control'
if 'shop' in company or 'roller' in company:
enrichment['vertical'] = 'Retail / Automotive'
enrichment['score'] = 60
enrichment['recommendation'] = 'Medium Priority - Pitch Showroom Cleanliness'
# Update DB
conn = sqlite3.connect('lead-engine/data/leads.db')
c = conn.cursor()
c.execute('UPDATE leads SET enrichment_data = ?, status = ? WHERE id = ?',
(json.dumps(enrichment), 'enriched', lead['id']))
conn.commit()
conn.close()
def run_enrichment():
leads = get_leads()
for lead in leads:
if lead['status'] == 'new':
enrich_lead(lead)
if __name__ == "__main__":
run_enrichment()

75
ingest.py Normal file
View File

@@ -0,0 +1,75 @@
import re
from db import insert_lead
def parse_tradingtwins_email(body):
data = {}
# Simple regex extraction based on the email format
patterns = {
'id': r'Lead-ID:\s*(\d+)',
'company': r'Firma:\s*(.+)',
'contact_first': r'Vorname:\s*(.+)',
'contact_last': r'Nachname:\s*(.+)',
'email': r'E-Mail:\s*([^\s<]+)',
'phone': r'Rufnummer:\s*([^\n]+)',
'area': r'Reinigungs-Flche:\s*([^\n]+)',
'purpose': r'Einsatzzweck:\s*([^\n]+)'
}
for key, pattern in patterns.items():
match = re.search(pattern, body)
if match:
data[key] = match.group(1).strip()
# Combine names
if 'contact_first' in data and 'contact_last' in data:
data['contact'] = f"{data['contact_first']} {data['contact_last']}"
data['raw_body'] = body
return data
def ingest_mock_leads():
# Mock data from the session context
leads = [
{
'id': '2397256',
'company': 'pronorm Einbauküchen GmbH',
'contact': 'Jakob Funk',
'email': 'jakob.funk@pronorm.de',
'phone': '+49 5733 979175',
'raw_body': """
Lead-ID: 2397256
Firma: pronorm Einbauküchen GmbH
Vorname: Jakob
Nachname: Funk
Reinigungs-Flche: 1.001 - 10.000 qm
Einsatzzweck: Reinigung von Böden
"""
},
{
'id': '2414364',
'company': 'Quad & Rollershop Schwabmünchen GmbH',
'contact': 'Manfred Bihler',
'email': 'Rollershop.Schwabmuenchen@web.de',
'phone': '+49 8232 905246',
'raw_body': """
Lead-ID: 2414364
Firma: Quad & Rollershop Schwabmünchen GmbH
Vorname: Manfred
Nachname: Bihler
Reinigungs-Flche: 301 - 1.000 qm
Einsatzzweck: Reinigung von Böden
"""
}
]
count = 0
for lead in leads:
if insert_lead(lead):
count += 1
return count
if __name__ == "__main__":
from db import init_db
init_db()
print(f"Ingested {ingest_mock_leads()} new leads.")