Nick021402 commited on
Commit
66a19be
Β·
verified Β·
1 Parent(s): 9bb4922

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -53
app.py CHANGED
@@ -1,53 +1,37 @@
1
- import gradio as gr
2
- import torchaudio
3
- from transformers import pipeline
4
- import numpy as np
5
-
6
- # Load model for speech emotion recognition
7
- emotion_pipeline = pipeline("audio-classification", model="superb/hubert-large-superb-er")
8
-
9
- # Voice Analysis Logic
10
- def analyze_voice(audio):
11
- if audio is None:
12
- return "Please record or upload your voice."
13
-
14
- # Get emotion
15
- results = emotion_pipeline(audio["array"])
16
- top = max(results, key=lambda x: x["score"])
17
- emotion = top["label"].capitalize()
18
- confidence = round(top["score"] * 100, 2)
19
-
20
- # Personality interpretation (rule-based)
21
- personality_map = {
22
- "Happy": "You sound energetic, warm, and approachable. Possibly a social butterfly or a natural leader.",
23
- "Sad": "You're deep and thoughtful. Often empathetic and emotionally intelligent.",
24
- "Angry": "You radiate power and confidence. A strong-willed individual who’s not afraid to speak out.",
25
- "Neutral": "Balanced and calm. Likely introspective and logical in decision-making.",
26
- "Fearful": "You might be cautious, observant, and sensitive to surroundings.",
27
- "Disgust": "Sharp-witted with a strong sense of right and wrong.",
28
- "Surprise": "Curious and spontaneous. You embrace the unexpected with open arms."
29
- }
30
-
31
- persona = personality_map.get(emotion, "A unique voice with a unique mind. Hard to label, easy to admire.")
32
-
33
- return f"""
34
- ## πŸŽ™οΈ Voice2Persona-AI Report
35
- **Detected Emotion**: {emotion}
36
- **Confidence**: {confidence}%
37
- **Inferred Personality Vibe**:
38
- {persona}
39
- """
40
-
41
- # Gradio UI
42
- demo = gr.Interface(
43
- fn=analyze_voice,
44
- inputs=gr.Audio(source="microphone", type="numpy", label="🎀 Record or Upload Your Voice"),
45
- outputs=gr.Markdown(),
46
- title="πŸ”Š Voice2Persona-AI",
47
- description="Speak for 5–10 seconds and get a fun AI-generated personality profile based on your tone & emotion.",
48
- theme="soft",
49
- live=True
50
- )
51
-
52
- if __name__ == "__main__":
53
- demo.launch()
 
1
+ import gradio as gr
2
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
3
+ import torch
4
+ import numpy as np
5
+
6
+ # Load the pretrained model and processor
7
+ processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
8
+ model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model.to(device)
12
+
13
+ # Transcription function
14
+ def transcribe(audio):
15
+ if audio is None:
16
+ return "Please upload or record an audio file."
17
+
18
+ input_values = processor(audio, sampling_rate=16000, return_tensors="pt").input_values.to(device)
19
+ with torch.no_grad():
20
+ logits = model(input_values).logits
21
+ predicted_ids = torch.argmax(logits, dim=-1)
22
+ transcription = processor.decode(predicted_ids[0])
23
+ return transcription.lower()
24
+
25
+ # Gradio interface
26
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
27
+ gr.Markdown("# Voice2PersonaAI")
28
+ gr.Markdown("Upload or record your voice, and this app will transcribe what you say.")
29
+
30
+ with gr.Row():
31
+ audio_input = gr.Audio(label="🎀 Record or Upload Your Voice", type="numpy")
32
+ output_text = gr.Textbox(label="πŸ“ Transcribed Text")
33
+
34
+ transcribe_button = gr.Button("Transcribe")
35
+ transcribe_button.click(fn=transcribe, inputs=audio_input, outputs=output_text)
36
+
37
+ app.launch()