Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit.components.v1 import html
|
3 |
import requests
|
4 |
import os
|
5 |
import base64
|
6 |
import tempfile
|
7 |
import io
|
8 |
-
|
9 |
-
import
|
|
|
|
|
10 |
from PIL import Image
|
11 |
|
12 |
# Hugging Face API Keys
|
@@ -34,20 +35,6 @@ avatars = {
|
|
34 |
"pig": {"desc": "Warm and cheerful. Creates a positive learning atmosphere.", "voice": "Learning is fun! Let's enjoy this together!"},
|
35 |
}
|
36 |
|
37 |
-
# Load the Swiper component
|
38 |
-
swiper_code = """
|
39 |
-
<script type="module">
|
40 |
-
import React from "react";
|
41 |
-
import ReactDOM from "react-dom";
|
42 |
-
import SwiperComponent from "./Swipercomponent.js";
|
43 |
-
function App() {
|
44 |
-
return <SwiperComponent />;
|
45 |
-
}
|
46 |
-
ReactDOM.render(<App />, document.getElementById("root"));
|
47 |
-
</script>
|
48 |
-
"""
|
49 |
-
st.components.v1.html(swiper_code, height=400)
|
50 |
-
|
51 |
# Function to process speech-to-text
|
52 |
def speech_to_text(audio_bytes):
|
53 |
files = {"file": ("audio.wav", audio_bytes, "audio/wav")}
|
@@ -67,28 +54,49 @@ def text_to_speech(text):
|
|
67 |
payload = {"inputs": text}
|
68 |
response = requests.post(HF_TEXT_TO_SPEECH_API, headers=HEADERS, json=payload)
|
69 |
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
# Streamlit UI
|
71 |
st.title("ποΈ AI Speaking Pal")
|
72 |
st.write("Press the button and chat with your pal!")
|
73 |
|
74 |
-
#
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
80 |
|
81 |
-
|
|
|
|
|
82 |
user_text = speech_to_text(audio_bytes)
|
83 |
st.write(f"**You:** {user_text}")
|
84 |
|
85 |
-
# Get AI response
|
86 |
ai_reply = chatbot_response(user_text)
|
87 |
st.write(f"**AI:** {ai_reply}")
|
88 |
|
89 |
-
# Convert AI response to speech
|
90 |
speech_audio = text_to_speech(ai_reply)
|
91 |
-
st.audio(speech_audio, format="audio/wav")
|
92 |
|
93 |
# Swiper Carousel Component
|
94 |
avatar_list = list(avatars.keys())
|
@@ -105,31 +113,4 @@ if st.button("Hear Sample"):
|
|
105 |
audio_bytes = text_to_speech(avatar_info["voice"])
|
106 |
st.audio(io.BytesIO(audio_bytes), format="audio/wav")
|
107 |
|
108 |
-
# Start/Stop Conversation Button
|
109 |
-
if "conversation_active" not in st.session_state:
|
110 |
-
st.session_state.conversation_active = False
|
111 |
-
|
112 |
-
def toggle_conversation():
|
113 |
-
st.session_state.conversation_active = not st.session_state.conversation_active
|
114 |
-
|
115 |
-
st.button("π€ Start/Stop Conversation", on_click=toggle_conversation)
|
116 |
-
|
117 |
-
# Conversation Text Box
|
118 |
-
show_text = st.checkbox("Show conversation text")
|
119 |
-
if show_text:
|
120 |
-
conversation_box = st.text_area("Conversation:", height=200)
|
121 |
-
|
122 |
-
# Handle microphone input
|
123 |
-
if st.session_state.conversation_active:
|
124 |
-
audio = audiorecorder("Click to Speak", "Stop recording")
|
125 |
-
if len(audio) > 0:
|
126 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
127 |
-
tmpfile.write(audio)
|
128 |
-
user_text = speech_to_text(tmpfile.name)
|
129 |
-
st.write(f"**You:** {user_text}")
|
130 |
-
ai_reply = chatbot_response(user_text)
|
131 |
-
st.write(f"**{selected_avatar.capitalize()}:** {ai_reply}")
|
132 |
-
speech_audio = text_to_speech(ai_reply)
|
133 |
-
st.audio(io.BytesIO(speech_audio), format="audio/wav")
|
134 |
-
|
135 |
st.write("Have fun learning English with your AI pal!")
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import requests
|
3 |
import os
|
4 |
import base64
|
5 |
import tempfile
|
6 |
import io
|
7 |
+
import numpy as np
|
8 |
+
import sounddevice as sd
|
9 |
+
import librosa
|
10 |
+
from scipy.io.wavfile import write
|
11 |
from PIL import Image
|
12 |
|
13 |
# Hugging Face API Keys
|
|
|
35 |
"pig": {"desc": "Warm and cheerful. Creates a positive learning atmosphere.", "voice": "Learning is fun! Let's enjoy this together!"},
|
36 |
}
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Function to process speech-to-text
|
39 |
def speech_to_text(audio_bytes):
|
40 |
files = {"file": ("audio.wav", audio_bytes, "audio/wav")}
|
|
|
54 |
payload = {"inputs": text}
|
55 |
response = requests.post(HF_TEXT_TO_SPEECH_API, headers=HEADERS, json=payload)
|
56 |
return response.content
|
57 |
+
|
58 |
+
# Function to record audio using sounddevice
|
59 |
+
def record_audio(duration=5, samplerate=16000):
|
60 |
+
st.write("ποΈ Recording... Speak now!")
|
61 |
+
audio_data = sd.rec(int(samplerate * duration), samplerate=samplerate, channels=1, dtype=np.int16)
|
62 |
+
sd.wait()
|
63 |
+
st.write("β
Recording finished!")
|
64 |
+
|
65 |
+
# Save as WAV file
|
66 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
67 |
+
write(tmpfile.name, samplerate, audio_data)
|
68 |
+
tmpfile.seek(0)
|
69 |
+
return tmpfile.read()
|
70 |
+
|
71 |
# Streamlit UI
|
72 |
st.title("ποΈ AI Speaking Pal")
|
73 |
st.write("Press the button and chat with your pal!")
|
74 |
|
75 |
+
# Start/Stop Conversation Button
|
76 |
+
if "conversation_active" not in st.session_state:
|
77 |
+
st.session_state.conversation_active = False
|
78 |
+
|
79 |
+
def toggle_conversation():
|
80 |
+
st.session_state.conversation_active = not st.session_state.conversation_active
|
81 |
+
|
82 |
+
st.button("π€ Start/Stop Conversation", on_click=toggle_conversation)
|
83 |
|
84 |
+
# Conversation Text Box
|
85 |
+
show_text = st.checkbox("Show conversation text")
|
86 |
+
if show_text:
|
87 |
+
conversation_box = st.text_area("Conversation:", height=200)
|
88 |
|
89 |
+
# Handle microphone input
|
90 |
+
if st.session_state.conversation_active:
|
91 |
+
audio_bytes = record_audio(duration=5) # Record for 5 seconds
|
92 |
user_text = speech_to_text(audio_bytes)
|
93 |
st.write(f"**You:** {user_text}")
|
94 |
|
|
|
95 |
ai_reply = chatbot_response(user_text)
|
96 |
st.write(f"**AI:** {ai_reply}")
|
97 |
|
|
|
98 |
speech_audio = text_to_speech(ai_reply)
|
99 |
+
st.audio(io.BytesIO(speech_audio), format="audio/wav")
|
100 |
|
101 |
# Swiper Carousel Component
|
102 |
avatar_list = list(avatars.keys())
|
|
|
113 |
audio_bytes = text_to_speech(avatar_info["voice"])
|
114 |
st.audio(io.BytesIO(audio_bytes), format="audio/wav")
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
st.write("Have fun learning English with your AI pal!")
|