Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
import librosa
|
7 |
+
|
8 |
+
# Load T5 model for simplification
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("t5-base")
|
10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
|
11 |
+
|
12 |
+
# Dummy function for stress detection from voice (replace with your actual model)
|
13 |
+
def detect_stress_from_voice(audio_path):
|
14 |
+
# For demo, let's randomly return 'low' or 'high' stress
|
15 |
+
# You will replace this with real stress detection logic
|
16 |
+
return "high"
|
17 |
+
|
18 |
+
# Dummy function for stress detection from face image (replace with your actual model)
|
19 |
+
def detect_stress_from_face(image):
|
20 |
+
# For demo, randomly return 'low' or 'high' stress
|
21 |
+
return "high"
|
22 |
+
|
23 |
+
def simplify_task(task, stress_level):
|
24 |
+
if stress_level == "low":
|
25 |
+
return task # No simplification needed if stress is low
|
26 |
+
|
27 |
+
input_text = "simplify: " + task
|
28 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
29 |
+
outputs = model.generate(inputs, max_length=60, num_beams=4, early_stopping=True)
|
30 |
+
simplified_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
+
return simplified_text
|
32 |
+
|
33 |
+
def assistant(voice, face_image, task):
|
34 |
+
# Step 1: Detect stress from voice and face image
|
35 |
+
voice_stress = detect_stress_from_voice(voice.name)
|
36 |
+
face_stress = detect_stress_from_face(face_image)
|
37 |
+
|
38 |
+
# Combine stress signals (simple majority vote)
|
39 |
+
stress_level = "high" if (voice_stress == "high" or face_stress == "high") else "low"
|
40 |
+
|
41 |
+
# Step 2: Simplify the task based on stress level
|
42 |
+
simplified = simplify_task(task, stress_level)
|
43 |
+
|
44 |
+
# Return stress level and simplified task
|
45 |
+
return f"Detected Stress Level: {stress_level.capitalize()}", simplified
|
46 |
+
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
gr.Markdown("# Context-Aware Multimodal Assistant")
|
49 |
+
gr.Markdown("Upload your voice recording and face image, then type your task below.")
|
50 |
+
|
51 |
+
voice_input = gr.Audio(label="Upload your voice recording (.wav)", type="filepath")
|
52 |
+
face_input = gr.Image(label="Upload your face image")
|
53 |
+
task_input = gr.Textbox(label="📝 What are you trying to do or say?", placeholder="E.g. I need help writing a message to my manager.")
|
54 |
+
|
55 |
+
output_stress = gr.Textbox(label="🧠 Stress Level Detected", interactive=False)
|
56 |
+
output_simplified = gr.Textbox(label="💬 Simplified Task / Message", interactive=False)
|
57 |
+
|
58 |
+
submit_btn = gr.Button("Simplify Task")
|
59 |
+
|
60 |
+
submit_btn.click(
|
61 |
+
fn=assistant,
|
62 |
+
inputs=[voice_input, face_input, task_input],
|
63 |
+
outputs=[output_stress, output_simplified]
|
64 |
+
)
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
demo.launch()
|