ElPierrito commited on
Commit
9ad8f9f
·
verified ·
1 Parent(s): 02bf68d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import gradio as gr
4
+ from sklearn.model_selection import train_test_split
5
+ from sklearn.pipeline import Pipeline
6
+ from sklearn.preprocessing import OneHotEncoder
7
+ from sklearn.feature_extraction.text import TfidfVectorizer
8
+ from sklearn.compose import ColumnTransformer
9
+ from sklearn.linear_model import LogisticRegression
10
+
11
+ # 1. Modelltraining auf lokalem CSV
12
+ df = pd.read_csv("mersen_angebotsanfragen_1000_mit_text.csv")
13
+
14
+ # Vorverarbeitung
15
+ df["Anfrage_Datum"] = pd.to_datetime(df["Anfrage_Datum"])
16
+ df["Wochentag"] = df["Anfrage_Datum"].dt.day_name()
17
+
18
+ X = df[["Kundentyp", "Branche", "Produktgruppe", "Region", "Kanal",
19
+ "Dringlichkeit", "Wochentag", "Anfrage_Text", "Projektgröße (€)"]]
20
+ y = df["Abschluss"]
21
+
22
+ categorical_features = ["Kundentyp", "Branche", "Produktgruppe", "Region",
23
+ "Kanal", "Dringlichkeit", "Wochentag"]
24
+ text_feature = "Anfrage_Text"
25
+ numeric_feature = "Projektgröße (€)"
26
+
27
+ preprocessor = ColumnTransformer([
28
+ ("cat", OneHotEncoder(handle_unknown="ignore"), categorical_features),
29
+ ("text", TfidfVectorizer(), text_feature),
30
+ ("num", "passthrough", [numeric_feature])
31
+ ])
32
+
33
+ pipeline = Pipeline([
34
+ ("preprocessor", preprocessor),
35
+ ("classifier", LogisticRegression(max_iter=1000))
36
+ ])
37
+
38
+ pipeline.fit(X, y)
39
+
40
+ # 2. Vorhersagefunktion für Gradio
41
+ def predict_lead(kundentyp, branche, produktgruppe, region, kanal,
42
+ dringlichkeit, wochentag, anfrage_text, projektgroesse):
43
+
44
+ input_data = pd.DataFrame([{
45
+ "Kundentyp": kundentyp,
46
+ "Branche": branche,
47
+ "Produktgruppe": produktgruppe,
48
+ "Region": region,
49
+ "Kanal": kanal,
50
+ "Dringlichkeit": dringlichkeit,
51
+ "Wochentag": wochentag,
52
+ "Anfrage_Text": anfrage_text,
53
+ "Projektgröße (€)": float(projektgroesse)
54
+ }])
55
+
56
+ prob = pipeline.predict_proba(input_data)[0][1]
57
+ klasse = "hoch" if prob >= 0.75 else "mittel" if prob >= 0.4 else "niedrig"
58
+
59
+ return f"Abschlusswahrscheinlichkeit: {prob:.2f} → Priorität: {klasse.upper()}"
60
+
61
+ # 3. Gradio UI
62
+ demo = gr.Interface(
63
+ fn=predict_lead,
64
+ inputs=[
65
+ gr.Dropdown(["Neukunde", "Bestandskunde", "OEM"], label="Kundentyp"),
66
+ gr.Dropdown(["Gebäude", "Infrastruktur"], label="Branche"),
67
+ gr.Dropdown(["Sicherung", "Graphitmodul", "Isolationsmaterial", "Spezialfertigung"], label="Produktgruppe"),
68
+ gr.Dropdown(["DACH"], label="Region"),
69
+ gr.Dropdown(["Webformular", "E-Mail", "Vertriebspartner"], label="Kanal"),
70
+ gr.Dropdown(["sofort", "Q1 2025", "Q2 2025", "nächstes Jahr", "unklar"], label="Dringlichkeit"),
71
+ gr.Dropdown(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], label="Wochentag"),
72
+ gr.Textbox(label="Anfrage-Text"),
73
+ gr.Number(label="Projektgröße (€)")
74
+ ],
75
+ outputs="text",
76
+ title="Lead-Priorisierung – Angebotsanfragen bei Mersen",
77
+ description="Gib die Eckdaten einer Angebotsanfrage ein. Das Modell bewertet die Abschlusswahrscheinlichkeit und Priorität."
78
+ )
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch()