Spaces:
Build error
Build error
Create app.py
Browse filesinitial commit
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
import io
|
6 |
+
import soundfile as sf
|
7 |
+
|
8 |
+
# Hugging Face API Key (Make sure to add it in your HF Space secrets)
|
9 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
10 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
11 |
+
|
12 |
+
# Hugging Face Model APIs
|
13 |
+
HF_SPEECH_TO_TEXT_API = "https://api-inference.huggingface.co/models/openai/whisper-small"
|
14 |
+
HF_CHATBOT_API = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct"
|
15 |
+
HF_TEXT_TO_SPEECH_API = "https://api-inference.huggingface.co/models/facebook/mms-tts-eng"
|
16 |
+
|
17 |
+
# Chinese Zodiac Avatars with Personalities
|
18 |
+
avatars = {
|
19 |
+
"Rat": "Clever and resourceful. Loves puzzles and word games.",
|
20 |
+
"Ox": "Patient and wise. Encourages steady learning.",
|
21 |
+
"Tiger": "Brave and adventurous. Loves storytelling!",
|
22 |
+
"Rabbit": "Gentle and kind. Makes learning fun and safe.",
|
23 |
+
"Dragon": "Bold and confident. Loves to challenge students!",
|
24 |
+
"Snake": "Calm and analytical. Gives insightful explanations.",
|
25 |
+
"Horse": "Energetic and playful. Encourages fast-paced learning!",
|
26 |
+
"Goat": "Creative and artistic. Uses imagination in learning.",
|
27 |
+
"Monkey": "Curious and mischievous. Always has fun facts!",
|
28 |
+
"Rooster": "Diligent and disciplined. Helps with structured learning.",
|
29 |
+
"Dog": "Loyal and friendly. Encourages confidence.",
|
30 |
+
"Pig": "Easygoing and supportive. Encourages relaxed learning."
|
31 |
+
}
|
32 |
+
|
33 |
+
# Function to process speech-to-text
|
34 |
+
def speech_to_text(audio_path):
|
35 |
+
with open(audio_path, "rb") as f:
|
36 |
+
files = {"file": f}
|
37 |
+
response = requests.post(HF_SPEECH_TO_TEXT_API, headers=HEADERS, files=files)
|
38 |
+
return response.json().get("text", "Could not transcribe audio.")
|
39 |
+
|
40 |
+
# Function to get chatbot response
|
41 |
+
def chatbot_response(user_input, avatar):
|
42 |
+
personality = avatars.get(avatar, "Friendly and supportive.")
|
43 |
+
prompt = f"You are {avatar}, an AI speaking coach. You are {personality}.\n"
|
44 |
+
prompt += f"Help the student practice English in a fun and engaging way.\n"
|
45 |
+
prompt += f"User: {user_input}\n{avatar}:"
|
46 |
+
payload = {"inputs": prompt}
|
47 |
+
response = requests.post(HF_CHATBOT_API, headers=HEADERS, json=payload)
|
48 |
+
return response.json().get("generated_text", "I'm here to help!")
|
49 |
+
|
50 |
+
# Function for text-to-speech
|
51 |
+
def text_to_speech(text):
|
52 |
+
payload = {"inputs": text}
|
53 |
+
response = requests.post(HF_TEXT_TO_SPEECH_API, headers=HEADERS, json=payload)
|
54 |
+
return response.content
|
55 |
+
|
56 |
+
# Streamlit UI
|
57 |
+
st.title("🎙️ AI Speaking Coach - Talking Pals")
|
58 |
+
st.write("Choose an avatar and start speaking!")
|
59 |
+
|
60 |
+
# Select Avatar
|
61 |
+
avatar = st.selectbox("Pick your speaking coach:", list(avatars.keys()))
|
62 |
+
st.write(f"**{avatar}** - {avatars[avatar]}")
|
63 |
+
|
64 |
+
# Upload or record audio
|
65 |
+
audio_file = st.file_uploader("Record or upload your voice", type=["wav", "mp3"])
|
66 |
+
show_text = st.checkbox("Show conversation text")
|
67 |
+
|
68 |
+
if audio_file:
|
69 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
70 |
+
tmpfile.write(audio_file.getvalue())
|
71 |
+
tmpfile_path = tmpfile.name
|
72 |
+
|
73 |
+
st.audio(tmpfile_path)
|
74 |
+
|
75 |
+
user_text = speech_to_text(tmpfile_path)
|
76 |
+
if show_text:
|
77 |
+
st.write(f"**You:** {user_text}")
|
78 |
+
|
79 |
+
ai_reply = chatbot_response(user_text, avatar)
|
80 |
+
if show_text:
|
81 |
+
st.write(f"**{avatar}:** {ai_reply}")
|
82 |
+
|
83 |
+
speech_audio = text_to_speech(ai_reply)
|
84 |
+
audio_bytes = io.BytesIO(speech_audio)
|
85 |
+
st.audio(audio_bytes, format="audio/wav")
|