THP2903 commited on
Commit
22dfecc
·
verified ·
1 Parent(s): 9aac2b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -66
app.py CHANGED
@@ -1,13 +1,3 @@
1
- # # import gradio as gr
2
- # # import torch as pt
3
- # # import torchaudio
4
- # # import cv2
5
- # # import os
6
- # # import numpy as np
7
- # # import tensorflow as tf
8
- # # from tensorflow.keras.models import load_model
9
- # # from moviepy.editor import VideoFileClip
10
-
11
  import gradio as gr
12
  import torch as pt
13
  import torchaudio
@@ -17,8 +7,6 @@ import numpy as np
17
  import tensorflow as tf
18
  from tensorflow.keras.models import load_model
19
  from moviepy.editor import VideoFileClip
20
- from flask import Flask
21
- from flask_socketio import SocketIO, emit
22
 
23
  def convert_video_to_audio_moviepy(video_file, output_ext="wav"):
24
  """Converts video to audio using MoviePy library that uses `ffmpeg` under the hood"""
@@ -92,68 +80,25 @@ def predict_emotion(video_path):
92
  predicted_label = np.argmax(predictions)
93
  return last_frame, audio_path, predicted_label
94
 
95
- # def predict_emotion_gradio(video_path):
96
- # emotion_dict = {0: 'neutral', 1: 'calm', 2: 'happy', 3: 'sad', 4: 'angry', 5: 'fearful'}
97
- # last_frame, audio_path, predicted_label = predict_emotion(video_path)
98
- # predicted_emotion = emotion_dict[predicted_label]
99
- # return last_frame, audio_path, predicted_emotion
100
-
101
- # iface = gr.Interface(
102
- # fn=predict_emotion_gradio,
103
- # inputs=[
104
- # gr.Video(label="Upload a video")
105
- # ],
106
- # outputs=[
107
- # gr.Image(label="Last Frame"),
108
- # gr.Audio(label = "Audio"),
109
- # gr.Textbox(label="Predicted Emotion")
110
- # ],
111
- # title="Emotion Recognition from Video",
112
- # description="Upload a video and get the predicted emotion."
113
- # )
114
-
115
- # iface.launch()
116
-
117
-
118
- # Integrate chat functionality with emotion prediction
119
- def predict_emotion_with_chat(video_path):
120
  emotion_dict = {0: 'neutral', 1: 'calm', 2: 'happy', 3: 'sad', 4: 'angry', 5: 'fearful'}
121
  last_frame, audio_path, predicted_label = predict_emotion(video_path)
122
  predicted_emotion = emotion_dict[predicted_label]
 
123
 
124
- # Connect to the chat server
125
- socketio.emit('message', {'client': 'Emotion Recognition', 'message': f'Predicted emotion: {predicted_emotion}'})
126
-
127
- # Get messages from the chat server
128
- messages = [] # This should be updated with real messages from the server
129
-
130
- return last_frame, audio_path, predicted_emotion, messages
131
-
132
- # Gradio Interface
133
  iface = gr.Interface(
134
- fn=predict_emotion_with_chat,
135
- inputs=[gr.Video(label="Upload a video")],
 
 
136
  outputs=[
137
  gr.Image(label="Last Frame"),
138
- gr.Audio(label="Audio"),
139
- gr.Textbox(label="Predicted Emotion"),
140
- gr.Chatbot(label="Chat")
141
  ],
142
- title="Emotion Recognition with Chat",
143
- description="Upload a video and get the predicted emotion. Chat with others in real-time."
144
  )
145
 
146
- # Flask app setup
147
- app = Flask(__name__)
148
- app.config['SECRET_KEY'] = 'secret'
149
- socketio = SocketIO(app)
150
-
151
- # Run the chat server
152
- @socketio.on('message')
153
- def handle_message(message):
154
- emit('message', message, broadcast=True)
155
 
156
- if __name__ == '__main__':
157
- iface.launch(share=True)
158
- socketio.run(app, debug=True)
159
-
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch as pt
3
  import torchaudio
 
7
  import tensorflow as tf
8
  from tensorflow.keras.models import load_model
9
  from moviepy.editor import VideoFileClip
 
 
10
 
11
  def convert_video_to_audio_moviepy(video_file, output_ext="wav"):
12
  """Converts video to audio using MoviePy library that uses `ffmpeg` under the hood"""
 
80
  predicted_label = np.argmax(predictions)
81
  return last_frame, audio_path, predicted_label
82
 
83
+ def predict_emotion_gradio(video_path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  emotion_dict = {0: 'neutral', 1: 'calm', 2: 'happy', 3: 'sad', 4: 'angry', 5: 'fearful'}
85
  last_frame, audio_path, predicted_label = predict_emotion(video_path)
86
  predicted_emotion = emotion_dict[predicted_label]
87
+ return last_frame, audio_path, predicted_emotion
88
 
 
 
 
 
 
 
 
 
 
89
  iface = gr.Interface(
90
+ fn=predict_emotion_gradio,
91
+ inputs=[
92
+ gr.Video(label="Upload a video")
93
+ ],
94
  outputs=[
95
  gr.Image(label="Last Frame"),
96
+ gr.Audio(label = "Audio"),
97
+ gr.Textbox(label="Predicted Emotion")
 
98
  ],
99
+ title="Emotion Recognition from Video",
100
+ description="Upload a video and get the predicted emotion."
101
  )
102
 
103
+ iface.launch()
 
 
 
 
 
 
 
 
104