File size: 1,768 Bytes
69f0d18
d4040f9
11c8c1b
69f0d18
d4040f9
11c8c1b
d4040f9
 
 
11c8c1b
 
 
 
d4040f9
11c8c1b
d4040f9
 
 
11c8c1b
 
d4040f9
 
11c8c1b
d4040f9
 
 
 
11c8c1b
d4040f9
 
 
 
 
 
 
 
 
 
11c8c1b
d4040f9
 
 
11c8c1b
d4040f9
11c8c1b
d4040f9
 
11c8c1b
d4040f9
 
 
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
import streamlit as st
import google.generativeai as genai
import time

# === 1. KONFIGURASI GEMINI API ===
GEMINI_API_KEY = "AIzaSyBE9-DBPLH__WC2jrTJIb4jbWfTNb5lbMs"  # ganti dengan API key kamu dari Google AI Studio
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel("gemini-2.0-flash")

# === 2. KONFIGURASI STREAMLIT ===
st.set_page_config(page_title="Gemini Chatbot", page_icon="🤖", layout="centered")
st.title("🤖 Heru Arranuri, S.T Chatbot")
st.caption("Ditenagai oleh Google Gemini API via Streamlit")

# === 3. SIMPAN RIWAYAT CHAT ===
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

# === 4. INPUT USER ===
user_input = st.chat_input("Ketik pesan kamu di sini...")

if user_input:
    # Tampilkan pesan user langsung
    st.session_state.chat_history.append({"role": "user", "text": user_input})
    with st.chat_message("user"):
        st.markdown(user_input)

    # Proses respons dari model Gemini
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""

        try:
            response = model.generate_content(user_input)
            reply = response.text.strip()
        except Exception as e:
            reply = f"[Error] {e}"

        # Efek mengetik per kata
        for word in reply.split():
            full_response += word + " "
            message_placeholder.markdown(full_response + "▌")
            time.sleep(0.05)

        message_placeholder.markdown(full_response)
        st.session_state.chat_history.append({"role": "assistant", "text": full_response})

# === 5. TAMPILKAN RIWAYAT CHAT SEBELUMNYA ===
for chat in st.session_state.chat_history:
    with st.chat_message(chat["role"]):
        st.markdown(chat["text"])