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"])