import kivy from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.scrollview import ScrollView from kivy.uix.widget import Widget from kivy.core.window import Window import threading import requests import tempfile import wave import pyaudio import boto3 import anthropic import os from google.cloud import speech import io import os os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/Users/doggycelik/Downloads/sigma-night-432821-i7-e512f4b1dd84.json' # AWS Polly ve API anahtarları (bu bilgileri gizli tutmak önemlidir) aws_access_key_id = 'AKIAXZEFIC4QUGG3HDFT' aws_secret_access_key = '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' aws_region_name = 'eu-west-1' deepgram_api_key = '43d3e50ef1afadc69d0b5fb985cc79276cf60572' api_key = "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" # Anthropic istemcisini oluşturun client = anthropic.Anthropic(api_key=api_key) # Geçmiş mesajları tutmak için liste history = [] # Dinleme işlemini kontrol etmek için bir olay stop_event = threading.Event() def get_claude_response(user_input): global history system_message = "Sen acil servise gelmiş sinirli ve ajite, çok sinirli bir acil durum hastasısın. Bir doktor sana tıbbi durumunla ilgili sorular soracak lütfen bu duruma göre cevaplar ver." history.append({"role": "user", "content": user_input}) response = client.messages.create( model="claude-3-5-sonnet-20240620", max_tokens=1000, temperature=0, system=system_message, messages=history ) response_content = ''.join([block.text for block in response.content if hasattr(block, 'text')]) history.append({"role": "assistant", "content": response_content}) return response_content def text_to_speech(text, output_file): polly = boto3.client('polly', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region_name) response = polly.synthesize_speech( Text=text, OutputFormat='mp3', VoiceId='Filiz' ) with open(output_file, 'wb') as file: file.write(response['AudioStream'].read()) def speech_to_text(): chunk = 1024 sample_rate = 16000 format = pyaudio.paInt16 channels = 1 p = pyaudio.PyAudio() stream = p.open(format=format, channels=channels, rate=sample_rate, input=True, frames_per_buffer=chunk) audio_data = b'' for _ in range(0, int(sample_rate / chunk * 5)): data = stream.read(chunk) audio_data += data if stop_event.is_set(): break stream.stop_stream() stream.close() p.terminate() client = speech.SpeechClient() with io.BytesIO(audio_data) as audio_file: audio_content = audio_file.read() audio = speech.RecognitionAudio(content=audio_content) config = speech.RecognitionConfig( encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=sample_rate, language_code="tr-TR", ) response = client.recognize(config=config, audio=audio) for result in response.results: return result.alternatives[0].transcript return "Tanımlanamadı." class MyApp(App): def build(self): Window.clearcolor = (1, 1, 1, 1) # Apple estetiği için beyaz arka plan main_layout = BoxLayout(orientation='vertical', padding=30, spacing=20) # Başlık header_layout = BoxLayout(orientation='horizontal', size_hint_y=None, height=100, padding=[20, 0]) self.title_label = Label( text='d\'OGGY GPT', font_size='40sp', bold=True, color=(0, 0, 0, 1), # Siyah renk size_hint_x=None, width=Window.width - 40, halign='center', valign='middle' ) header_layout.add_widget(Widget(size_hint_x=1)) header_layout.add_widget(self.title_label) header_layout.add_widget(Widget(size_hint_x=1)) main_layout.add_widget(header_layout) # Başlat metni self.start_text = Label( text='Konuşmaya başlamak için başlat tuşuna basınız', size_hint_y=None, height=50, font_size='18sp', color=(0.7, 0.7, 0.7, 1), # Açık gri renk halign='center' ) main_layout.add_widget(self.start_text) # Terminal çıktısı self.terminal_label = Label( text='Konuşma ve Claude yanıtları burada görünecek!! \n', size_hint_y=None, height=400, font_size='16sp', color=(0, 0, 0, 1), # Siyah metin valign='top', halign='left', text_size=(Window.width - 40, None), markup=True ) scroll_view = ScrollView(size_hint=(1, None), size=(Window.width, 400)) scroll_view.add_widget(self.terminal_label) main_layout.add_widget(scroll_view) # Butonlar ve etiket bottom_layout = BoxLayout(orientation='horizontal', padding=20, spacing=20, size_hint_y=None, height=100) # Sol taraf: Başlat metni left_layout = BoxLayout(orientation='vertical', size_hint_x=0.5) self.label = Label( text='Başlat butonuna basarak konuşmayı başlatın.', size_hint_x=None, size_hint_y=None, height=50, color=(0.7, 0.7, 0.7, 1) ) left_layout.add_widget(self.label) # Sağ taraf: Butonlar button_layout = BoxLayout(orientation='vertical', size_hint_x=0.5, spacing=10) self.button = Button( text='Başla', size_hint=(None, None), width=180, height=60, background_color=(0.0, 0.5, 1, 1), # Mavi arka plan color=(1, 1, 1, 1), # Beyaz metin font_size='18sp' ) self.button.bind(on_press=self.start_listening) self.stop_button = Button( text='Bitir', size_hint=(None, None), width=180, height=60, background_color=(1, 0.3, 0.3, 1), # Kırmızı arka plan color=(1, 1, 1, 1), # Beyaz metin font_size='18sp' ) self.stop_button.bind(on_press=self.stop_listening) button_layout.add_widget(self.button) button_layout.add_widget(self.stop_button) bottom_layout.add_widget(left_layout) bottom_layout.add_widget(button_layout) main_layout.add_widget(bottom_layout) return main_layout def start_listening(self, instance): self.start_text.color = (0, 1, 0, 1) # Yeşil renk stop_event.clear() threading.Thread(target=self.listen).start() def stop_listening(self, instance): stop_event.set() def listen(self): text = speech_to_text() if text: self.terminal_label.text += f'Ben: {text}\n' response = get_claude_response(text) self.terminal_label.text += f'd\'OGGY: {response}\n' output_file = 'output.mp3' text_to_speech(response, output_file) os.system(f'afplay {output_file}') # Ses dosyasını çal (macOS için) os.remove(output_file) if __name__ == '__main__': MyApp().run()