Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,37 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import
|
3 |
-
|
4 |
-
import numpy as np
|
5 |
-
|
6 |
-
# Load
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|