safwansajad commited on
Commit
4bbe9fc
·
verified ·
1 Parent(s): ae2023a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -1,25 +1,29 @@
1
- from llama_cpp import Llama
2
  import gradio as gr
3
 
4
- # Load GGUF model
5
- llm = Llama(
6
- model_path="Mental-Health-Chatbot.i1-IQ1_M.gguf", # Make sure this filename matches exactly
7
- n_ctx=2048,
8
- n_threads=4,
9
- )
10
 
11
- # Chat logic
12
  def chat(message, history):
 
13
  full_prompt = ""
14
  for user, bot in history:
15
  full_prompt += f"User: {user}\nBot: {bot}\n"
16
  full_prompt += f"User: {message}\nBot:"
17
 
18
- output = llm(full_prompt, max_tokens=128, stop=["User:", "\n"], echo=False)
19
- reply = output["choices"][0]["text"].strip()
 
 
 
 
 
 
20
  return reply
21
 
22
- # Chat Interface
23
- gr.ChatInterface(fn=chat, title="Mental Health Llama Chatbot").launch(
24
- server_name="0.0.0.0", server_port=7860
25
- )
 
1
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
2
  import gradio as gr
3
 
4
+ # Load the tokenizer and model from Hugging Face
5
+ tokenizer = GPT2Tokenizer.from_pretrained("distilgpt2")
6
+ model = GPT2LMHeadModel.from_pretrained("distilgpt2")
7
+
8
+ # Ensure the model doesn't generate any special tokens like <pad>
9
+ tokenizer.pad_token = tokenizer.eos_token
10
 
 
11
  def chat(message, history):
12
+ # Prepare the conversation history
13
  full_prompt = ""
14
  for user, bot in history:
15
  full_prompt += f"User: {user}\nBot: {bot}\n"
16
  full_prompt += f"User: {message}\nBot:"
17
 
18
+ # Tokenize the input and generate a response
19
+ inputs = tokenizer(full_prompt, return_tensors="pt")
20
+ outputs = model.generate(inputs["input_ids"], max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)
21
+ reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
22
+
23
+ # Extract only the new reply
24
+ reply = reply.split("Bot:")[-1].strip()
25
+
26
  return reply
27
 
28
+ # Set up the Gradio interface
29
+ gr.ChatInterface(fn=chat, title="Simple Chatbot with DistilGPT-2").launch()