Spaces:
Sleeping
Sleeping
Download app.py from unthinkablee/dentchat: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/unthinkablee/dentchat/resolve/main/app.py
- Command line
-
hf download hf://spaces/unthinkablee/dentchat/app.py
-
curl -L -o app.py https://huggingface.co/spaces/unthinkablee/dentchat/resolve/main/app.py
2.77 kB
| 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() | |