SatyamSinghal commited on
Commit
3ef1aec
·
verified ·
1 Parent(s): f2203ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -1,24 +1,34 @@
 
1
  import gradio as gr
2
- from transformers import pipeline
3
  import torch
 
 
4
 
5
  MODEL_ID = "SatyamSinghal/taskmind-1.1b-chat-lora"
6
 
7
- # Load once when Space starts
8
- pipe = pipeline(
9
- "text-generation",
10
- model=MODEL_ID,
11
- device_map="auto",
 
12
  )
13
 
14
- SYSTEM_PROMPT = (
15
- "You are TaskMind, a helpful assistant for understanding team messages "
16
- "and converting them into structured task information when appropriate."
 
 
17
  )
18
 
19
- def chat_fn(message, history):
20
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
 
 
21
 
 
 
22
  for user_msg, assistant_msg in history:
23
  if user_msg:
24
  messages.append({"role": "user", "content": user_msg})
@@ -27,7 +37,7 @@ def chat_fn(message, history):
27
 
28
  messages.append({"role": "user", "content": message})
29
 
30
- output = pipe(
31
  messages,
32
  max_new_tokens=256,
33
  do_sample=True,
@@ -35,27 +45,23 @@ def chat_fn(message, history):
35
  top_p=0.9,
36
  )
37
 
38
- # Pipeline output for chat models usually returns generated conversation
39
- generated = output[0]["generated_text"]
40
-
41
  if isinstance(generated, list):
42
  return generated[-1]["content"]
43
-
44
  return str(generated)
45
 
46
  demo = gr.ChatInterface(
47
- fn=chat_fn,
48
- title="TaskMind Chat Demo",
49
- description="Try the TaskMind LoRA model hosted on Hugging Face Spaces.",
50
  examples=[
51
  "Who are you?",
52
- "@Agrim fix the growstreams deck ASAP no delay",
53
- "login page 60% ho gaya",
54
  "done bhai, merged the PR",
 
55
  "getting 500 error on registration",
56
  ],
57
- theme="soft",
58
  )
59
 
60
  if __name__ == "__main__":
61
- demo.launch()
 
1
+ import os
2
  import gradio as gr
 
3
  import torch
4
+ from peft import AutoPeftModelForCausalLM
5
+ from transformers import AutoTokenizer, pipeline
6
 
7
  MODEL_ID = "SatyamSinghal/taskmind-1.1b-chat-lora"
8
 
9
+ # Optional: use HF token from Space secrets if you add one
10
+ HF_TOKEN = os.getenv("HF_TOKEN")
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained(
13
+ MODEL_ID,
14
+ token=HF_TOKEN,
15
  )
16
 
17
+ model = AutoPeftModelForCausalLM.from_pretrained(
18
+ MODEL_ID,
19
+ token=HF_TOKEN,
20
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
21
+ low_cpu_mem_usage=True,
22
  )
23
 
24
+ pipe = pipeline(
25
+ "text-generation",
26
+ model=model,
27
+ tokenizer=tokenizer,
28
+ )
29
 
30
+ def respond(message, history):
31
+ messages = []
32
  for user_msg, assistant_msg in history:
33
  if user_msg:
34
  messages.append({"role": "user", "content": user_msg})
 
37
 
38
  messages.append({"role": "user", "content": message})
39
 
40
+ out = pipe(
41
  messages,
42
  max_new_tokens=256,
43
  do_sample=True,
 
45
  top_p=0.9,
46
  )
47
 
48
+ generated = out[0]["generated_text"]
 
 
49
  if isinstance(generated, list):
50
  return generated[-1]["content"]
 
51
  return str(generated)
52
 
53
  demo = gr.ChatInterface(
54
+ fn=respond,
55
+ title="TaskMind Demo",
56
+ description="Try the TaskMind LoRA model.",
57
  examples=[
58
  "Who are you?",
59
+ "@Agrim fix the growstreams deck ASAP NO Delay",
 
60
  "done bhai, merged the PR",
61
+ "login page 60% ho gaya",
62
  "getting 500 error on registration",
63
  ],
 
64
  )
65
 
66
  if __name__ == "__main__":
67
+ demo.launch()