| import streamlit as st |
| import google.generativeai as genai |
| import os |
| from gtts import gTTS |
| from langdetect import detect |
| import io |
|
|
| |
| SYSTEM_PROMPT = """ |
| You are Kanka-Bot, a friendly and supportive chatbot. |
| Your personality is like a close buddy or 'kanka'. |
| You are easy-going, informal, and always ready to help with a positive and relaxed attitude. |
| You can use friendly slang or emojis where appropriate, but always remain helpful and respectful. |
| IMPORTANT: Respond in the exact same language the user is speaking to you (e.g., if they speak Turkish, you MUST respond in Turkish). |
| """ |
|
|
| |
| st.set_page_config( |
| page_title="Kanka-Bot (Sesli)", |
| page_icon="🗣️", |
| layout="wide" |
| ) |
|
|
| |
| with st.sidebar: |
| st.title("👋 Kanka-Bot'a Hoş Geldin!") |
| st.info( |
| "Bu bot, senin dijital kankan olmak için burada. " |
| "Aklına ne geliyorsa sor, cevaplarını sesli dinle!" |
| ) |
| st.success( |
| "**Unutma:** Bu uygulamanın çalışması için Hugging Face Space " |
| "ayarlarındaki 'Secrets' kısmına `GEMINI_API_KEY`'ini eklemiş olman gerekiyor." |
| ) |
|
|
| |
| st.title("🗣️ Kanka-Bot: Senin Sesli Dijital Dostun") |
| st.caption("Gemini 2.5 Flash ile güçlendirilmiştir.") |
|
|
|
|
| |
| @st.cache_data |
| def text_to_audio(text_to_read): |
| """ |
| Verilen metni analiz eder, dilini algılar ve sesi MP3 formatında |
| byte olarak döndürür. |
| """ |
| try: |
| |
| lang = detect(text_to_read) |
| |
| |
| tts = gTTS(text=text_to_read, lang=lang, slow=False) |
| |
| |
| audio_fp = io.BytesIO() |
| tts.write_to_fp(audio_fp) |
| audio_fp.seek(0) |
| |
| return audio_fp.read() |
| |
| except Exception as e: |
| st.warning(f"Seslendirme sırasında bir hata oluştu: {e}") |
| return None |
| |
|
|
|
|
| try: |
| |
| api_key = st.secrets["GEMINI_API_KEY"] |
| |
| if not api_key: |
| st.error("GEMINI_API_KEY bulunamadı. Lütfen Hugging Face Space Secrets'a ekleyin.") |
| st.stop() |
|
|
| genai.configure(api_key=api_key) |
|
|
| |
| if "chat" not in st.session_state: |
| model = genai.GenerativeModel( |
| 'gemini-2.5-flash', |
| system_instruction=SYSTEM_PROMPT |
| ) |
| st.session_state.chat = model.start_chat(history=[]) |
|
|
| |
| |
| |
| |
| for message in st.session_state.chat.history: |
| avatar_icon = "👤" if message.role == "user" else "🤖" |
| with st.chat_message(name=message.role, avatar=avatar_icon): |
| st.markdown(message.parts[0].text) |
|
|
| |
| prompt = st.chat_input("Naber kanka? Aklından ne geçiyor?") |
|
|
| if prompt: |
| |
| with st.chat_message("user", avatar="👤"): |
| st.markdown(prompt) |
| |
| |
| with st.spinner("Kanka bi' saniye, bakıyorum..."): |
| try: |
| response = st.session_state.chat.send_message(prompt) |
| |
| |
| with st.chat_message("assistant", avatar="🤖"): |
| st.markdown(response.text) |
| |
| |
| with st.spinner("Cevap seslendiriliyor..."): |
| audio_data = text_to_audio(response.text) |
| if audio_data: |
| st.audio(audio_data, format='audio/mp3') |
| |
| |
| except Exception as e: |
| st.error(f"Eyvah, bir sorun çıktı kanka: {e}") |
|
|
| except KeyError: |
| st.error("API Anahtarı bulunamadı! Lütfen Hugging Face Space'inizin 'Settings' > 'Secrets' bölümüne 'GEMINI_API_KEY' olarak ekleyin.") |
| except Exception as e: |
| st.error(f"Genel bir hata oluştu: {e}") |