Poojasreeh commited on
Commit
40f8e19
·
verified ·
1 Parent(s): 0747b63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
 
4
  model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
5
 
@@ -7,29 +7,30 @@ print("Loading tokenizer...")
7
  tokenizer = AutoTokenizer.from_pretrained(model_id)
8
 
9
  print("Loading model on CPU...")
10
- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto")
11
- model.to("cpu")
12
 
13
  pipe = pipeline(
14
  "text-generation",
15
  model=model,
16
  tokenizer=tokenizer,
17
- max_new_tokens=256,
 
18
  temperature=0.7,
19
  top_p=0.9,
20
- repetition_penalty=1.05
21
  )
22
 
23
  def chat_fn(message, history):
24
  prompt = f"User: {message}\nAssistant:"
25
  output = pipe(prompt, num_return_sequences=1)
26
- reply = output[0]['generated_text'].split("Assistant:")[-1].strip()
27
- return reply
 
28
 
29
  with gr.Blocks() as demo:
30
  gr.Markdown("<h1 style='text-align:center;'>🧠 Mental Health Companion Bot</h1>")
31
  chatbot = gr.Chatbot()
32
- msg = gr.Textbox(placeholder="Type your message...")
33
  clear = gr.Button("Clear Chat")
34
 
35
  def respond(message, chat_history):
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
  model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
5
 
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_id)
8
 
9
  print("Loading model on CPU...")
10
+ model = AutoModelForCausalLM.from_pretrained(model_id)
11
+ model = model.to("cpu")
12
 
13
  pipe = pipeline(
14
  "text-generation",
15
  model=model,
16
  tokenizer=tokenizer,
17
+ device=-1, # ensures CPU mode
18
+ max_new_tokens=200,
19
  temperature=0.7,
20
  top_p=0.9,
 
21
  )
22
 
23
  def chat_fn(message, history):
24
  prompt = f"User: {message}\nAssistant:"
25
  output = pipe(prompt, num_return_sequences=1)
26
+ text = output[0]["generated_text"]
27
+ response = text.split("Assistant:")[-1].strip()
28
+ return response
29
 
30
  with gr.Blocks() as demo:
31
  gr.Markdown("<h1 style='text-align:center;'>🧠 Mental Health Companion Bot</h1>")
32
  chatbot = gr.Chatbot()
33
+ msg = gr.Textbox(placeholder="Type your message here...")
34
  clear = gr.Button("Clear Chat")
35
 
36
  def respond(message, chat_history):