Spaces:
Sleeping
Sleeping
Delete streamlit_app.py
Browse files- streamlit_app.py +0 -178
streamlit_app.py
DELETED
|
@@ -1,178 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import asyncio
|
| 3 |
-
import os
|
| 4 |
-
from manager import TourManager
|
| 5 |
-
|
| 6 |
-
def tts(text, voice_style):
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
|
| 9 |
-
speech_file_path = Path(__file__).parent / f"speech_tour.mp3"
|
| 10 |
-
|
| 11 |
-
if voice_style == "Google Translate (Chị Google - Miễn Phí)":
|
| 12 |
-
from gtts import gTTS
|
| 13 |
-
tts = gTTS(text, lang='vi')
|
| 14 |
-
tts.save(str(speech_file_path))
|
| 15 |
-
|
| 16 |
-
elif voice_style == "Edge TTS (Giọng Nữ - Hoài My)":
|
| 17 |
-
import edge_tts
|
| 18 |
-
communicate = edge_tts.Communicate(text, "vi-VN-HoaiMyNeural")
|
| 19 |
-
loop = asyncio.new_event_loop()
|
| 20 |
-
asyncio.set_event_loop(loop)
|
| 21 |
-
loop.run_until_complete(communicate.save(str(speech_file_path)))
|
| 22 |
-
loop.close()
|
| 23 |
-
|
| 24 |
-
elif voice_style == "Edge TTS (Giọng Nam - Nam Minh)":
|
| 25 |
-
import edge_tts
|
| 26 |
-
communicate = edge_tts.Communicate(text, "vi-VN-NamMinhNeural")
|
| 27 |
-
loop = asyncio.new_event_loop()
|
| 28 |
-
asyncio.set_event_loop(loop)
|
| 29 |
-
loop.run_until_complete(communicate.save(str(speech_file_path)))
|
| 30 |
-
loop.close()
|
| 31 |
-
|
| 32 |
-
elif voice_style == "Google Cloud TTS (Giọng Tự Nhiên - Cần Credentials)":
|
| 33 |
-
from google.cloud import texttospeech
|
| 34 |
-
# Requires GOOGLE_APPLICATION_CREDENTIALS environment variable
|
| 35 |
-
client = texttospeech.TextToSpeechClient()
|
| 36 |
-
|
| 37 |
-
# Split text into chunks if too long (GCP has 5000 byte limit per request)
|
| 38 |
-
# For simplicity, we just take the first 4500 characters here if it's too long
|
| 39 |
-
# A robust solution would stitch multiple MP3s together.
|
| 40 |
-
if len(text) > 4500:
|
| 41 |
-
text = text[:4500]
|
| 42 |
-
|
| 43 |
-
synthesis_input = texttospeech.SynthesisInput(text=text)
|
| 44 |
-
voice = texttospeech.VoiceSelectionParams(
|
| 45 |
-
language_code="vi-VN", name="vi-VN-Standard-A"
|
| 46 |
-
)
|
| 47 |
-
audio_config = texttospeech.AudioConfig(
|
| 48 |
-
audio_encoding=texttospeech.AudioEncoding.MP3
|
| 49 |
-
)
|
| 50 |
-
response = client.synthesize_speech(
|
| 51 |
-
input=synthesis_input, voice=voice, audio_config=audio_config
|
| 52 |
-
)
|
| 53 |
-
with open(speech_file_path, "wb") as out:
|
| 54 |
-
out.write(response.audio_content)
|
| 55 |
-
|
| 56 |
-
return speech_file_path
|
| 57 |
-
|
| 58 |
-
def run_async(func, *args, **kwargs):
|
| 59 |
-
try:
|
| 60 |
-
return asyncio.run(func(*args, **kwargs))
|
| 61 |
-
except RuntimeError:
|
| 62 |
-
loop = asyncio.get_event_loop()
|
| 63 |
-
return loop.run_until_complete(func(*args, **kwargs))
|
| 64 |
-
|
| 65 |
-
# Set page config for a better UI
|
| 66 |
-
st.set_page_config(
|
| 67 |
-
page_title="AI Audio Tour Agent",
|
| 68 |
-
page_icon="🎧",
|
| 69 |
-
layout="wide",
|
| 70 |
-
initial_sidebar_state="expanded"
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
# Sidebar for API key
|
| 74 |
-
with st.sidebar:
|
| 75 |
-
st.title("🔑 Cài đặt Hệ thống")
|
| 76 |
-
# Try to get API key from environment (Hugging Face Secrets) or session state
|
| 77 |
-
saved_api_key = os.environ.get("GEMINI_API_KEY", st.session_state.get("GEMINI_API_KEY", ""))
|
| 78 |
-
|
| 79 |
-
api_key = st.text_input("Gemini API Key:", value=saved_api_key, type="password", help="Nhập khóa API của Google Gemini")
|
| 80 |
-
if api_key:
|
| 81 |
-
st.session_state["GEMINI_API_KEY"] = api_key
|
| 82 |
-
os.environ["GEMINI_API_KEY"] = api_key
|
| 83 |
-
if api_key != saved_api_key:
|
| 84 |
-
st.success("Đã lưu API key!")
|
| 85 |
-
|
| 86 |
-
st.markdown("---")
|
| 87 |
-
st.markdown("### Dịch vụ Google Cloud (Tùy chọn)")
|
| 88 |
-
st.markdown("Chỉ cần thiết nếu bạn chọn **Google Cloud TTS**. Bạn phải đặt biến môi trường `GOOGLE_APPLICATION_CREDENTIALS` trỏ tới file JSON xác thực của GCP trên máy chủ.")
|
| 89 |
-
|
| 90 |
-
# Main content
|
| 91 |
-
st.title("🎧 Hướng dẫn viên AI Audio Tour")
|
| 92 |
-
st.markdown("""
|
| 93 |
-
<div style='background-color: #f0f2f6; padding: 20px; border-radius: 10px; margin-bottom: 20px;'>
|
| 94 |
-
<h3>Chào mừng bạn đến với chuyến tham quan bằng âm thanh!</h3>
|
| 95 |
-
<p>Hệ thống sử dụng sức mạnh của <b>Google Gemini</b> để tổng hợp thông tin, soạn thảo kịch bản và đọc cho bạn nghe thông qua các dịch vụ TTS tốt nhất.</p>
|
| 96 |
-
</div>
|
| 97 |
-
""", unsafe_allow_html=True)
|
| 98 |
-
|
| 99 |
-
# Create a clean layout with cards
|
| 100 |
-
col1, col2 = st.columns([2, 1])
|
| 101 |
-
|
| 102 |
-
with col1:
|
| 103 |
-
st.markdown("### 📍 Bạn muốn đi đâu?")
|
| 104 |
-
location = st.text_input("", placeholder="Nhập tên một thành phố, địa danh, di tích...")
|
| 105 |
-
|
| 106 |
-
st.markdown("### 🎯 Chủ đề quan tâm?")
|
| 107 |
-
interests = st.multiselect(
|
| 108 |
-
"",
|
| 109 |
-
options=["History", "Architecture", "Culinary", "Culture"],
|
| 110 |
-
default=["History", "Architecture"],
|
| 111 |
-
help="Lựa chọn các chủ đề bạn muốn AI tìm hiểu"
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
with col2:
|
| 115 |
-
st.markdown("### ⏱️ Cài đặt Thời lượng")
|
| 116 |
-
duration = st.slider(
|
| 117 |
-
"Độ dài Tour (phút)",
|
| 118 |
-
min_value=5,
|
| 119 |
-
max_value=60,
|
| 120 |
-
value=10,
|
| 121 |
-
step=5,
|
| 122 |
-
help="Tour càng dài AI càng viết chi tiết"
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
st.markdown("### 🎙️ Dịch vụ Đọc (TTS)")
|
| 126 |
-
voice_style = st.selectbox(
|
| 127 |
-
"Chọn giọng đọc",
|
| 128 |
-
options=[
|
| 129 |
-
"Edge TTS (Giọng Nữ - Hoài My)",
|
| 130 |
-
"Edge TTS (Giọng Nam - Nam Minh)",
|
| 131 |
-
"Google Translate (Chị Google - Miễn Phí)",
|
| 132 |
-
"Google Cloud TTS (Giọng Tự Nhiên - Cần Credentials)"
|
| 133 |
-
],
|
| 134 |
-
help="Chọn công nghệ để chuyển văn bản thành giọng nói"
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
# Generate Tour Button
|
| 138 |
-
if st.button("🎧 Bắt Đầu Tạo Tour", type="primary"):
|
| 139 |
-
if "GEMINI_API_KEY" not in st.session_state:
|
| 140 |
-
st.error("Vui lòng nhập Gemini API key ở thanh bên trái (Sidebar).")
|
| 141 |
-
elif not location:
|
| 142 |
-
st.error("Vui lòng nhập địa danh.")
|
| 143 |
-
elif not interests:
|
| 144 |
-
st.error("Vui lòng chọn ít nhất một chủ đề.")
|
| 145 |
-
else:
|
| 146 |
-
with st.spinner(f"Đang lên kịch bản cho {location}... Quá trình này dùng Gemini và mất khoảng 15-20 giây."):
|
| 147 |
-
mgr = TourManager(location=location, interests=interests, duration=duration)
|
| 148 |
-
try:
|
| 149 |
-
final_tour = mgr.generate_tour()
|
| 150 |
-
|
| 151 |
-
# Display the tour content in an expandable section
|
| 152 |
-
with st.expander("📝 Kịch Bản Tour", expanded=True):
|
| 153 |
-
st.markdown(final_tour)
|
| 154 |
-
|
| 155 |
-
# Add a progress bar for audio generation
|
| 156 |
-
with st.spinner(f"🎙️ Đang tổng hợp giọng nói bằng {voice_style}..."):
|
| 157 |
-
progress_bar = st.progress(0)
|
| 158 |
-
try:
|
| 159 |
-
tour_audio = tts(final_tour, voice_style)
|
| 160 |
-
progress_bar.progress(100)
|
| 161 |
-
|
| 162 |
-
# Display audio player with custom styling
|
| 163 |
-
st.markdown("### 🎧 Nghe Tour")
|
| 164 |
-
st.audio(tour_audio, format="audio/mp3")
|
| 165 |
-
|
| 166 |
-
# Add download button for the audio
|
| 167 |
-
with open(tour_audio, "rb") as file:
|
| 168 |
-
st.download_button(
|
| 169 |
-
label="📥 Tải xuống Audio",
|
| 170 |
-
data=file,
|
| 171 |
-
file_name=f"{location.lower().replace(' ', '_')}_tour.mp3",
|
| 172 |
-
mime="audio/mp3"
|
| 173 |
-
)
|
| 174 |
-
except Exception as tts_error:
|
| 175 |
-
st.error(f"Lỗi khi tổng hợp giọng nói: {str(tts_error)}. Có thể do bạn chọn Google Cloud TTS nhưng chưa thiết lập Credentials.")
|
| 176 |
-
|
| 177 |
-
except Exception as e:
|
| 178 |
-
st.error(f"Lỗi từ hệ thống AI: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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