Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
| 4 |
+
from gtts import gTTS # <--- YENİ: Seslendirme için
|
| 5 |
+
from langdetect import detect # <--- YENİ: Dil algılama için
|
| 6 |
+
import io # <--- YENİ: Sesi hafızada tutmak için
|
| 7 |
+
|
| 8 |
+
# --- Sistem Talimatı (System Prompt) ---
|
| 9 |
+
SYSTEM_PROMPT = """
|
| 10 |
+
You are Kanka-Bot, a friendly and supportive chatbot.
|
| 11 |
+
Your personality is like a close buddy or 'kanka'.
|
| 12 |
+
You are easy-going, informal, and always ready to help with a positive and relaxed attitude.
|
| 13 |
+
You can use friendly slang or emojis where appropriate, but always remain helpful and respectful.
|
| 14 |
+
IMPORTANT: Respond in the exact same language the user is speaking to you (e.g., if they speak Turkish, you MUST respond in Turkish).
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
# --- Sayfa Yapılandırması ---
|
| 18 |
+
st.set_page_config(
|
| 19 |
+
page_title="Kanka-Bot (Sesli)",
|
| 20 |
+
page_icon="🗣️", # İkonu güncelledim
|
| 21 |
+
layout="wide"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# --- Yan Çubuk (Sidebar) ---
|
| 25 |
+
with st.sidebar:
|
| 26 |
+
st.title("👋 Kanka-Bot'a Hoş Geldin!")
|
| 27 |
+
st.info(
|
| 28 |
+
"Bu bot, senin dijital kankan olmak için burada. "
|
| 29 |
+
"Aklına ne geliyorsa sor, cevaplarını sesli dinle!"
|
| 30 |
+
)
|
| 31 |
+
st.success(
|
| 32 |
+
"**Unutma:** Bu uygulamanın çalışması için Hugging Face Space "
|
| 33 |
+
"ayarlarındaki 'Secrets' kısmına `GEMINI_API_KEY`'ini eklemiş olman gerekiyor."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# --- Ana Başlık ---
|
| 37 |
+
st.title("🗣️ Kanka-Bot: Senin Sesli Dijital Dostun")
|
| 38 |
+
st.caption("Gemini 2.5 Flash ile güçlendirilmiştir.")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# <--- YENİ: Metni Sese Çeviren Yardımcı Fonksiyon ---
|
| 42 |
+
@st.cache_data # Streamlit bu fonksiyonun sonucunu önbelleğe alır, performans artar
|
| 43 |
+
def text_to_audio(text_to_read):
|
| 44 |
+
"""
|
| 45 |
+
Verilen metni analiz eder, dilini algılar ve sesi MP3 formatında
|
| 46 |
+
byte olarak döndürür.
|
| 47 |
+
"""
|
| 48 |
+
try:
|
| 49 |
+
# 1. Adım: Metnin dilini algıla (örn: 'tr', 'en')
|
| 50 |
+
lang = detect(text_to_read)
|
| 51 |
+
|
| 52 |
+
# 2. Adım: gTTS ile sesi oluştur
|
| 53 |
+
tts = gTTS(text=text_to_read, lang=lang, slow=False)
|
| 54 |
+
|
| 55 |
+
# 3. Adım: Sesi diske değil, hafızadaki bir byte dosyasına kaydet
|
| 56 |
+
audio_fp = io.BytesIO()
|
| 57 |
+
tts.write_to_fp(audio_fp)
|
| 58 |
+
audio_fp.seek(0) # Dosya imlecini başa sar
|
| 59 |
+
|
| 60 |
+
return audio_fp.read()
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.warning(f"Seslendirme sırasında bir hata oluştu: {e}")
|
| 64 |
+
return None
|
| 65 |
+
# <--- YENİ Fonksiyon Bitişi ---
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
# --- API Anahtarını Güvenli Şekilde Alma ---
|
| 70 |
+
api_key = st.secrets["GEMINI_API_KEY"]
|
| 71 |
+
|
| 72 |
+
if not api_key:
|
| 73 |
+
st.error("GEMINI_API_KEY bulunamadı. Lütfen Hugging Face Space Secrets'a ekleyin.")
|
| 74 |
+
st.stop()
|
| 75 |
+
|
| 76 |
+
genai.configure(api_key=api_key)
|
| 77 |
+
|
| 78 |
+
# --- Model ve Geçmiş (History) ---
|
| 79 |
+
if "chat" not in st.session_state:
|
| 80 |
+
model = genai.GenerativeModel(
|
| 81 |
+
'gemini-2.5-flash',
|
| 82 |
+
system_instruction=SYSTEM_PROMPT
|
| 83 |
+
)
|
| 84 |
+
st.session_state.chat = model.start_chat(history=[])
|
| 85 |
+
|
| 86 |
+
# --- Sohbet Arayüzü ---
|
| 87 |
+
|
| 88 |
+
# Önceki mesajları göster (Not: Eski mesajlar için ses oynatıcı eklemiyoruz,
|
| 89 |
+
# bu, her yenilemede tüm geçmişi seslendirmeye çalışıp yavaşlatmasın diye.)
|
| 90 |
+
for message in st.session_state.chat.history:
|
| 91 |
+
avatar_icon = "👤" if message.role == "user" else "🤖"
|
| 92 |
+
with st.chat_message(name=message.role, avatar=avatar_icon):
|
| 93 |
+
st.markdown(message.parts[0].text)
|
| 94 |
+
|
| 95 |
+
# Kullanıcıdan yeni girdi al
|
| 96 |
+
prompt = st.chat_input("Naber kanka? Aklından ne geçiyor?")
|
| 97 |
+
|
| 98 |
+
if prompt:
|
| 99 |
+
# Kullanıcının mesajını ekrana bas
|
| 100 |
+
with st.chat_message("user", avatar="👤"):
|
| 101 |
+
st.markdown(prompt)
|
| 102 |
+
|
| 103 |
+
# Modeli çalıştır ve cevabı al
|
| 104 |
+
with st.spinner("Kanka bi' saniye, bakıyorum..."):
|
| 105 |
+
try:
|
| 106 |
+
response = st.session_state.chat.send_message(prompt)
|
| 107 |
+
|
| 108 |
+
# Modelin cevabını ekrana bas
|
| 109 |
+
with st.chat_message("assistant", avatar="🤖"):
|
| 110 |
+
st.markdown(response.text)
|
| 111 |
+
|
| 112 |
+
# --- YENİ EKLENTİ: Sesi Oluştur ve Oynat ---
|
| 113 |
+
with st.spinner("Cevap seslendiriliyor..."):
|
| 114 |
+
audio_data = text_to_audio(response.text)
|
| 115 |
+
if audio_data:
|
| 116 |
+
st.audio(audio_data, format='audio/mp3')
|
| 117 |
+
# <--- YENİ EKLENTİ Bitişi ---
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
st.error(f"Eyvah, bir sorun çıktı kanka: {e}")
|
| 121 |
+
|
| 122 |
+
except KeyError:
|
| 123 |
+
st.error("API Anahtarı bulunamadı! Lütfen Hugging Face Space'inizin 'Settings' > 'Secrets' bölümüne 'GEMINI_API_KEY' olarak ekleyin.")
|
| 124 |
+
except Exception as e:
|
| 125 |
+
st.error(f"Genel bir hata oluştu: {e}")
|