safwansajad commited on
Commit
e381c51
·
verified ·
1 Parent(s): c52f1ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -1,22 +1,32 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Load model from Hugging Face Hub
5
- model_name = "thrishala/mental_health_chatbot"
6
- nlp = pipeline("text-generation", model=model_name)
 
7
 
8
- # Function to process user input and return chatbot's response
9
- def chatbot_response(user_input):
10
- # Get response using the model pipeline
11
- response = nlp(user_input, max_length=150, num_return_sequences=1)
12
- return response[0]['generated_text']
13
 
14
- # Gradio interface
15
- iface = gr.Interface(fn=chatbot_response,
16
- inputs="text",
17
- outputs="text",
18
- title="Mental Health Chatbot",
19
- description="This chatbot provides empathetic responses to mental health-related queries. It aims to support users in a safe and compassionate manner.")
 
 
 
 
 
 
 
 
 
20
 
21
- # Launch the app
22
- iface.launch()
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import torch
3
 
4
+ # Load the model and tokenizer
5
+ model_name = "tanusrich/Mental_Health_Chatbot"
6
+ model = AutoModelForCausalLM.from_pretrained(model_name)
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
+ # Move the model to the appropriate device (CPU or GPU)
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ model.to(device)
 
 
12
 
13
+ # Generate a response
14
+ def generate_response(user_input):
15
+ inputs = tokenizer(user_input, return_tensors="pt").to(device)
16
+ with torch.no_grad():
17
+ output = model.generate(
18
+ **inputs,
19
+ max_new_tokens=200,
20
+ temperature=0.7,
21
+ top_k=50,
22
+ top_p=0.9,
23
+ repetition_penalty=1.2,
24
+ pad_token_id=tokenizer.eos_token_id
25
+ )
26
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
27
+ return response
28
 
29
+ # Example interaction
30
+ user_input = "I'm feeling lonely and anxious. What can I do?"
31
+ response = generate_response(user_input)
32
+ print("Chatbot: ", response)