canksdi commited on
Commit
1c41a7f
·
verified ·
1 Parent(s): 8aff53f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -1
app.py CHANGED
@@ -757,4 +757,145 @@ demo.queue()
757
  demo.launch(debug=True)
758
 
759
  # Tüm fonksiyonlar tanımlandıktan sonra modelleri ısındır
760
- init_models()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
757
  demo.launch(debug=True)
758
 
759
  # Tüm fonksiyonlar tanımlandıktan sonra modelleri ısındır
760
+ init_models()
761
+
762
+ async def create_speech_async(text):
763
+ """Edge TTS ile doğal insan sesi üret - iyileştirilmiş hata yönetimi"""
764
+ try:
765
+ if not text or len(text.strip()) < 1:
766
+ return None
767
+
768
+ # Ses parametrelerini ayarla - daha doğal bir konuşma için
769
+ communicate = edge_tts.Communicate(text, VOICE)
770
+ audio_path = "response.mp3"
771
+
772
+ # Dosya zaten varsa sil
773
+ if os.path.exists(audio_path):
774
+ os.remove(audio_path)
775
+
776
+ await communicate.save(audio_path)
777
+
778
+ # Dosyanın başarıyla oluşturulduğunu kontrol et
779
+ if os.path.exists(audio_path) and os.path.getsize(audio_path) > 0:
780
+ return audio_path
781
+ return None
782
+
783
+ except Exception as e:
784
+ print(f"Ses oluşturma hatası: {str(e)}")
785
+ return None
786
+
787
+ def create_speech(text):
788
+ """Ana thread'de async fonksiyonu çalıştır - hata yönetimi eklenmiş"""
789
+ try:
790
+ if not text or len(text.strip()) < 1:
791
+ print("Ses oluşturulamadı: Metin boş")
792
+ return None
793
+
794
+ # Metni normalize et
795
+ text = normalize_text(text)
796
+
797
+ # Async işlemi çalıştır
798
+ loop = asyncio.new_event_loop()
799
+ asyncio.set_event_loop(loop)
800
+ result = loop.run_until_complete(create_speech_async(text))
801
+ loop.close()
802
+
803
+ if result:
804
+ return result
805
+
806
+ # Ses oluşturulamazsa boş ses dosyası oluştur
807
+ print("Ses dosyası oluşturulamadı, boş dosya döndürülüyor")
808
+ with open("empty.mp3", "wb") as f:
809
+ f.write(b"")
810
+ return "empty.mp3"
811
+
812
+ except Exception as e:
813
+ print(f"Ses oluşturma hatası: {str(e)}")
814
+ # Boş ses dosyası oluştur
815
+ with open("empty.mp3", "wb") as f:
816
+ f.write(b"")
817
+ return "empty.mp3"
818
+
819
+ def get_appropriate_expert_recommendations(text, emotion, score):
820
+ """Kullanıcının durumuna uygun uzman önerileri seçer"""
821
+ text_lower = text.lower()
822
+
823
+ # Uygun öneri kategorisini seç
824
+ if "uyuyam" in text_lower or "uykus" in text_lower or "uyku" in text_lower:
825
+ category = "uyku_problemleri"
826
+ elif "stress" in text_lower or "stres" in text_lower or "gergin" in text_lower or "baskı" in text_lower:
827
+ category = "stress_yonetimi"
828
+ elif "korku" in text_lower or "panik" in text_lower or "endişe" in text_lower or "kaygı" in text_lower:
829
+ category = "kaygi_ve_panik"
830
+ elif "motivasyon" in text_lower or "istek" in text_lower or "yapmak istemiyorum" in text_lower or "enerjim yok" in text_lower:
831
+ category = "motivasyon_eksikligi"
832
+ elif "acı" in text_lower or "keder" in text_lower or "üzüntü" in text_lower or "kayıp" in text_lower:
833
+ category = "duygusal_acı"
834
+ elif "utanç" in text_lower or "sosyal" in text_lower or "insanlar" in text_lower or "topluluk" in text_lower:
835
+ category = "sosyal_kaygi"
836
+ else:
837
+ # Duygu analizi sonucuna göre en uygun kategoriyi seç
838
+ if emotion == "üzüntü":
839
+ category = "duygusal_acı"
840
+ elif emotion == "kötü durum" and score > 0.8:
841
+ category = "stress_yonetimi"
842
+ else:
843
+ # Varsayılan olarak stres yönetimi öneri ver
844
+ category = "stress_yonetimi"
845
+
846
+ # Seçilen kategoriden 1-2 öneri seç
847
+ if category in EXPERT_RECOMMENDATIONS:
848
+ recommendations = EXPERT_RECOMMENDATIONS[category]
849
+ # Eğer birden fazla öneri varsa, rastgele 1-2 tane seç
850
+ if len(recommendations) > 1:
851
+ return random.sample(recommendations, min(2, len(recommendations)))
852
+ return recommendations
853
+
854
+ # Uygun bir kategori bulunamazsa boş liste döndür
855
+ return []
856
+
857
+ def format_recommendations(recommendations):
858
+ """Önerileri okunabilir formatta biçimlendirir"""
859
+ if not recommendations:
860
+ return ""
861
+
862
+ result = "\n\n**İşte sana yardımcı olabilecek birkaç öneri:**\n\n"
863
+
864
+ for i, rec in enumerate(recommendations, 1):
865
+ result += f"**{i}. {rec['title']}**\n"
866
+ result += f"{rec['description']}\n"
867
+ result += f"*Ne zaman kullanmalı:* {rec['when_to_use']}\n"
868
+ result += f"*Faydası:* {rec['benefit']}\n\n"
869
+
870
+ return result
871
+
872
+ def get_personality_profile(text=None, emotion=None):
873
+ """Kullanıcının durum ve ifadesine göre en uygun kişilik profilini seç"""
874
+
875
+ # Eğer metin veya duygu belirtilmediyse varsayılan kişilik kullan
876
+ if not text or not emotion:
877
+ return PERSONALITY_PROFILES[DEFAULT_PERSONALITY]
878
+
879
+ text_lower = text.lower()
880
+
881
+ # Kişilik seçimi için ipuçları ara
882
+ if any(word in text_lower for word in ["motivasyon", "isteksiz", "enerjim yok", "yapamıyorum", "başaramıyorum"]):
883
+ return PERSONALITY_PROFILES["motive_edici_koc"]
884
+ elif any(word in text_lower for word in ["anlamıyorum", "karmaşık", "kafam karıştı", "ne yapmam gerekiyor", "tavsiye"]):
885
+ return PERSONALITY_PROFILES["bilge_danisman"]
886
+ elif any(word in text_lower for word in ["üzgün", "mutsuz", "kötü", "yalnız", "anlaşılmıyorum"]):
887
+ return PERSONALITY_PROFILES["anlayisli_dost"]
888
+ elif any(word in text_lower for word in ["mutlu", "sevinçli", "heyecanlı", "neşeli"]):
889
+ return PERSONALITY_PROFILES["pozitif_arkadas"]
890
+
891
+ # Duygulara göre en uygun kişiliği seç
892
+ if emotion == "üzüntü":
893
+ return PERSONALITY_PROFILES["anlayisli_dost"]
894
+ elif emotion == "mutluluk":
895
+ return PERSONALITY_PROFILES["pozitif_arkadas"]
896
+ elif emotion == "kötü durum":
897
+ # Kötü durum için bilge veya motivasyon arasında rastgele seç
898
+ return random.choice([PERSONALITY_PROFILES["bilge_danisman"], PERSONALITY_PROFILES["motive_edici_koc"]])
899
+
900
+ # Varsayılan olarak anlayışlı dost modelini kullan
901
+ return PERSONALITY_PROFILES[DEFAULT_PERSONALITY]