Spaces:
Sleeping
Sleeping
File size: 2,767 Bytes
5e49e9e 7a9001c 3ca9364 7a9001c 3ca9364 5e49e9e 7a9001c 3ca9364 5e49e9e ee4164b 7a9001c 5e49e9e 3ca9364 5e49e9e d0274d0 5e49e9e 3ca9364 041d619 1eea2c3 041d619 1eea2c3 3ca9364 1eea2c3 3ca9364 a3d418f 5e49e9e 041d619 5e49e9e 3ca9364 399a897 5e49e9e d0274d0 3504983 3ca9364 005f5e8 3ca9364 6bc1d2d 3ca9364 839aadb 3ca9364 d0274d0 3ca9364 839aadb d0274d0 3ca9364 5e49e9e 005f5e8 1eea2c3 7a9001c 1eea2c3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import google.generativeai as genai
import gradio as gr
import speech_recognition as sr
# 1️⃣ Google Generative AI API Anahtarı
genai.configure(api_key="AIzaSyCYXHPlZVZo1vUnV60UacVEr9wLKAWJ42k")
# 2️⃣ Konfigürasyon
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config,
)
# 3️⃣ Sohbet Oturumu
chat_session = model.start_chat(
history=[{"role": "model", "parts": ["Hoş geldiniz! Ben sanal bir diş hekimiyim. 😊"]}]
)
# 4️⃣ Ses Girişini Metne Çevir
def transcribe_audio(audio_file):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
try:
text = recognizer.recognize_google(audio, language="tr-TR")
return text
except sr.UnknownValueError:
return "Ses anlaşılamadı, lütfen tekrar deneyin."
except sr.RequestError:
return "Ses tanıma servisine bağlanılamadı."
# 5️⃣ Yanıt Fonksiyonu
def respond(user_input, history):
if not history:
history = []
chat_session.history.append({"role": "user", "parts": [user_input]})
response = chat_session.send_message(user_input)
chat_session.history.append({"role": "model", "parts": [response.text]})
# Gradio Chatbot formatına uygun ekliyoruz:
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": response.text})
return history, ""
# 6️⃣ Mikrofon Entegrasyonu
def speech_to_text_and_respond(audio, history):
text = transcribe_audio(audio)
return respond(text, history)
# 7️⃣ Gradio Arayüz
with gr.Blocks(css="style.css") as demo:
gr.Markdown("## 👩⚕️ **Sanal Diş Hekimi**")
# 🎥 **Durdurulamaz Loop Video**
video = gr.Video(
value="videodis.mp4",
autoplay=True,
loop=True,
interactive=False,
show_label=False
)
# 💬 Chatbot Bölümü
chatbot = gr.Chatbot(label="Chatbot", type="messages")
with gr.Row():
mic_button = gr.Audio(type="filepath")
user_input = gr.Textbox(show_label=False, placeholder="Bir mesaj yazın...")
send_button = gr.Button("Gönder")
# 📩 Butonlarla Etkileşim
send_button.click(fn=respond, inputs=[user_input, chatbot], outputs=[chatbot, user_input])
user_input.submit(fn=respond, inputs=[user_input, chatbot], outputs=[chatbot, user_input])
mic_button.change(fn=speech_to_text_and_respond, inputs=[mic_button, chatbot], outputs=[chatbot, user_input])
if __name__ == "__main__":
demo.launch()
|