safwansajad commited on
Commit
a0428bd
·
verified ·
1 Parent(s): b915000

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -1,35 +1,31 @@
1
  import gradio as gr
2
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
 
4
- # Load chatbot model and move it to CPU explicitly
5
  chatbot_model = "microsoft/DialoGPT-medium"
6
  tokenizer = AutoTokenizer.from_pretrained(chatbot_model)
7
- model = AutoModelForCausalLM.from_pretrained(chatbot_model).to('cpu') # Ensure model is on CPU
8
 
9
- # Load emotion detection model and move it to CPU explicitly
10
- emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", device=-1) # -1 means CPU
11
 
12
- # Function to generate chatbot response and emotion analysis
13
  def generate_response(user_input):
14
  # Generate chatbot response
15
- input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt").to('cpu') # Ensure input tensor is on CPU
16
  output = model.generate(input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id)
17
  response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
18
 
19
  # Detect emotion
20
  emotion_result = emotion_pipeline(user_input)
21
  emotion = emotion_result[0]["label"]
 
 
22
 
23
- # Return chatbot response and detected emotion
24
- return response, f"Emotion Detected: {emotion}"
25
-
26
- # Gradio interface setup
27
  iface = gr.Interface(
28
  fn=generate_response,
29
- inputs=gr.Textbox(label="You:", placeholder="Enter your message here..."),
30
- outputs=[gr.Textbox(label="Bot Response"), gr.Textbox(label="Emotion Detected")],
31
  live=True
32
  )
33
 
34
- # Launch the app
35
  iface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
 
4
+ # Load chatbot model
5
  chatbot_model = "microsoft/DialoGPT-medium"
6
  tokenizer = AutoTokenizer.from_pretrained(chatbot_model)
7
+ model = AutoModelForCausalLM.from_pretrained(chatbot_model)
8
 
9
+ # Load emotion detection model
10
+ emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
11
 
 
12
  def generate_response(user_input):
13
  # Generate chatbot response
14
+ input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
15
  output = model.generate(input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id)
16
  response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
17
 
18
  # Detect emotion
19
  emotion_result = emotion_pipeline(user_input)
20
  emotion = emotion_result[0]["label"]
21
+
22
+ return response, emotion
23
 
 
 
 
 
24
  iface = gr.Interface(
25
  fn=generate_response,
26
+ inputs=gr.Textbox(label="Enter your message"),
27
+ outputs=[gr.Textbox(label="Chatbot Response"), gr.Textbox(label="Emotion Detected")],
28
  live=True
29
  )
30
 
 
31
  iface.launch()