Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,22 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
-
from transformers import
|
| 6 |
from sklearn.datasets import fetch_california_housing
|
| 7 |
from sklearn.linear_model import LinearRegression, LogisticRegression
|
| 8 |
from sklearn.model_selection import train_test_split
|
| 9 |
from sklearn.metrics import r2_score, accuracy_score
|
| 10 |
|
| 11 |
-
# βββ 1) LOAD
|
| 12 |
-
model_dir = "model" # folder to upload your safetensors+configs into
|
| 13 |
device = 0 if torch.cuda.is_available() else -1
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
model
|
| 17 |
-
|
| 18 |
-
model=model,
|
| 19 |
-
tokenizer=tokenizer,
|
| 20 |
device=device,
|
| 21 |
max_new_tokens=100,
|
| 22 |
do_sample=True,
|
| 23 |
-
temperature=0.7
|
| 24 |
)
|
| 25 |
|
| 26 |
def chat_response(prompt: str) -> str:
|
|
@@ -28,26 +25,26 @@ def chat_response(prompt: str) -> str:
|
|
| 28 |
return out[0]["generated_text"].strip()
|
| 29 |
|
| 30 |
# βββ 2) PREPARE HOUSING ML MODELS βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
-
data
|
| 32 |
-
X, y
|
| 33 |
|
| 34 |
# linear regression
|
| 35 |
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 36 |
-
reg
|
| 37 |
-
r2
|
| 38 |
|
| 39 |
-
# logistic classification
|
| 40 |
-
y_bin
|
| 41 |
Xtr2, Xte2, ytr2, yte2 = train_test_split(X, y_bin, test_size=0.2, random_state=42)
|
| 42 |
-
clf
|
| 43 |
-
acc
|
| 44 |
|
| 45 |
-
# βββ 3) ORCHESTRATOR βββββββββββββββββββββββββββββββββββββββββ
|
| 46 |
def respond(message, chat_history, state):
|
| 47 |
stage = state.get("stage", "chat")
|
| 48 |
ml_type = state.get("ml_type")
|
| 49 |
|
| 50 |
-
#
|
| 51 |
if stage == "clarify":
|
| 52 |
m = message.lower()
|
| 53 |
if "linear" in m:
|
|
@@ -57,40 +54,40 @@ def respond(message, chat_history, state):
|
|
| 57 |
state.update(stage="ml", ml_type="classification")
|
| 58 |
chat_history.append(("Bot","Running logistic classificationβ¦"))
|
| 59 |
else:
|
| 60 |
-
chat_history.append(("Bot","β οΈ
|
| 61 |
return chat_history, state
|
| 62 |
|
| 63 |
# ML execution
|
| 64 |
if state["stage"] == "ml":
|
| 65 |
if state["ml_type"] == "regression":
|
| 66 |
-
chat_history.append(("Bot",f"β
|
| 67 |
else:
|
| 68 |
-
chat_history.append(("Bot",f"β
|
| 69 |
state.update(stage="chat", ml_type=None)
|
| 70 |
return chat_history, state
|
| 71 |
|
| 72 |
-
#
|
| 73 |
if any(k in message.lower() for k in ["predict","regression","classification"]):
|
| 74 |
state["stage"] = "clarify"
|
| 75 |
chat_history.append(("Bot","Sureβlinear regression or classification?"))
|
| 76 |
return chat_history, state
|
| 77 |
|
| 78 |
-
#
|
| 79 |
reply = chat_response(message)
|
| 80 |
chat_history.append(("Bot", reply))
|
| 81 |
return chat_history, state
|
| 82 |
|
| 83 |
# βββ 4) GRADIO UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
-
gr.Markdown("## π€
|
| 86 |
-
chat
|
| 87 |
-
|
| 88 |
-
state
|
| 89 |
-
|
| 90 |
|
| 91 |
-
#
|
| 92 |
demo = demo.queue()
|
| 93 |
|
| 94 |
-
if __name__=="__main__":
|
| 95 |
port = int(os.environ.get("PORT", 7860))
|
| 96 |
demo.launch(server_name="0.0.0.0", server_port=port)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
+
from transformers import pipeline
|
| 6 |
from sklearn.datasets import fetch_california_housing
|
| 7 |
from sklearn.linear_model import LinearRegression, LogisticRegression
|
| 8 |
from sklearn.model_selection import train_test_split
|
| 9 |
from sklearn.metrics import r2_score, accuracy_score
|
| 10 |
|
| 11 |
+
# βββ 1) LOAD A PUBLIC LLM ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 12 |
device = 0 if torch.cuda.is_available() else -1
|
| 13 |
+
generator = pipeline(
|
| 14 |
+
"text-generation",
|
| 15 |
+
model="distilgpt2",
|
| 16 |
+
tokenizer="distilgpt2",
|
|
|
|
|
|
|
| 17 |
device=device,
|
| 18 |
max_new_tokens=100,
|
| 19 |
do_sample=True,
|
| 20 |
+
temperature=0.7,
|
| 21 |
)
|
| 22 |
|
| 23 |
def chat_response(prompt: str) -> str:
|
|
|
|
| 25 |
return out[0]["generated_text"].strip()
|
| 26 |
|
| 27 |
# βββ 2) PREPARE HOUSING ML MODELS βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
+
data = fetch_california_housing()
|
| 29 |
+
X, y = data.data, data.target
|
| 30 |
|
| 31 |
# linear regression
|
| 32 |
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 33 |
+
reg = LinearRegression().fit(Xtr, ytr)
|
| 34 |
+
r2 = r2_score(yte, reg.predict(Xte))
|
| 35 |
|
| 36 |
+
# logistic classification
|
| 37 |
+
y_bin = (y > np.median(y)).astype(int)
|
| 38 |
Xtr2, Xte2, ytr2, yte2 = train_test_split(X, y_bin, test_size=0.2, random_state=42)
|
| 39 |
+
clf = LogisticRegression(max_iter=1000).fit(Xtr2, ytr2)
|
| 40 |
+
acc = accuracy_score(yte2, clf.predict(Xte2))
|
| 41 |
|
| 42 |
+
# βββ 3) ORCHESTRATOR / STATE MACHINE βββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
def respond(message, chat_history, state):
|
| 44 |
stage = state.get("stage", "chat")
|
| 45 |
ml_type = state.get("ml_type")
|
| 46 |
|
| 47 |
+
# Clarify step
|
| 48 |
if stage == "clarify":
|
| 49 |
m = message.lower()
|
| 50 |
if "linear" in m:
|
|
|
|
| 54 |
state.update(stage="ml", ml_type="classification")
|
| 55 |
chat_history.append(("Bot","Running logistic classificationβ¦"))
|
| 56 |
else:
|
| 57 |
+
chat_history.append(("Bot","β οΈ Say βlinear regressionβ or βclassificationβ."))
|
| 58 |
return chat_history, state
|
| 59 |
|
| 60 |
# ML execution
|
| 61 |
if state["stage"] == "ml":
|
| 62 |
if state["ml_type"] == "regression":
|
| 63 |
+
chat_history.append(("Bot",f"β
RΒ²={r2:.3f}\nCoefs={np.round(reg.coef_,3).tolist()}"))
|
| 64 |
else:
|
| 65 |
+
chat_history.append(("Bot",f"β
Accuracy={acc:.3f}"))
|
| 66 |
state.update(stage="chat", ml_type=None)
|
| 67 |
return chat_history, state
|
| 68 |
|
| 69 |
+
# Detect intent
|
| 70 |
if any(k in message.lower() for k in ["predict","regression","classification"]):
|
| 71 |
state["stage"] = "clarify"
|
| 72 |
chat_history.append(("Bot","Sureβlinear regression or classification?"))
|
| 73 |
return chat_history, state
|
| 74 |
|
| 75 |
+
# Fallback to LLM
|
| 76 |
reply = chat_response(message)
|
| 77 |
chat_history.append(("Bot", reply))
|
| 78 |
return chat_history, state
|
| 79 |
|
| 80 |
# βββ 4) GRADIO UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("## π€ LLM + HousingβML Chatbot")
|
| 83 |
+
chat = gr.Chatbot()
|
| 84 |
+
user_in = gr.Textbox(placeholder="Type hereβ¦", show_label=False)
|
| 85 |
+
state = gr.State({"stage":"chat","ml_type":None})
|
| 86 |
+
user_in.submit(respond, [user_in, chat, state], [chat, state])
|
| 87 |
|
| 88 |
+
# queue() creates the /api/predict endpoint that Spaces needs
|
| 89 |
demo = demo.queue()
|
| 90 |
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
port = int(os.environ.get("PORT", 7860))
|
| 93 |
demo.launch(server_name="0.0.0.0", server_port=port)
|