DronA23 commited on
Commit
ce0d582
Β·
verified Β·
1 Parent(s): 1ef636f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -32
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 AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
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 LOCAL LLM ─────────────────────────────────────────────────────────
12
- model_dir = "model" # folder to upload your safetensors+configs into
13
  device = 0 if torch.cuda.is_available() else -1
14
-
15
- tokenizer = AutoTokenizer.from_pretrained(model_dir, local_files_only=True)
16
- model = AutoModelForCausalLM.from_pretrained(model_dir, local_files_only=True)
17
- generator = TextGenerationPipeline(
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 = fetch_california_housing()
32
- X, y = data.data, data.target
33
 
34
  # linear regression
35
  Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)
36
- reg = LinearRegression().fit(Xtr, ytr)
37
- r2 = r2_score(yte, reg.predict(Xte))
38
 
39
- # logistic classification (above/below median)
40
- y_bin = (y > np.median(y)).astype(int)
41
  Xtr2, Xte2, ytr2, yte2 = train_test_split(X, y_bin, test_size=0.2, random_state=42)
42
- clf = LogisticRegression(max_iter=1000).fit(Xtr2, ytr2)
43
- acc = accuracy_score(yte2, clf.predict(Xte2))
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
- # clarify step
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","⚠️ Please say β€˜linear regression’ or β€˜classification’"))
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"βœ… Regression RΒ²={r2:.3f}\nCoefs={np.round(reg.coef_,3).tolist()}"))
67
  else:
68
- chat_history.append(("Bot",f"βœ… Classification accuracy={acc:.3f}"))
69
  state.update(stage="chat", ml_type=None)
70
  return chat_history, state
71
 
72
- # detect ML intent
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
- # fallback to LLM
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("## πŸ€– Local‐LLM + Housing‐ML Chatbot")
86
- chat = gr.Chatbot()
87
- txt = gr.Textbox(placeholder="Type here…", show_label=False)
88
- state = gr.State({"stage":"chat","ml_type":None})
89
- txt.submit(respond, [txt, chat, state], [chat, state])
90
 
91
- # this .queue() creates /api/predict so Spaces detects your app
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)