Garvitj commited on
Commit
ea5f08e
·
verified ·
1 Parent(s): 4496aae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import cv2
4
+ import librosa
5
+ import moviepy.editor as mp
6
+ import speech_recognition as sr
7
+ import tempfile
8
+ import wave
9
+ import os
10
+ import tensorflow as tf
11
+ from tensorflow.keras.preprocessing.text import tokenizer_from_json
12
+ from tensorflow.keras.models import load_model, model_from_json
13
+ from sklearn.preprocessing import StandardScaler
14
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
15
+ import nltk
16
+ from nltk.corpus import stopwords
17
+ from nltk.stem import WordNetLemmatizer
18
+ import pickle
19
+ import json
20
+ from transformers import pipeline
21
+
22
+ nltk.download('punkt') # Tokenizer
23
+ nltk.download('wordnet') # WordNet lemmatizer
24
+ nltk.download('stopwords') # Stopwords
25
+
26
+ # Load text emotion model
27
+ with open('model_architecture_for_text_emotion_updated_json.json', 'r') as json_file:
28
+ model_json = json_file.read()
29
+ text_model = model_from_json(model_json)
30
+ text_model.load_weights("model_for_text_emotion_updated(1).keras")
31
+
32
+ # Load tokenizer
33
+ with open('tokenizer.json') as json_file:
34
+ tokenizer_json = json.load(json_file)
35
+ tokenizer = tokenizer_from_json(tokenizer_json)
36
+
37
+ # Load LLM chatbot (replace with LLama or another LLM of your choice)
38
+ chatbot = pipeline("text-generation", model="facebook/blenderbot-3B")
39
+
40
+ # Initialize NLTK
41
+ lemmatizer = WordNetLemmatizer()
42
+ stop_words = set(stopwords.words('english'))
43
+
44
+ def preprocess_text(text):
45
+ tokens = nltk.word_tokenize(text.lower())
46
+ tokens = [word for word in tokens if word.isalnum() and word not in stop_words]
47
+ lemmatized_tokens = [lemmatizer.lemmatize(word) for word in tokens]
48
+ return ' '.join(lemmatized_tokens)
49
+
50
+ # Transcribe audio and get emotion
51
+
52
+ def transcribe_audio(audio_file):
53
+ recognizer = sr.Recognizer()
54
+ with sr.AudioFile(audio_file) as source:
55
+ audio_record = recognizer.record(source)
56
+ text = recognizer.recognize_google(audio_record)
57
+
58
+ pre_text = preprocess_text(text)
59
+ title_seq = tokenizer.texts_to_sequences([pre_text])
60
+ padded_title_seq = pad_sequences(title_seq, maxlen=35, padding='post', truncating='post')
61
+ inp1 = np.array(padded_title_seq)
62
+ text_prediction = text_model.predict(inp1)
63
+
64
+ mapping = {0: "anger", 1: "disgust", 2: "fear", 3: "joy", 4: "neutral", 5: "sadness", 6: "surprise"}
65
+ max_index = text_prediction.argmax()
66
+ return text, mapping[max_index]
67
+
68
+ # Chatbot response
69
+
70
+ def chatbot_response(audio_file):
71
+ user_input, emotion = transcribe_audio(audio_file)
72
+ response = chatbot(user_input, max_length=100, num_return_sequences=1)[0]['generated_text']
73
+ return f"Detected Emotion: {emotion}\nChatbot: {response}"
74
+
75
+ # Create Gradio Interface
76
+ iface = gr.Interface(
77
+ fn=chatbot_response,
78
+ inputs=gr.Audio(source="microphone", type="filepath"),
79
+ outputs="text",
80
+ title="Emotion-Aware AI Chatbot",
81
+ description="Speak into the microphone, and the chatbot will analyze your emotion and respond accordingly."
82
+ )
83
+
84
+ iface.launch()