File size: 1,873 Bytes
112c4c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import nest_asyncio
from src.chatbot import create_rag_chain 

nest_asyncio.apply()




# --- ARAYÜZ TASARIMI ---


st.set_page_config(page_title="Sahaf.AI",layout="centered")


# Ana başlık
st.markdown("<h1 style='text-align: center;'>SAHAF.AI</h1>", unsafe_allow_html=True)

# --- CHATBOT MANTIĞI ---


chain = create_rag_chain()


if "messages" not in st.session_state:
    st.session_state.messages = []


chat_container = st.container(height=500, border=True)

with chat_container:
    
    if not st.session_state.messages:
        st.markdown("### Merhaba, Ben Sahaf.AI! 👋")
        st.info(
            "Kitaplar, yazarları ve eserleri hakkında merak ettiklerini "
            "bana sorabilirsin. Sana yardımcı olmaktan mutluluk duyarım."
        )
        st.markdown("##### Örnek Sorular:")
        st.markdown("- *Ahmet Ümit'in İstanbul Hatırası romanının konusu nedir?*")
        st.markdown("- *Bana polisiye türünde 3 tane roman önerebilir misin?*")
    
   
    else:
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])


if prompt := st.chat_input("Bir yazar veya kitap hakkında soru sorun..."):
   
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Chatbot'un cevabını oluştur
    with st.spinner("Düşünüyorum..."):
       
        try:
            
            result = chain.invoke({"query": prompt})
            response = result["result"]
        
        except Exception as e:
            
            response = "Üzgünüm, bir sorunla karşılaştım. Lütfen birkaç saniye sonra tekrar deneyin."
            print(f"HATA OLUŞTU: {e}") 
       
        
    
    st.session_state.messages.append({"role": "assistant", "content": response})
    
    
    st.rerun()