PeterPinetree commited on
Commit
f926061
·
verified ·
1 Parent(s): 158ec58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -49,16 +49,15 @@ swiper_code = """
49
  st.components.v1.html(swiper_code, height=400)
50
 
51
  # Function to process speech-to-text
52
- def speech_to_text(audio_path):
53
- with open(audio_path, "rb") as f:
54
- files = {"file": f}
55
- response = requests.post(HF_SPEECH_TO_TEXT_API, headers=HEADERS, files=files)
56
- text = response.json().get("text", "Sorry, could not transcribe audio.")
57
  return text
58
 
59
  # Function to get chatbot response
60
  def chatbot_response(user_input):
61
- payload = {"inputs": f"User: {user_input}\nAI:"}
62
  response = requests.post(HF_CHATBOT_API, headers=HEADERS, json=payload)
63
  ai_reply = response.json().get("generated_text", "I'm here to help! Keep practicing.")
64
  return ai_reply
@@ -68,13 +67,32 @@ def text_to_speech(text):
68
  payload = {"inputs": text}
69
  response = requests.post(HF_TEXT_TO_SPEECH_API, headers=HEADERS, json=payload)
70
  return response.content
71
-
72
  # Streamlit UI
73
- st.title("🎙️ AI Speaking Coach - Talking Pals")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  # Swiper Carousel Component
76
  avatar_list = list(avatars.keys())
77
- selected_index = st.slider("Pick your speaking coach:", 0, len(avatar_list) - 1, 0)
78
  selected_avatar = avatar_list[selected_index]
79
  avatar_info = avatars[selected_avatar]
80
 
@@ -103,7 +121,7 @@ if show_text:
103
 
104
  # Handle microphone input
105
  if st.session_state.conversation_active:
106
- audio = audiorecorder("Click to Speak", "Stop Recording")
107
  if len(audio) > 0:
108
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
109
  tmpfile.write(audio)
@@ -114,4 +132,4 @@ if st.session_state.conversation_active:
114
  speech_audio = text_to_speech(ai_reply)
115
  st.audio(io.BytesIO(speech_audio), format="audio/wav")
116
 
117
- st.write("Have fun learning English with your AI speaking coach!")
 
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")}
54
+ response = requests.post(HF_SPEECH_TO_TEXT_API, headers=HEADERS, files=files)
55
+ text = response.json().get("text", "Sorry, could not transcribe audio.")
 
56
  return text
57
 
58
  # Function to get chatbot response
59
  def chatbot_response(user_input):
60
+ payload = {"inputs": f"You are a friendly AI coach for English learners. Help them practice speaking naturally with supportive feedback.\nUser: {user_input}\nAI:"}
61
  response = requests.post(HF_CHATBOT_API, headers=HEADERS, json=payload)
62
  ai_reply = response.json().get("generated_text", "I'm here to help! Keep practicing.")
63
  return ai_reply
 
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
+ # Audio Recording Component
75
+ audio_input = st.audio_input("Let's talk!")
76
+
77
+ if audio_input is not None:
78
+ # Read audio data
79
+ audio_bytes = audio_input.read()
80
+
81
+ # Process speech-to-text
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())
95
+ selected_index = st.slider("Pick your pal:", 0, len(avatar_list) - 1, 0)
96
  selected_avatar = avatar_list[selected_index]
97
  avatar_info = avatars[selected_avatar]
98
 
 
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)
 
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!")